package api import ( "reflect" "github.com/gin-gonic/gin" "qoobing.com/gomod/log" ) type routes struct { gin.IRoutes } type HandlerFunc func(*Context) error type IRoutes interface { POST(uri string, handler HandlerFunc) } func (r routes) POST(uri string, handler HandlerFunc) { r.IRoutes.POST(uri, handlerWrapper(handler)) } func DumpHandler(c *Context) error { return c.RESULT_ERROR(errCodeUnimplementApi, "api not implemented") } func ErrorHandler(c *gin.Context, errcode int, errmsg string) { handler := handlerWrapper(func(c *Context) error { return c.RESULT_ERROR(errcode, errmsg) }) handler(c) } func handlerWrapper(handler HandlerFunc) gin.HandlerFunc { return func(c *gin.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 { defer cc.TryRecover() cc.AppId = c.GetString("appid") cc.UserId = c.GetUint64("userid") handler(cc) } } }