// // context.go // Copyright (C) 2023 tiglog // // Distributed under terms of the MIT license. // package gweb import "github.com/gin-gonic/gin" type IToken interface { GetToken() string IsExpired() string } type IClient interface { GetAppId() string GetAppSecret() string } type IUser interface { GetUserId() int64 GetUserName() string } type Context struct { *gin.Context VarInt int VarStr string Token IToken Client IClient User IUser } type Engine struct { *gin.Engine } type Router struct { *gin.RouterGroup } func New() *Engine { return &Engine{ Engine: gin.New(), } } func Default() *Engine { return &Engine{ Engine: gin.Default(), } } type HandleFunc func(c *Context) func handleFunc(hd func(c *Context)) func(ctx *gin.Context) { return func(c *gin.Context) { hd(&Context{ Context: c, }) } } // 重写路由组注册 func (app *Engine) Group(relativePath string, handlers ...HandleFunc) *Router { hds := make([]gin.HandlerFunc, 0) for _, hd := range handlers { hds = append(hds, handleFunc(hd)) } return &Router{ app.Engine.Group(relativePath, hds...), } } // 重写根GET请求 func (app *Engine) GET(relativePath string, handlers ...HandleFunc) gin.IRoutes { hds := make([]gin.HandlerFunc, 0) for _, hd := range handlers { hds = append(hds, handleFunc(hd)) } return app.Engine.GET(relativePath, hds...) } // 重写根POST请求 func (app *Engine) POST(relativePath string, handlers ...HandleFunc) gin.IRoutes { hds := make([]gin.HandlerFunc, 0) for _, hd := range handlers { hds = append(hds, handleFunc(hd)) } return app.Engine.POST(relativePath, hds...) } // 重写子GET请求 func (r *Router) GET(relativePath string, handlers ...HandleFunc) gin.IRoutes { hds := make([]gin.HandlerFunc, 0) for _, hd := range handlers { hds = append(hds, handleFunc(hd)) } return r.RouterGroup.GET(relativePath, hds...) } // 重写子 POST 请求 func (r *Router) POST(relativePath string, handlers ...HandleFunc) gin.IRoutes { hds := make([]gin.HandlerFunc, 0) for _, hd := range handlers { hds = append(hds, handleFunc(hd)) } return r.RouterGroup.POST(relativePath, hds...) } // 重写中间件注册 func (r *Router) Use(mws ...HandleFunc) gin.IRoutes { hds := make([]gin.HandlerFunc, 0) for _, mw := range mws { hds = append(hds, handleFunc(mw)) } return r.RouterGroup.Use(hds...) }