optimize relax web api define in mirc/web/v1

pull/378/head
Michael Li 1 year ago
parent 537f64090e
commit 3581d9bc2f
No known key found for this signature in database

@ -23,27 +23,15 @@ type Relax interface {
mustEmbedUnimplementedRelaxServant() mustEmbedUnimplementedRelaxServant()
} }
type RelaxChain interface {
ChainGetUnreadMsgCount() gin.HandlersChain
mustEmbedUnimplementedRelaxChain()
}
// RegisterRelaxServant register Relax servant to gin // RegisterRelaxServant register Relax servant to gin
func RegisterRelaxServant(e *gin.Engine, s Relax, m ...RelaxChain) { func RegisterRelaxServant(e *gin.Engine, s Relax) {
var cc RelaxChain
if len(m) > 0 {
cc = m[0]
} else {
cc = &UnimplementedRelaxChain{}
}
router := e.Group("v1") router := e.Group("v1")
// use chain for router // use chain for router
middlewares := s.Chain() middlewares := s.Chain()
router.Use(middlewares...) router.Use(middlewares...)
// register routes info to router // 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 { select {
case <-c.Request.Context().Done(): case <-c.Request.Context().Done():
return return
@ -56,7 +44,7 @@ func RegisterRelaxServant(e *gin.Engine, s Relax, m ...RelaxChain) {
} }
resp, err := s.GetUnreadMsgCount(req) resp, err := s.GetUnreadMsgCount(req)
s.Render(c, resp, err) s.Render(c, resp, err)
})...) })
} }
// UnimplementedRelaxServant can be embedded to have forward compatible implementations. // UnimplementedRelaxServant can be embedded to have forward compatible implementations.
@ -71,12 +59,3 @@ func (UnimplementedRelaxServant) GetUnreadMsgCount(req *web.GetUnreadMsgCountReq
} }
func (UnimplementedRelaxServant) mustEmbedUnimplementedRelaxServant() {} 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() {}

@ -16,7 +16,6 @@ import (
var ( var (
_ api.Relax = (*relaxSrv)(nil) _ api.Relax = (*relaxSrv)(nil)
_ api.RelaxChain = (*relaxChain)(nil)
) )
type relaxSrv struct { type relaxSrv struct {
@ -24,8 +23,8 @@ type relaxSrv struct {
*base.DaoServant *base.DaoServant
} }
type relaxChain struct { func (s *relaxSrv) Chain() gin.HandlersChain {
api.UnimplementedRelaxChain return gin.HandlersChain{chain.JwtSurely()}
} }
func (s *relaxSrv) GetUnreadMsgCount(req *web.GetUnreadMsgCountReq) (*web.GetUnreadMsgCountResp, mir.Error) { 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 }, nil
} }
func (*relaxChain) ChainGetUnreadMsgCount() gin.HandlersChain {
return gin.HandlersChain{chain.JwtSurely()}
}
func newRelaxSrv(s *base.DaoServant) api.Relax { func newRelaxSrv(s *base.DaoServant) api.Relax {
return &relaxSrv{ return &relaxSrv{
DaoServant: s, DaoServant: s,
} }
} }
func newRelaxChain() api.RelaxChain {
return &relaxChain{}
}

@ -29,12 +29,12 @@ func RouteWeb(e *gin.Engine) {
// aways register servants // aways register servants
api.RegisterAdminServant(e, newAdminSrv(ds)) api.RegisterAdminServant(e, newAdminSrv(ds))
api.RegisterCoreServant(e, newCoreSrv(ds, oss)) api.RegisterCoreServant(e, newCoreSrv(ds, oss))
api.RegisterRelaxServant(e, newRelaxSrv(ds))
api.RegisterLooseServant(e, newLooseSrv(ds)) api.RegisterLooseServant(e, newLooseSrv(ds))
api.RegisterPrivServant(e, newPrivSrv(ds, oss)) api.RegisterPrivServant(e, newPrivSrv(ds, oss))
api.RegisterPubServant(e, newPubSrv(ds)) api.RegisterPubServant(e, newPubSrv(ds))
api.RegisterFollowshipServant(e, newFollowshipSrv(ds)) api.RegisterFollowshipServant(e, newFollowshipSrv(ds))
api.RegisterFriendshipServant(e, newFriendshipSrv(ds)) api.RegisterFriendshipServant(e, newFriendshipSrv(ds))
api.RegisterRelaxServant(e, newRelaxSrv(ds), newRelaxChain())
// regster servants if needed by configure // regster servants if needed by configure
cfg.Be("Alipay", func() { cfg.Be("Alipay", func() {
client := conf.MustAlipayClient() client := conf.MustAlipayClient()

@ -16,5 +16,5 @@ type Relax struct {
Group `mir:"v1"` Group `mir:"v1"`
// GetUnreadMsgCount 获取当前用户未读消息数量 // GetUnreadMsgCount 获取当前用户未读消息数量
GetUnreadMsgCount func(Get, Chain, web.GetUnreadMsgCountReq) web.GetUnreadMsgCountResp `mir:"/user/msgcount/unread"` GetUnreadMsgCount func(Get, web.GetUnreadMsgCountReq) web.GetUnreadMsgCountResp `mir:"/user/msgcount/unread"`
} }

Loading…
Cancel
Save