http.go 8.9 KB

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