add ApiHandler

This commit is contained in:
bryanqiu 2022-12-09 14:51:14 +08:00
parent 136178a1c1
commit 92b821a12d
2 changed files with 20 additions and 4 deletions

19
api.go
View File

@ -77,6 +77,8 @@ func middlewareMyApiEngine() gin.HandlerFunc {
defer log.Cleanup()
// Step 2. init *api.Context
req := c.Request
log.DebugfWithDepth(1, "[%s|%s|%s]", req.Method, req.Host, req.RequestURI)
cc := New(c)
c.Set("cc", cc)
@ -96,6 +98,23 @@ func defaultLogidGetter(c *gin.Context) (logid string) {
panic("unreachable code")
}
func CC(c *gin.Context) *Context {
if icc, ok := c.Get("cc"); !ok {
log.Errorf("Unreachable Code: can not get cc(*api.Context) from *gin.Context")
} else if cc, ok := icc.(*Context); !ok {
log.Debugf("Unreachable Code: cc from *gin.Context is type of:[%s]", reflect.TypeOf(icc).String())
log.Errorf("Unreachable Code: cc from *gin.Context is not type of *api.Context")
} else {
return cc
}
return nil
}
func ApiHandler(c *gin.Context) Handler {
cc := CC(c)
return cc.ApiHandler()
}
func DumpHandler(c *Context) error {
errmsg := "api not implemented"
errcode := errCodeUnimplementApi

View File

@ -57,9 +57,6 @@ func (c *Context) TryRecover() {
}
func (c *Context) BindInput(i interface{}) (err error) {
req := c.Request
log.DebugfWithDepth(1, "[%s|%s|%s]", req.Method, req.Host, req.RequestURI)
logstr := fmt.Sprintf("input:[have not initialized]")
defer func(plogstr *string) {
log.DebugfWithDepth(2, "%s", *plogstr)
@ -186,7 +183,7 @@ func (c *Context) ShapeOutput(o interface{}) {
return
}
func (c *Context) Handler() Handler {
func (c *Context) ApiHandler() Handler {
h := c.Context.Handler()
if handler, ok := handlerMapper[fmt.Sprintf("%v", h)]; ok {
return handler