http.go 6.7 KB

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