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()
}
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() {}

@ -16,7 +16,6 @@ import (
var (
_ api.Relax = (*relaxSrv)(nil)
_ api.RelaxChain = (*relaxChain)(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{}
}

@ -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()

@ -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"`
}

Loading…
Cancel
Save