From 3581d9bc2f2d0f4816d036d5106bef0306ce4a86 Mon Sep 17 00:00:00 2001 From: Michael Li Date: Wed, 30 Aug 2023 04:00:40 +0800 Subject: [PATCH] optimize relax web api define in mirc/web/v1 --- auto/api/v1/relax.go | 27 +++------------------------ internal/servants/web/relax.go | 15 +++------------ internal/servants/web/web.go | 2 +- mirc/web/v1/relax.go | 2 +- 4 files changed, 8 insertions(+), 38 deletions(-) diff --git a/auto/api/v1/relax.go b/auto/api/v1/relax.go index c1e64ab4..0f394cea 100644 --- a/auto/api/v1/relax.go +++ b/auto/api/v1/relax.go @@ -23,27 +23,15 @@ type Relax interface { mustEmbedUnimplementedRelaxServant() } -type RelaxChain interface { - ChainGetUnreadMsgCount() gin.HandlersChain - - mustEmbedUnimplementedRelaxChain() -} - // RegisterRelaxServant register Relax servant to gin -func RegisterRelaxServant(e *gin.Engine, s Relax, m ...RelaxChain) { - var cc RelaxChain - if len(m) > 0 { - cc = m[0] - } else { - cc = &UnimplementedRelaxChain{} - } +func RegisterRelaxServant(e *gin.Engine, s Relax) { router := e.Group("v1") // use chain for router middlewares := s.Chain() router.Use(middlewares...) // register routes info to router - router.Handle("GET", "/user/msgcount/unread", append(cc.ChainGetUnreadMsgCount(), func(c *gin.Context) { + router.Handle("GET", "/user/msgcount/unread", func(c *gin.Context) { select { case <-c.Request.Context().Done(): return @@ -56,7 +44,7 @@ func RegisterRelaxServant(e *gin.Engine, s Relax, m ...RelaxChain) { } resp, err := s.GetUnreadMsgCount(req) s.Render(c, resp, err) - })...) + }) } // UnimplementedRelaxServant can be embedded to have forward compatible implementations. @@ -71,12 +59,3 @@ func (UnimplementedRelaxServant) GetUnreadMsgCount(req *web.GetUnreadMsgCountReq } func (UnimplementedRelaxServant) mustEmbedUnimplementedRelaxServant() {} - -// UnimplementedRelaxChain can be embedded to have forward compatible implementations. -type UnimplementedRelaxChain struct{} - -func (b *UnimplementedRelaxChain) ChainGetUnreadMsgCount() gin.HandlersChain { - return nil -} - -func (b *UnimplementedRelaxChain) mustEmbedUnimplementedRelaxChain() {} diff --git a/internal/servants/web/relax.go b/internal/servants/web/relax.go index ddd7f078..bef7da90 100644 --- a/internal/servants/web/relax.go +++ b/internal/servants/web/relax.go @@ -15,8 +15,7 @@ import ( ) var ( - _ api.Relax = (*relaxSrv)(nil) - _ api.RelaxChain = (*relaxChain)(nil) + _ api.Relax = (*relaxSrv)(nil) ) type relaxSrv struct { @@ -24,8 +23,8 @@ type relaxSrv struct { *base.DaoServant } -type relaxChain struct { - api.UnimplementedRelaxChain +func (s *relaxSrv) Chain() gin.HandlersChain { + return gin.HandlersChain{chain.JwtSurely()} } func (s *relaxSrv) GetUnreadMsgCount(req *web.GetUnreadMsgCountReq) (*web.GetUnreadMsgCountResp, mir.Error) { @@ -38,16 +37,8 @@ func (s *relaxSrv) GetUnreadMsgCount(req *web.GetUnreadMsgCountReq) (*web.GetUnr }, nil } -func (*relaxChain) ChainGetUnreadMsgCount() gin.HandlersChain { - return gin.HandlersChain{chain.JwtSurely()} -} - func newRelaxSrv(s *base.DaoServant) api.Relax { return &relaxSrv{ DaoServant: s, } } - -func newRelaxChain() api.RelaxChain { - return &relaxChain{} -} diff --git a/internal/servants/web/web.go b/internal/servants/web/web.go index a508d02a..9c955310 100644 --- a/internal/servants/web/web.go +++ b/internal/servants/web/web.go @@ -29,12 +29,12 @@ func RouteWeb(e *gin.Engine) { // aways register servants api.RegisterAdminServant(e, newAdminSrv(ds)) api.RegisterCoreServant(e, newCoreSrv(ds, oss)) + api.RegisterRelaxServant(e, newRelaxSrv(ds)) api.RegisterLooseServant(e, newLooseSrv(ds)) api.RegisterPrivServant(e, newPrivSrv(ds, oss)) api.RegisterPubServant(e, newPubSrv(ds)) api.RegisterFollowshipServant(e, newFollowshipSrv(ds)) api.RegisterFriendshipServant(e, newFriendshipSrv(ds)) - api.RegisterRelaxServant(e, newRelaxSrv(ds), newRelaxChain()) // regster servants if needed by configure cfg.Be("Alipay", func() { client := conf.MustAlipayClient() diff --git a/mirc/web/v1/relax.go b/mirc/web/v1/relax.go index 3bdfdb91..af4bbe83 100644 --- a/mirc/web/v1/relax.go +++ b/mirc/web/v1/relax.go @@ -16,5 +16,5 @@ type Relax struct { Group `mir:"v1"` // GetUnreadMsgCount 获取当前用户未读消息数量 - GetUnreadMsgCount func(Get, Chain, web.GetUnreadMsgCountReq) web.GetUnreadMsgCountResp `mir:"/user/msgcount/unread"` + GetUnreadMsgCount func(Get, web.GetUnreadMsgCountReq) web.GetUnreadMsgCountResp `mir:"/user/msgcount/unread"` }