Skip to content

Commit

Permalink
♻️ refactor: refactor routes #2
Browse files Browse the repository at this point in the history
  • Loading branch information
pnguyen215 committed May 11, 2024
1 parent 4f45aac commit 746323f
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 34 deletions.
34 changes: 17 additions & 17 deletions config/conf-params.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ curl:
max_interval: 10s
backoff_factor: 2
retry_on_status:
- 500
- 504
- 500
- 504
authentication:
enabled: false
type: basic
Expand All @@ -48,7 +48,7 @@ curl:
enabled: false
debug_mode: true
chat_id:
- 123456789
- 123456789
token: <token_here>
b_endpoint:
enabled: true
Expand All @@ -72,8 +72,8 @@ curl:
max_interval: 10s
backoff_factor: 2
retry_on_status:
- 500
- 504
- 500
- 504
authentication:
enabled: false
type: basic
Expand All @@ -84,7 +84,7 @@ curl:
enabled: false
debug_mode: true
chat_id:
- 123456789
- 123456789
token: <token_here>
retry:
enabled: true
Expand All @@ -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: <token_here>
# ################################
# 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
37 changes: 22 additions & 15 deletions internal/core/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
10 changes: 8 additions & 2 deletions internal/handlers/common_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand All @@ -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) {
Expand All @@ -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
}

0 comments on commit 746323f

Please sign in to comment.