http.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package server
  2. import (
  3. "github.com/casbin/casbin/v2"
  4. "github.com/gin-gonic/gin"
  5. apiV1 "github.com/go-nunu/nunu-layout-advanced/api/v1"
  6. "github.com/go-nunu/nunu-layout-advanced/docs"
  7. "github.com/go-nunu/nunu-layout-advanced/internal/handler"
  8. "github.com/go-nunu/nunu-layout-advanced/internal/middleware"
  9. "github.com/go-nunu/nunu-layout-advanced/pkg/jwt"
  10. "github.com/go-nunu/nunu-layout-advanced/pkg/limiter"
  11. "github.com/go-nunu/nunu-layout-advanced/pkg/log"
  12. "github.com/go-nunu/nunu-layout-advanced/pkg/server/http"
  13. "github.com/spf13/viper"
  14. swaggerfiles "github.com/swaggo/files"
  15. ginSwagger "github.com/swaggo/gin-swagger"
  16. )
  17. func NewHTTPServer(
  18. logger *log.Logger,
  19. conf *viper.Viper,
  20. jwt *jwt.JWT,
  21. e *casbin.SyncedEnforcer,
  22. limiterInstance *limiter.Limiter,
  23. rateLimitMiddleware gin.HandlerFunc,
  24. userHandler *handler.UserHandler,
  25. gameShieldHandler *handler.GameShieldHandler,
  26. gameShieldBackendHandler *handler.GameShieldBackendHandler,
  27. webForwardingHandler *handler.WebForwardingHandler,
  28. weblimitHandler *handler.WebLimitHandler,
  29. tcpForwardingHandler *handler.TcpforwardingHandler,
  30. udpForwardingHandler *handler.UdpForWardingHandler,
  31. tcpLimitHandler *handler.TcpLimitHandler,
  32. udpLimitHandler *handler.UdpLimitHandler,
  33. globalLimitHandler *handler.GlobalLimitHandler,
  34. adminHandler *handler.AdminHandler,
  35. ) *http.Server {
  36. gin.SetMode(gin.DebugMode)
  37. s := http.NewServer(
  38. gin.Default(),
  39. logger,
  40. http.WithServerHost(conf.GetString("http.host")),
  41. http.WithServerPort(conf.GetInt("http.port")),
  42. )
  43. // swagger doc
  44. docs.SwaggerInfo.BasePath = "/v1"
  45. s.GET("/swagger/*any", ginSwagger.WrapHandler(
  46. swaggerfiles.Handler,
  47. //ginSwagger.URL(fmt.Sprintf("http://localhost:%d/swagger/doc.json", conf.GetInt("app.http.port"))),
  48. ginSwagger.DefaultModelsExpandDepth(-1),
  49. ginSwagger.PersistAuthorization(true),
  50. ))
  51. s.Use(
  52. middleware.CORSMiddleware(),
  53. middleware.ResponseLogMiddleware(logger),
  54. middleware.RequestLogMiddleware(logger),
  55. //middleware.SignMiddleware(log),
  56. rateLimitMiddleware,
  57. )
  58. s.GET("/", func(ctx *gin.Context) {
  59. logger.WithContext(ctx).Info("hello")
  60. apiV1.HandleSuccess(ctx, map[string]interface{}{
  61. ":)": "Thank you for using nunu!",
  62. })
  63. })
  64. v1 := s.Group("/v1")
  65. {
  66. // No route group has permission
  67. noAuthRouter := v1.Group("/")
  68. {
  69. // 使用增强的Limiter.GetAPIConfig方法获取特定API的限流配置
  70. // 登录API限流
  71. loginConfig := limiterInstance.GetAPIConfig("login")
  72. noAuthRouter.POST("/login", middleware.IPRateLimitMiddleware(loginConfig), adminHandler.Login)
  73. //// 注册API限流
  74. //registerConfig := limiterInstance.GetAPIConfig("register")
  75. //noAuthRouter.POST("/register", middleware.IPRateLimitMiddleware(registerConfig), userHandler.Register)
  76. // 创建IP白名单实例
  77. ipAllowlist := middleware.NewIPAllowlist(conf, logger)
  78. ipAllowlistMiddleware := ipAllowlist.IPAllowlistMiddleware()
  79. // 为GameShield相关接口添加IP白名单保护
  80. noAuthRouter.POST("/gameShield/add", ipAllowlistMiddleware, gameShieldHandler.SubmitGameShield)
  81. noAuthRouter.POST("/gameShield/getField", ipAllowlistMiddleware, gameShieldHandler.GetGameShieldField)
  82. noAuthRouter.POST("/gameShield/getKey", ipAllowlistMiddleware, gameShieldHandler.GetGameShieldKey)
  83. noAuthRouter.POST("/gameShield/edit", ipAllowlistMiddleware, gameShieldHandler.EditGameShield)
  84. noAuthRouter.POST("/gameShield/delete", ipAllowlistMiddleware, gameShieldHandler.DeleteGameShield)
  85. noAuthRouter.POST("/gameShield/getOnline", ipAllowlistMiddleware, gameShieldHandler.GetGameShieldOnlineList)
  86. noAuthRouter.POST("/gameShield/IsExistKey", gameShieldHandler.IsExistGameShieldKey)
  87. noAuthRouter.POST("/webForward/get", ipAllowlistMiddleware, webForwardingHandler.GetWebForwarding)
  88. noAuthRouter.POST("/webForward/getList", ipAllowlistMiddleware, webForwardingHandler.GetWebForwardingList)
  89. noAuthRouter.POST("/webForward/add", ipAllowlistMiddleware, webForwardingHandler.AddWebForwarding)
  90. noAuthRouter.POST("/webForward/edit", ipAllowlistMiddleware, webForwardingHandler.EditWebForwarding)
  91. noAuthRouter.POST("/webForward/delete", ipAllowlistMiddleware, webForwardingHandler.DeleteWebForwarding)
  92. noAuthRouter.POST("/webLimit/add", ipAllowlistMiddleware, weblimitHandler.AddWebLimit)
  93. noAuthRouter.POST("/webLimit/edit", ipAllowlistMiddleware, weblimitHandler.EditWebLimit)
  94. noAuthRouter.POST("/webLimit/delete", ipAllowlistMiddleware, weblimitHandler.DeleteWebLimit)
  95. noAuthRouter.POST("/tcpForward/add", ipAllowlistMiddleware, tcpForwardingHandler.AddTcpForwarding)
  96. noAuthRouter.POST("/tcpForward/get", ipAllowlistMiddleware, tcpForwardingHandler.GetTcpforwarding)
  97. noAuthRouter.POST("/tcpForward/getList", ipAllowlistMiddleware, tcpForwardingHandler.GetTcpForwardingList)
  98. noAuthRouter.POST("/tcpForward/edit", ipAllowlistMiddleware, tcpForwardingHandler.EditTcpForwarding)
  99. noAuthRouter.POST("/tcpForward/delete", ipAllowlistMiddleware, tcpForwardingHandler.DeleteTcpForwarding)
  100. noAuthRouter.POST("/udpForward/get", ipAllowlistMiddleware, udpForwardingHandler.GetUdpForWarding)
  101. noAuthRouter.POST("/udpForward/getList", ipAllowlistMiddleware, udpForwardingHandler.GetUdpForWardingList)
  102. noAuthRouter.POST("/udpForward/add", ipAllowlistMiddleware, udpForwardingHandler.AddUdpForWarding)
  103. noAuthRouter.POST("/udpForward/edit", ipAllowlistMiddleware, udpForwardingHandler.EditUdpForWarding)
  104. noAuthRouter.POST("/udpForward/delete", ipAllowlistMiddleware, udpForwardingHandler.DeleteUdpForWarding)
  105. noAuthRouter.POST("/tcpLimit/add", ipAllowlistMiddleware, tcpLimitHandler.AddTcpLimit)
  106. noAuthRouter.POST("/tcpLimit/edit", ipAllowlistMiddleware, tcpLimitHandler.EditTcpLimit)
  107. noAuthRouter.POST("/tcpLimit/delete", ipAllowlistMiddleware, tcpLimitHandler.DeleteTcpLimit)
  108. noAuthRouter.POST("/udpLimit/add", ipAllowlistMiddleware, udpLimitHandler.AddUdpLimit)
  109. noAuthRouter.POST("/udpLimit/edit", ipAllowlistMiddleware, udpLimitHandler.EditUdpLimit)
  110. noAuthRouter.POST("/udpLimit/delete", ipAllowlistMiddleware, udpLimitHandler.DeleteUdpLimit)
  111. noAuthRouter.POST("/gameShieldBackend/add", ipAllowlistMiddleware, gameShieldBackendHandler.AddGameShieldBackend)
  112. noAuthRouter.POST("/gameShieldBackend/edit", ipAllowlistMiddleware, gameShieldBackendHandler.EditGameShieldBackend)
  113. noAuthRouter.POST("/gameShieldBackend/delete", ipAllowlistMiddleware, gameShieldBackendHandler.DeleteGameShieldBackend)
  114. noAuthRouter.POST("/gameShieldBackend/replacementSourceMachineIp", ipAllowlistMiddleware, gameShieldBackendHandler.ReplacementSourceMachineIp)
  115. noAuthRouter.POST("/globalLimit/add", ipAllowlistMiddleware, globalLimitHandler.AddGlobalLimit)
  116. noAuthRouter.POST("/globalLimit/edit", ipAllowlistMiddleware, globalLimitHandler.EditGlobalLimit)
  117. noAuthRouter.POST("/globalLimit/delete", ipAllowlistMiddleware, globalLimitHandler.DeleteGlobalLimit)
  118. }
  119. // Non-strict permission routing group
  120. //noStrictAuthRouter := v1.Group("/").Use(middleware.NoStrictAuth(jwt, logger))
  121. //{
  122. // noStrictAuthRouter.GET("/user", userHandler.GetProfile)
  123. //}
  124. // Strict permission routing group
  125. strictAuthRouter := v1.Group("/").Use(middleware.StrictAuth(jwt, logger), middleware.AuthMiddleware(e))
  126. {
  127. strictAuthRouter.GET("/users", userHandler.GetUsers)
  128. strictAuthRouter.GET("/menus", adminHandler.GetMenus)
  129. strictAuthRouter.GET("/admin/menus", adminHandler.GetAdminMenus)
  130. strictAuthRouter.POST("/admin/menu", adminHandler.MenuCreate)
  131. strictAuthRouter.PUT("/admin/menu", adminHandler.MenuUpdate)
  132. strictAuthRouter.DELETE("/admin/menu", adminHandler.MenuDelete)
  133. strictAuthRouter.GET("/admin/users", adminHandler.GetAdminUsers)
  134. strictAuthRouter.GET("/admin/user", adminHandler.GetAdminUser)
  135. strictAuthRouter.PUT("/admin/user", adminHandler.AdminUserUpdate)
  136. strictAuthRouter.POST("/admin/user", adminHandler.AdminUserCreate)
  137. strictAuthRouter.DELETE("/admin/user", adminHandler.AdminUserDelete)
  138. strictAuthRouter.GET("/admin/user/permissions", adminHandler.GetUserPermissions)
  139. strictAuthRouter.GET("/admin/role/permissions", adminHandler.GetRolePermissions)
  140. strictAuthRouter.PUT("/admin/role/permission", adminHandler.UpdateRolePermission)
  141. strictAuthRouter.GET("/admin/roles", adminHandler.GetRoles)
  142. strictAuthRouter.POST("/admin/role", adminHandler.RoleCreate)
  143. strictAuthRouter.PUT("/admin/role", adminHandler.RoleUpdate)
  144. strictAuthRouter.DELETE("/admin/role", adminHandler.RoleDelete)
  145. strictAuthRouter.GET("/admin/apis", adminHandler.GetApis)
  146. strictAuthRouter.POST("/admin/api", adminHandler.ApiCreate)
  147. strictAuthRouter.PUT("/admin/api", adminHandler.ApiUpdate)
  148. strictAuthRouter.DELETE("/admin/api", adminHandler.ApiDelete)
  149. }
  150. }
  151. return s
  152. }