http.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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/internal/service"
  10. "github.com/go-nunu/nunu-layout-advanced/pkg/jwt"
  11. "github.com/go-nunu/nunu-layout-advanced/pkg/limiter"
  12. "github.com/go-nunu/nunu-layout-advanced/pkg/log"
  13. "github.com/go-nunu/nunu-layout-advanced/pkg/server/http"
  14. "github.com/spf13/viper"
  15. swaggerfiles "github.com/swaggo/files"
  16. ginSwagger "github.com/swaggo/gin-swagger"
  17. )
  18. func NewHTTPServer(
  19. logger *log.Logger,
  20. conf *viper.Viper,
  21. jwt *jwt.JWT,
  22. e *casbin.SyncedEnforcer,
  23. limiterInstance *limiter.Limiter,
  24. rateLimitMiddleware gin.HandlerFunc,
  25. userHandler *handler.UserHandler,
  26. gameShieldHandler *handler.GameShieldHandler,
  27. gameShieldBackendHandler *handler.GameShieldBackendHandler,
  28. webForwardingHandler *handler.WebForwardingHandler,
  29. tcpForwardingHandler *handler.TcpforwardingHandler,
  30. udpForwardingHandler *handler.UdpForWardingHandler,
  31. globalLimitHandler *handler.GlobalLimitHandler,
  32. adminHandler *handler.AdminHandler,
  33. gatewayHandler *handler.GatewayGroupHandler,
  34. gatewayIpHandler *handler.GateWayGroupIpHandler,
  35. allowAnddenyHandler *handler.AllowAndDenyIpHandler,
  36. ccHandler *handler.CcHandler,
  37. logService service.LogService,
  38. ) *http.Server {
  39. gin.SetMode(gin.DebugMode)
  40. s := http.NewServer(
  41. gin.Default(),
  42. logger,
  43. http.WithServerHost(conf.GetString("http.host")),
  44. http.WithServerPort(conf.GetInt("http.port")),
  45. )
  46. // swagger doc
  47. docs.SwaggerInfo.BasePath = "/v1"
  48. s.GET("/swagger/*any", ginSwagger.WrapHandler(
  49. swaggerfiles.Handler,
  50. //ginSwagger.URL(fmt.Sprintf("http://localhost:%d/swagger/doc.json", conf.GetInt("app.http.port"))),
  51. ginSwagger.DefaultModelsExpandDepth(-1),
  52. ginSwagger.PersistAuthorization(true),
  53. ))
  54. s.Use(
  55. middleware.CORSMiddleware(),
  56. middleware.ResponseLogMiddleware(logger),
  57. middleware.RequestLogMiddleware(logger),
  58. middleware.OperationLogMiddleware(logService),
  59. //middleware.SignMiddleware(log),
  60. rateLimitMiddleware,
  61. )
  62. s.GET("/", func(ctx *gin.Context) {
  63. logger.WithContext(ctx).Info("hello")
  64. apiV1.HandleSuccess(ctx, map[string]interface{}{
  65. ":)": "Thank you for using nunu!",
  66. })
  67. })
  68. v1 := s.Group("/v1")
  69. {
  70. // No route group has permission
  71. noAuthRouter := v1.Group("/")
  72. {
  73. // 使用增强的Limiter.GetAPIConfig方法获取特定API的限流配置
  74. // 登录API限流
  75. loginConfig := limiterInstance.GetAPIConfig("login")
  76. noAuthRouter.POST("/login", middleware.IPRateLimitMiddleware(loginConfig), adminHandler.Login)
  77. //// 注册API限流
  78. //registerConfig := limiterInstance.GetAPIConfig("register")
  79. //noAuthRouter.POST("/register", middleware.IPRateLimitMiddleware(registerConfig), userHandler.Register)
  80. // 创建IP白名单实例
  81. ipAllowlist := middleware.NewIPAllowlist(conf, logger)
  82. ipAllowlistMiddleware := ipAllowlist.IPAllowlistMiddleware()
  83. // 为GameShield相关接口添加IP白名单保护
  84. noAuthRouter.POST("/gameShield/add", ipAllowlistMiddleware, gameShieldHandler.SubmitGameShield)
  85. noAuthRouter.POST("/gameShield/getField", ipAllowlistMiddleware, gameShieldHandler.GetGameShieldField)
  86. noAuthRouter.POST("/gameShield/getKey", ipAllowlistMiddleware, gameShieldHandler.GetGameShieldKey)
  87. noAuthRouter.POST("/gameShield/edit", ipAllowlistMiddleware, gameShieldHandler.EditGameShield)
  88. noAuthRouter.POST("/gameShield/delete", ipAllowlistMiddleware, gameShieldHandler.DeleteGameShield)
  89. noAuthRouter.POST("/gameShield/getOnline", ipAllowlistMiddleware, gameShieldHandler.GetGameShieldOnlineList)
  90. noAuthRouter.POST("/gameShield/IsExistKey", gameShieldHandler.IsExistGameShieldKey)
  91. noAuthRouter.POST("/webForward/get", ipAllowlistMiddleware, webForwardingHandler.GetWebForwarding)
  92. noAuthRouter.POST("/webForward/getList", ipAllowlistMiddleware, webForwardingHandler.GetWebForwardingList)
  93. noAuthRouter.POST("/webForward/add", ipAllowlistMiddleware, webForwardingHandler.AddWebForwarding)
  94. noAuthRouter.POST("/webForward/edit", ipAllowlistMiddleware, webForwardingHandler.EditWebForwarding)
  95. noAuthRouter.POST("/webForward/delete", ipAllowlistMiddleware, webForwardingHandler.DeleteWebForwarding)
  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("/gameShieldBackend/add", ipAllowlistMiddleware, gameShieldBackendHandler.AddGameShieldBackend)
  107. noAuthRouter.POST("/gameShieldBackend/edit", ipAllowlistMiddleware, gameShieldBackendHandler.EditGameShieldBackend)
  108. noAuthRouter.POST("/gameShieldBackend/delete", ipAllowlistMiddleware, gameShieldBackendHandler.DeleteGameShieldBackend)
  109. noAuthRouter.POST("/gameShieldBackend/replacementSourceMachineIp", ipAllowlistMiddleware, gameShieldBackendHandler.ReplacementSourceMachineIp)
  110. noAuthRouter.POST("/globalLimit/add", ipAllowlistMiddleware, globalLimitHandler.AddGlobalLimit)
  111. noAuthRouter.POST("/globalLimit/edit", ipAllowlistMiddleware, globalLimitHandler.EditGlobalLimit)
  112. noAuthRouter.POST("/globalLimit/delete", ipAllowlistMiddleware, globalLimitHandler.DeleteGlobalLimit)
  113. noAuthRouter.POST("/allowAndDeny/get", ipAllowlistMiddleware, allowAnddenyHandler.GetAllowAndDenyIp)
  114. noAuthRouter.POST("/allowAndDeny/getList", ipAllowlistMiddleware, allowAnddenyHandler.GetAllowAndDenyIpList)
  115. noAuthRouter.POST("/allowAndDeny/add", ipAllowlistMiddleware, allowAnddenyHandler.AddAllowAndDenyIp)
  116. noAuthRouter.POST("/allowAndDeny/edit", ipAllowlistMiddleware, allowAnddenyHandler.EditAllowAndDenyIp)
  117. noAuthRouter.POST("/allowAndDeny/delete", ipAllowlistMiddleware, allowAnddenyHandler.DeleteAllowAndDenyIp)
  118. noAuthRouter.POST("/cc/getList", ipAllowlistMiddleware, ccHandler.GetCcList)
  119. noAuthRouter.POST("/cc/editState", ipAllowlistMiddleware, ccHandler.EditCcState)
  120. }
  121. // Non-strict permission routing group
  122. //noStrictAuthRouter := v1.Group("/").Use(middleware.NoStrictAuth(jwt, logger))
  123. //{
  124. // noStrictAuthRouter.GET("/user", userHandler.GetProfile)
  125. //}
  126. // Strict permission routing group
  127. strictAuthRouter := v1.Group("/").Use(middleware.StrictAuth(jwt, logger), middleware.AuthMiddleware(e))
  128. {
  129. strictAuthRouter.GET("/user", userHandler.GetUsers)
  130. strictAuthRouter.GET("/menus", adminHandler.GetMenus)
  131. strictAuthRouter.GET("/admin/menus", adminHandler.GetAdminMenus)
  132. strictAuthRouter.POST("/admin/menu", adminHandler.MenuCreate)
  133. strictAuthRouter.PUT("/admin/menu", adminHandler.MenuUpdate)
  134. strictAuthRouter.DELETE("/admin/menu", adminHandler.MenuDelete)
  135. strictAuthRouter.GET("/admin/users", adminHandler.GetAdminUsers)
  136. strictAuthRouter.GET("/admin/user", adminHandler.GetAdminUser)
  137. strictAuthRouter.PUT("/admin/user", adminHandler.AdminUserUpdate)
  138. strictAuthRouter.POST("/admin/user", adminHandler.AdminUserCreate)
  139. strictAuthRouter.DELETE("/admin/user", adminHandler.AdminUserDelete)
  140. strictAuthRouter.GET("/admin/user/permissions", adminHandler.GetUserPermissions)
  141. strictAuthRouter.GET("/admin/role/permissions", adminHandler.GetRolePermissions)
  142. strictAuthRouter.PUT("/admin/role/permission", adminHandler.UpdateRolePermission)
  143. strictAuthRouter.GET("/admin/roles", adminHandler.GetRoles)
  144. strictAuthRouter.POST("/admin/role", adminHandler.RoleCreate)
  145. strictAuthRouter.PUT("/admin/role", adminHandler.RoleUpdate)
  146. strictAuthRouter.DELETE("/admin/role", adminHandler.RoleDelete)
  147. strictAuthRouter.GET("/admin/apis", adminHandler.GetApis)
  148. strictAuthRouter.POST("/admin/api", adminHandler.ApiCreate)
  149. strictAuthRouter.PUT("/admin/api", adminHandler.ApiUpdate)
  150. strictAuthRouter.DELETE("/admin/api", adminHandler.ApiDelete)
  151. strictAuthRouter.GET("/gateway/list", gatewayHandler.GetGatewayGroupList)
  152. strictAuthRouter.POST("/gateway/add", gatewayHandler.AddGatewayGroup)
  153. strictAuthRouter.PUT("/gateway/edit", gatewayHandler.EditGatewayGroup)
  154. strictAuthRouter.DELETE("/gateway/del", gatewayHandler.DeleteGatewayGroup)
  155. strictAuthRouter.GET("/gatewayIp/get", gatewayIpHandler.GetGateWayGroupIpList)
  156. strictAuthRouter.POST("/gatewayIp/add", gatewayIpHandler.AddGateWayGroupIp)
  157. strictAuthRouter.PUT("/gatewayIp/edit", gatewayIpHandler.EditGateWayGroupIp)
  158. strictAuthRouter.DELETE("/gatewayIp/del", gatewayIpHandler.DeleteGateWayGroupIp)
  159. }
  160. }
  161. return s
  162. }