feat: 扩展 gin.Context 使用 gweb.Conetxt

This commit is contained in:
tiglog 2023-10-16 15:56:11 +08:00
parent d953b99b62
commit b6b0a7c0ca
4 changed files with 159 additions and 4 deletions

116
gweb/context.go Normal file
View File

@ -0,0 +1,116 @@
//
// context.go
// Copyright (C) 2023 tiglog <me@tiglog.com>
//
// 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...)
}

43
gweb/context_test.go Normal file
View File

@ -0,0 +1,43 @@
//
// context_test.go
// Copyright (C) 2023 tiglog <me@tiglog.com>
//
// 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()
}

View File

@ -59,5 +59,4 @@ func RunWeb(app *Engine, addr string) {
}
log.Print("Server exiting")
}

View File

@ -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