http.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. ) *http.Server {
  32. gin.SetMode(gin.DebugMode)
  33. s := http.NewServer(
  34. gin.Default(),
  35. logger,
  36. http.WithServerHost(conf.GetString("http.host")),
  37. http.WithServerPort(conf.GetInt("http.port")),
  38. )
  39. // swagger doc
  40. docs.SwaggerInfo.BasePath = "/v1"
  41. s.GET("/swagger/*any", ginSwagger.WrapHandler(
  42. swaggerfiles.Handler,
  43. //ginSwagger.URL(fmt.Sprintf("http://localhost:%d/swagger/doc.json", conf.GetInt("app.http.port"))),
  44. ginSwagger.DefaultModelsExpandDepth(-1),
  45. ginSwagger.PersistAuthorization(true),
  46. ))
  47. s.Use(
  48. middleware.CORSMiddleware(),
  49. middleware.ResponseLogMiddleware(logger),
  50. middleware.RequestLogMiddleware(logger),
  51. //middleware.SignMiddleware(log),
  52. rateLimitMiddleware,
  53. )
  54. s.GET("/", func(ctx *gin.Context) {
  55. logger.WithContext(ctx).Info("hello")
  56. apiV1.HandleSuccess(ctx, map[string]interface{}{
  57. ":)": "Thank you for using nunu!",
  58. })
  59. })
  60. v1 := s.Group("/v1")
  61. {
  62. // No route group has permission
  63. noAuthRouter := v1.Group("/")
  64. {
  65. // 使用增强的Limiter.GetAPIConfig方法获取特定API的限流配置
  66. // 登录API限流
  67. loginConfig := limiterInstance.GetAPIConfig("login")
  68. noAuthRouter.POST("/login", middleware.IPRateLimitMiddleware(loginConfig), userHandler.Login)
  69. // 注册API限流
  70. registerConfig := limiterInstance.GetAPIConfig("register")
  71. noAuthRouter.POST("/register", middleware.IPRateLimitMiddleware(registerConfig), userHandler.Register)
  72. // 创建IP白名单实例
  73. ipAllowlist := middleware.NewIPAllowlist(conf, logger)
  74. ipAllowlistMiddleware := ipAllowlist.IPAllowlistMiddleware()
  75. // 为GameShield相关接口添加IP白名单保护
  76. noAuthRouter.POST("/gameShield/add", ipAllowlistMiddleware, gameShieldHandler.SubmitGameShield)
  77. noAuthRouter.POST("/gameShield/getField", ipAllowlistMiddleware, gameShieldHandler.GetGameShieldField)
  78. noAuthRouter.POST("/gameShield/getKey", ipAllowlistMiddleware, gameShieldHandler.GetGameShieldKey)
  79. noAuthRouter.POST("/gameShield/edit", ipAllowlistMiddleware, gameShieldHandler.EditGameShield)
  80. noAuthRouter.POST("/gameShield/delete", ipAllowlistMiddleware, gameShieldHandler.DeleteGameShield)
  81. noAuthRouter.POST("/gameShield/getOnline", ipAllowlistMiddleware, gameShieldHandler.GetGameShieldOnlineList)
  82. noAuthRouter.POST("/gameShield/IsExistKey", ipAllowlistMiddleware, gameShieldHandler.IsExistGameShieldKey)
  83. noAuthRouter.POST("/webForward/add", ipAllowlistMiddleware, webForwardingHandler.AddWebForwarding)
  84. noAuthRouter.POST("/webForward/edit", ipAllowlistMiddleware, webForwardingHandler.EditWebForwarding)
  85. noAuthRouter.POST("/webForward/delete", ipAllowlistMiddleware, webForwardingHandler.DeleteWebForwarding)
  86. noAuthRouter.POST("/webLimit/add", ipAllowlistMiddleware, weblimitHandler.AddWebLimit)
  87. noAuthRouter.POST("/webLimit/edit", ipAllowlistMiddleware, weblimitHandler.EditWebLimit)
  88. noAuthRouter.POST("/webLimit/delete", ipAllowlistMiddleware, weblimitHandler.DeleteWebLimit)
  89. noAuthRouter.POST("/tcpForward/add", ipAllowlistMiddleware, tcpForwardingHandler.AddTcpForwarding)
  90. noAuthRouter.POST("/tcpForward/edit", ipAllowlistMiddleware, tcpForwardingHandler.EditTcpForwarding)
  91. noAuthRouter.POST("/tcpForward/delete", ipAllowlistMiddleware, tcpForwardingHandler.DeleteTcpForwarding)
  92. noAuthRouter.POST("/udpForward/add", ipAllowlistMiddleware, udpForwardingHandler.AddUdpForWarding)
  93. noAuthRouter.POST("/udpForward/edit", ipAllowlistMiddleware, udpForwardingHandler.EditUdpForWarding)
  94. noAuthRouter.POST("/udpForward/delete", ipAllowlistMiddleware, udpForwardingHandler.DeleteUdpForWarding)
  95. noAuthRouter.POST("/tcpLimit/add", ipAllowlistMiddleware, tcpLimitHandler.AddTcpLimit)
  96. noAuthRouter.POST("/tcpLimit/edit", ipAllowlistMiddleware, tcpLimitHandler.EditTcpLimit)
  97. noAuthRouter.POST("/tcpLimit/delete", ipAllowlistMiddleware, tcpLimitHandler.DeleteTcpLimit)
  98. noAuthRouter.POST("/udpLimit/add", ipAllowlistMiddleware, udpLimitHandler.AddUdpLimit)
  99. noAuthRouter.POST("/udpLimit/edit", ipAllowlistMiddleware, udpLimitHandler.EditUdpLimit)
  100. noAuthRouter.POST("/udpLimit/delete", ipAllowlistMiddleware, udpLimitHandler.DeleteUdpLimit)
  101. noAuthRouter.POST("/gameShieldBackend/add", ipAllowlistMiddleware, gameShieldBackendHandler.AddGameShieldBackend)
  102. noAuthRouter.POST("/gameShieldBackend/edit", ipAllowlistMiddleware, gameShieldBackendHandler.EditGameShieldBackend)
  103. noAuthRouter.POST("/gameShieldBackend/delete", ipAllowlistMiddleware, gameShieldBackendHandler.DeleteGameShieldBackend)
  104. }
  105. // Non-strict permission routing group
  106. noStrictAuthRouter := v1.Group("/").Use(middleware.NoStrictAuth(jwt, logger))
  107. {
  108. noStrictAuthRouter.GET("/user", userHandler.GetProfile)
  109. }
  110. // Strict permission routing group
  111. strictAuthRouter := v1.Group("/").Use(middleware.StrictAuth(jwt, logger))
  112. {
  113. strictAuthRouter.PUT("/user", userHandler.UpdateProfile)
  114. }
  115. }
  116. return s
  117. }