2021-03-24 17:18:39 +08:00
|
|
|
|
package ctx
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
2021-04-16 15:10:07 +08:00
|
|
|
|
"mayfly-go/base/ginx"
|
|
|
|
|
|
"mayfly-go/base/model"
|
2021-03-24 17:18:39 +08:00
|
|
|
|
"net/http"
|
2021-04-21 10:22:09 +08:00
|
|
|
|
"time"
|
2021-04-16 15:10:07 +08:00
|
|
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2021-03-24 17:18:39 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
2021-04-16 15:10:07 +08:00
|
|
|
|
// 处理函数
|
|
|
|
|
|
type HandlerFunc func(*ReqCtx)
|
|
|
|
|
|
|
2021-03-24 17:18:39 +08:00
|
|
|
|
type ReqCtx struct {
|
2021-04-16 15:10:07 +08:00
|
|
|
|
Req *http.Request // http request
|
|
|
|
|
|
GinCtx *gin.Context // gin context
|
|
|
|
|
|
|
|
|
|
|
|
NeedToken bool // 是否需要token
|
|
|
|
|
|
LoginAccount *model.LoginAccount // 登录账号信息
|
2021-03-24 17:18:39 +08:00
|
|
|
|
|
2021-04-16 15:10:07 +08:00
|
|
|
|
LogInfo *LogInfo // 日志相关信息
|
|
|
|
|
|
ReqParam interface{} // 请求参数,主要用于记录日志
|
|
|
|
|
|
ResData interface{} // 响应结果
|
|
|
|
|
|
err interface{} // 请求错误
|
2021-04-21 10:22:09 +08:00
|
|
|
|
timed int64 // 执行时间
|
2021-03-24 17:18:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-04-16 15:10:07 +08:00
|
|
|
|
func (rc *ReqCtx) Handle(handler HandlerFunc) {
|
|
|
|
|
|
ginCtx := rc.GinCtx
|
|
|
|
|
|
defer func() {
|
|
|
|
|
|
if err := recover(); err != nil {
|
|
|
|
|
|
rc.err = err
|
|
|
|
|
|
ginx.ErrorRes(ginCtx, err)
|
|
|
|
|
|
}
|
|
|
|
|
|
// 应用所有请求后置处理器
|
|
|
|
|
|
ApplyAfterHandler(rc)
|
|
|
|
|
|
}()
|
|
|
|
|
|
if ginCtx == nil {
|
|
|
|
|
|
panic("ginContext == nil")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
rc.Req = ginCtx.Request
|
|
|
|
|
|
// 默认为不记录请求参数,可在handler回调函数中覆盖赋值
|
|
|
|
|
|
rc.ReqParam = 0
|
|
|
|
|
|
// 默认响应结果为nil,可在handler中赋值
|
|
|
|
|
|
rc.ResData = nil
|
|
|
|
|
|
|
|
|
|
|
|
// 调用请求前所有处理器
|
|
|
|
|
|
err := ApplyBeforeHandler(rc)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
panic(err)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-04-21 10:22:09 +08:00
|
|
|
|
begin := time.Now()
|
2021-04-16 15:10:07 +08:00
|
|
|
|
handler(rc)
|
2021-04-21 10:22:09 +08:00
|
|
|
|
rc.timed = time.Now().Sub(begin).Milliseconds()
|
2021-04-16 15:10:07 +08:00
|
|
|
|
ginx.SuccessRes(ginCtx, rc.ResData)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 请求前置处理器,返回error则停止后续逻辑
|
2021-03-24 17:18:39 +08:00
|
|
|
|
type BeforeHandler interface {
|
2021-04-16 15:10:07 +08:00
|
|
|
|
BeforeHandle(rc *ReqCtx) error
|
2021-03-24 17:18:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 请求后置处理器
|
|
|
|
|
|
type AfterHandler interface {
|
2021-04-16 15:10:07 +08:00
|
|
|
|
AfterHandle(rc *ReqCtx)
|
2021-03-24 17:18:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
|
BeforeHandlers []BeforeHandler
|
|
|
|
|
|
AfterHandlers []AfterHandler
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// 应用所有请求前置处理器
|
|
|
|
|
|
func ApplyBeforeHandler(rc *ReqCtx) error {
|
|
|
|
|
|
for _, e := range BeforeHandlers {
|
2021-04-16 15:10:07 +08:00
|
|
|
|
if err := e.BeforeHandle(rc); err != nil {
|
2021-03-24 17:18:39 +08:00
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 应用所有后置处理器
|
2021-04-16 15:10:07 +08:00
|
|
|
|
func ApplyAfterHandler(rc *ReqCtx) {
|
2021-03-24 17:18:39 +08:00
|
|
|
|
for _, e := range AfterHandlers {
|
2021-04-16 15:10:07 +08:00
|
|
|
|
e.AfterHandle(rc)
|
2021-03-24 17:18:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-04-16 15:10:07 +08:00
|
|
|
|
// 新建请求上下文,默认需要校验token
|
|
|
|
|
|
func NewReqCtx() *ReqCtx {
|
|
|
|
|
|
return &ReqCtx{NeedToken: true}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func NewReqCtxWithGin(g *gin.Context) *ReqCtx {
|
|
|
|
|
|
return &ReqCtx{NeedToken: true, GinCtx: g}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 调用该方法设置请求描述,则默认记录日志,并不记录响应结果
|
|
|
|
|
|
func (r *ReqCtx) WithLog(li *LogInfo) *ReqCtx {
|
|
|
|
|
|
r.LogInfo = li
|
|
|
|
|
|
return r
|
2021-03-24 17:18:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-04-16 15:10:07 +08:00
|
|
|
|
// 是否需要token
|
|
|
|
|
|
func (r *ReqCtx) WithNeedToken(needToken bool) *ReqCtx {
|
|
|
|
|
|
r.NeedToken = needToken
|
|
|
|
|
|
return r
|
2021-03-24 17:18:39 +08:00
|
|
|
|
}
|