Skip to content

Commit 746323f

Browse files
committed
♻️ refactor: refactor routes #2
1 parent 4f45aac commit 746323f

File tree

3 files changed

+47
-34
lines changed

3 files changed

+47
-34
lines changed

config/conf-params.yaml

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ curl:
3636
max_interval: 10s
3737
backoff_factor: 2
3838
retry_on_status:
39-
- 500
40-
- 504
39+
- 500
40+
- 504
4141
authentication:
4242
enabled: false
4343
type: basic
@@ -48,7 +48,7 @@ curl:
4848
enabled: false
4949
debug_mode: true
5050
chat_id:
51-
- 123456789
51+
- 123456789
5252
token: <token_here>
5353
b_endpoint:
5454
enabled: true
@@ -72,8 +72,8 @@ curl:
7272
max_interval: 10s
7373
backoff_factor: 2
7474
retry_on_status:
75-
- 500
76-
- 504
75+
- 500
76+
- 504
7777
authentication:
7878
enabled: false
7979
type: basic
@@ -84,7 +84,7 @@ curl:
8484
enabled: false
8585
debug_mode: true
8686
chat_id:
87-
- 123456789
87+
- 123456789
8888
token: <token_here>
8989
retry:
9090
enabled: true
@@ -93,24 +93,24 @@ curl:
9393
max_interval: 10s
9494
backoff_factor: 2
9595
retry_on_status:
96-
- 500
97-
- 504
96+
- 500
97+
- 504
9898
telegram:
9999
enabled: true
100100
debug_mode: true
101101
chat_id:
102-
- 123456789
102+
- 123456789
103103
token: <token_here>
104104
# ################################
105105
# Rate Limit Seekers Config
106106
# 2023-11-25 12:02:54
107107
# ################################
108108
rate-limit-seekers:
109-
- key: "psql_rate"
110-
usable_default: false
111-
config:
112-
enabled: false
113-
rate: 2
114-
max_burst: 1
115-
option:
116-
max_retries: 2
109+
- key: "psql_rate"
110+
usable_default: false
111+
config:
112+
enabled: false
113+
rate: 2
114+
max_burst: 1
115+
option:
116+
max_retries: 2

internal/core/routes.go

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,31 @@ import (
66
ginSwagger "github.com/swaggo/gin-swagger"
77
)
88

9-
func (c *CoreCommand) routes(core *gin.Engine) {
10-
core.GET("/api/v1/swagger/index.html", ginSwagger.WrapHandler(
9+
func (c *CoreCommand) routes(e *gin.Engine) {
10+
c.shared(e)
11+
c.protected(e)
12+
}
13+
14+
// Collection of authenticated endpoints
15+
func (c *CoreCommand) protected(e *gin.Engine) {
16+
v1 := e.Group("/api/v1")
17+
v1.GET("/swagger/index.html", ginSwagger.WrapHandler(
1118
swaggerFiles.Handler,
1219
ginSwagger.DefaultModelsExpandDepth(-1),
1320
))
14-
v1 := core.Group("/api/v1")
15-
{
16-
v1.GET("/common/psql-status",
21+
22+
c.handlers.commonHandler.Router(v1.Group("/common"), c.handlers.middlewares)
23+
}
24+
25+
// Collection of shared/public endpoints
26+
func (c *CoreCommand) shared(e *gin.Engine) {
27+
v1 := e.Group("/api/v1/shared")
28+
29+
c.handlers.commonHandler.Router(
30+
v1.Group("/common",
1731
c.handlers.middlewares.RequestMiddleWare(),
18-
c.handlers.middlewares.NoopMiddleWare(),
19-
c.handlers.middlewares.RateLimitMiddleWare("psql_rate"),
2032
c.handlers.middlewares.NetMiddleware(),
21-
c.handlers.commonHandler.OnPsqlStatus)
22-
v1.GET("/common/consumer", // endpoint websocket: ws://127.0.0.1:8081/api/v1/common/consumer
23-
c.handlers.middlewares.RequestMiddleWare(),
24-
c.handlers.commonHandler.OnSubscribe)
25-
v1.POST("/common/producer", // endpoint produce message to websocket
26-
c.handlers.middlewares.RequestMiddleWare(),
27-
c.handlers.commonHandler.OnProduce)
28-
}
33+
c.handlers.middlewares.RateLimitMiddleWare("psql_rate"),
34+
),
35+
c.handlers.middlewares)
2936
}

internal/handlers/common_handler.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"time"
66

77
"github.com/gin-gonic/gin"
8+
"github.com/sivaosorg/gocell/internal/middlewares"
89
"github.com/sivaosorg/gocell/internal/service"
910
"github.com/sivaosorg/govm/entity"
1011
"github.com/sivaosorg/govm/wsconnx"
@@ -25,6 +26,13 @@ func NewCommonHandler(commonSvc service.CommonService) *CommonHandler {
2526
return h
2627
}
2728

29+
func (c *CommonHandler) Router(r *gin.RouterGroup, middlewares *middlewares.MiddlewareManager) *gin.RouterGroup {
30+
r.GET("/psql-status", c.OnPsqlStatus) // endpoint: http://127.0.0.1:8081/api/v1/common/psql-status
31+
r.GET("/consumer", c.OnSubscribe) // endpoint: ws://127.0.0.1:8081/api/v1/common/consumer
32+
r.POST("/producer", c.OnProduce) // endpoint: http://127.0.0.1:8081/api/v1/common/producer
33+
return r
34+
}
35+
2836
func (h *CommonHandler) OnPsqlStatus(ctx *gin.Context) {
2937
data := h.commonSvc.GetPsqlStatus()
3038
response := entity.NewResponseEntity().SetData(data)
@@ -34,7 +42,6 @@ func (h *CommonHandler) OnPsqlStatus(ctx *gin.Context) {
3442
response.SetStatusCode(http.StatusInternalServerError)
3543
}
3644
ctx.JSON(response.StatusCode, response)
37-
return
3845
}
3946

4047
func (h *CommonHandler) OnSubscribe(ctx *gin.Context) {
@@ -53,5 +60,4 @@ func (h *CommonHandler) OnProduce(ctx *gin.Context) {
5360
go h.wsSvc.BroadcastMessage(message)
5461
response.SetStatusCode(http.StatusOK).SetMessage("Message sent successfully").SetData(message)
5562
ctx.JSON(response.StatusCode, response)
56-
return
5763
}

0 commit comments

Comments
 (0)