From 92b821a12d1bff569af0161e437665d67065cb29 Mon Sep 17 00:00:00 2001 From: bryanqiu Date: Fri, 9 Dec 2022 14:51:14 +0800 Subject: [PATCH] add ApiHandler --- api.go | 19 +++++++++++++++++++ context.go | 5 +---- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/api.go b/api.go index c2f7ec4..28c4585 100644 --- a/api.go +++ b/api.go @@ -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 diff --git a/context.go b/context.go index c750970..8f46607 100644 --- a/context.go +++ b/context.go @@ -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