From b6b0a7c0ca21ebfc56c0df10091e892bd2431bfe Mon Sep 17 00:00:00 2001 From: tiglog Date: Mon, 16 Oct 2023 15:56:11 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=89=A9=E5=B1=95=20gin.Context=20?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=20gweb.Conetxt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gweb/context.go | 116 ++++++++++++++++++++++++++++++++++++++++ gweb/context_test.go | 43 +++++++++++++++ gweb/{gin.go => web.go} | 1 - gweb/wrapper.go | 3 -- 4 files changed, 159 insertions(+), 4 deletions(-) create mode 100644 gweb/context.go create mode 100644 gweb/context_test.go rename gweb/{gin.go => web.go} (99%) diff --git a/gweb/context.go b/gweb/context.go new file mode 100644 index 0000000..ed5f6ab --- /dev/null +++ b/gweb/context.go @@ -0,0 +1,116 @@ +// +// 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 App struct { + *gin.Engine +} +type Router struct { + *gin.RouterGroup +} + +func New() *App { + return &App{ + Engine: gin.New(), + } +} +func Default() *App { + return &App{ + 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 *App) 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 *App) 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 *App) 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...) +} diff --git a/gweb/context_test.go b/gweb/context_test.go new file mode 100644 index 0000000..a22a7c8 --- /dev/null +++ b/gweb/context_test.go @@ -0,0 +1,43 @@ +// +// context_test.go +// Copyright (C) 2023 tiglog +// +// Distributed under terms of the MIT license. +// + +package gweb_test + +import ( + "testing" + + "git.hexq.cn/tiglog/golib/gweb" + "github.com/gin-gonic/gin" +) + +type controller struct { +} + +func (ctl *controller) foo(c *gweb.Context) { + + c.JSON(200, gin.H{ + "code": 0, + "msg": "hi, foo", + }) +} + +func (ctl *controller) bar(c *gweb.Context) { + c.String(200, "hello bar") +} + +func TestContext(t *testing.T) { + app := gweb.New() + + ctl := &controller{} + app.GET("/root", ctl.foo) + + v1 := app.Group("/v1") + { + v1.GET("/foo", ctl.bar) + } + _ = app.Run() +} diff --git a/gweb/gin.go b/gweb/web.go similarity index 99% rename from gweb/gin.go rename to gweb/web.go index e7472f9..7b95c9e 100644 --- a/gweb/gin.go +++ b/gweb/web.go @@ -59,5 +59,4 @@ func RunWeb(app *Engine, addr string) { } log.Print("Server exiting") - } diff --git a/gweb/wrapper.go b/gweb/wrapper.go index 769cc02..b30bf5e 100644 --- a/gweb/wrapper.go +++ b/gweb/wrapper.go @@ -10,9 +10,7 @@ package gweb import "github.com/gin-gonic/gin" type Engine = gin.Engine -type Context = gin.Context type H = gin.H -type HandlerFunc = gin.HandlerFunc type HandlersChain = gin.HandlersChain type RouteInfo = gin.RouteInfo type RoutesInfo = gin.RoutesInfo @@ -23,6 +21,5 @@ const ( TestMode = gin.TestMode ) -var New = gin.New var SetMode = gin.SetMode var Mode = gin.Mode