From 746323f6a380691d96951aa23df7b46679f22918 Mon Sep 17 00:00:00 2001 From: arisnguyenit97 Date: Sun, 12 May 2024 00:26:50 +0700 Subject: [PATCH] :recycle: refactor: refactor routes #2 --- config/conf-params.yaml | 34 +++++++++++++------------- internal/core/routes.go | 37 +++++++++++++++++------------ internal/handlers/common_handler.go | 10 ++++++-- 3 files changed, 47 insertions(+), 34 deletions(-) diff --git a/config/conf-params.yaml b/config/conf-params.yaml index 0f77785..6955119 100644 --- a/config/conf-params.yaml +++ b/config/conf-params.yaml @@ -36,8 +36,8 @@ curl: max_interval: 10s backoff_factor: 2 retry_on_status: - - 500 - - 504 + - 500 + - 504 authentication: enabled: false type: basic @@ -48,7 +48,7 @@ curl: enabled: false debug_mode: true chat_id: - - 123456789 + - 123456789 token: b_endpoint: enabled: true @@ -72,8 +72,8 @@ curl: max_interval: 10s backoff_factor: 2 retry_on_status: - - 500 - - 504 + - 500 + - 504 authentication: enabled: false type: basic @@ -84,7 +84,7 @@ curl: enabled: false debug_mode: true chat_id: - - 123456789 + - 123456789 token: retry: enabled: true @@ -93,24 +93,24 @@ curl: max_interval: 10s backoff_factor: 2 retry_on_status: - - 500 - - 504 + - 500 + - 504 telegram: enabled: true debug_mode: true chat_id: - - 123456789 + - 123456789 token: # ################################ # Rate Limit Seekers Config # 2023-11-25 12:02:54 # ################################ rate-limit-seekers: -- key: "psql_rate" - usable_default: false - config: - enabled: false - rate: 2 - max_burst: 1 - option: - max_retries: 2 + - key: "psql_rate" + usable_default: false + config: + enabled: false + rate: 2 + max_burst: 1 + option: + max_retries: 2 diff --git a/internal/core/routes.go b/internal/core/routes.go index 5216b00..95cddd2 100644 --- a/internal/core/routes.go +++ b/internal/core/routes.go @@ -6,24 +6,31 @@ import ( ginSwagger "github.com/swaggo/gin-swagger" ) -func (c *CoreCommand) routes(core *gin.Engine) { - core.GET("/api/v1/swagger/index.html", ginSwagger.WrapHandler( +func (c *CoreCommand) routes(e *gin.Engine) { + c.shared(e) + c.protected(e) +} + +// Collection of authenticated endpoints +func (c *CoreCommand) protected(e *gin.Engine) { + v1 := e.Group("/api/v1") + v1.GET("/swagger/index.html", ginSwagger.WrapHandler( swaggerFiles.Handler, ginSwagger.DefaultModelsExpandDepth(-1), )) - v1 := core.Group("/api/v1") - { - v1.GET("/common/psql-status", + + c.handlers.commonHandler.Router(v1.Group("/common"), c.handlers.middlewares) +} + +// Collection of shared/public endpoints +func (c *CoreCommand) shared(e *gin.Engine) { + v1 := e.Group("/api/v1/shared") + + c.handlers.commonHandler.Router( + v1.Group("/common", c.handlers.middlewares.RequestMiddleWare(), - c.handlers.middlewares.NoopMiddleWare(), - c.handlers.middlewares.RateLimitMiddleWare("psql_rate"), c.handlers.middlewares.NetMiddleware(), - c.handlers.commonHandler.OnPsqlStatus) - v1.GET("/common/consumer", // endpoint websocket: ws://127.0.0.1:8081/api/v1/common/consumer - c.handlers.middlewares.RequestMiddleWare(), - c.handlers.commonHandler.OnSubscribe) - v1.POST("/common/producer", // endpoint produce message to websocket - c.handlers.middlewares.RequestMiddleWare(), - c.handlers.commonHandler.OnProduce) - } + c.handlers.middlewares.RateLimitMiddleWare("psql_rate"), + ), + c.handlers.middlewares) } diff --git a/internal/handlers/common_handler.go b/internal/handlers/common_handler.go index bbeac55..dc16bc9 100644 --- a/internal/handlers/common_handler.go +++ b/internal/handlers/common_handler.go @@ -5,6 +5,7 @@ import ( "time" "github.com/gin-gonic/gin" + "github.com/sivaosorg/gocell/internal/middlewares" "github.com/sivaosorg/gocell/internal/service" "github.com/sivaosorg/govm/entity" "github.com/sivaosorg/govm/wsconnx" @@ -25,6 +26,13 @@ func NewCommonHandler(commonSvc service.CommonService) *CommonHandler { return h } +func (c *CommonHandler) Router(r *gin.RouterGroup, middlewares *middlewares.MiddlewareManager) *gin.RouterGroup { + r.GET("/psql-status", c.OnPsqlStatus) // endpoint: http://127.0.0.1:8081/api/v1/common/psql-status + r.GET("/consumer", c.OnSubscribe) // endpoint: ws://127.0.0.1:8081/api/v1/common/consumer + r.POST("/producer", c.OnProduce) // endpoint: http://127.0.0.1:8081/api/v1/common/producer + return r +} + func (h *CommonHandler) OnPsqlStatus(ctx *gin.Context) { data := h.commonSvc.GetPsqlStatus() response := entity.NewResponseEntity().SetData(data) @@ -34,7 +42,6 @@ func (h *CommonHandler) OnPsqlStatus(ctx *gin.Context) { response.SetStatusCode(http.StatusInternalServerError) } ctx.JSON(response.StatusCode, response) - return } func (h *CommonHandler) OnSubscribe(ctx *gin.Context) { @@ -53,5 +60,4 @@ func (h *CommonHandler) OnProduce(ctx *gin.Context) { go h.wsSvc.BroadcastMessage(message) response.SetStatusCode(http.StatusOK).SetMessage("Message sent successfully").SetData(message) ctx.JSON(response.StatusCode, response) - return }