有些时候会遇到gin.Context自带属性或方法无法满足开发需求的情况,可以对gin.Context进行一定的拓展和自定义
大致拓展效果如下,可以新增属性和方法
func main() {
server := core.NewMyServer()
//这里将是自定义拓展的MyContext,不再是gin.Conext
server.GET("/test", func(c *core.MyContext) {
c.ID = 1 //新增ID属性
c.Hello() //新增Hello()方法
})
server.GET("/login", controller.Login)
v1 := server.Group("/v1")
{
v1.Use(middleware.Auth())
v1.GET("/users", controller.Users)
v1.GET("/detail",controller.UserDetail)
v1.POST("/update", controller.UpdateUser)
}
_ = server.Run()
}
★目录结构
project
── controller
└─ example.go
── core
└─ context.go
main.go
★参考代码
1、拓展gin.Context,增加 用户ID 属性,增加 Hello() 方法
2、此处代码存放在project → core → context.go
//MyContext 拓展gin.Context后的MyContext
type MyContext struct {
*gin.Context
ID int
}
//HandleFunc 实现gin.Context到自定义MyContext的转换。
func HandleFunc(handler func(c *MyContext)) func(ctx *gin.Context) {
return func(c *gin.Context) {
handler(&MyContext{Context: c})
}
}
//Hello 响应测试数据
func (c *MyContext) Hello() {
c.JSON(http.StatusOK, gin.H{
"code": 0,
"message": "测试成功",
"data": "Hello!" + strconv.Itoa(c.ID),
})
}
1、完成控制器代码
2、此处代码存放在project → controller → example.go
func Example(c *core.MyContext) {
c.ID = 99
c.Hello()
}
1、主函数运行测试
2、此处代码存放在project → main.go
func main() {
server := gin.Default()
/*这里所有路由都要使用HandleFunc进行转换,强迫症可能会难受,下文有解决方案。*/
server.GET("/example", core.HandleFunc(controller.Example))
_ = server.Run()
}
★测试结果
路由定义时,每个方法都调用HandleFunc进行转换,看起来非常难受,这里用一些手段进行消除。
★目录结构
project
── controller
└─ example.go
── core
└─ context.go
main.go
★参考代码
1、优化上文中的代码
2、此处代码存放在project → core → context.go
type MyServer struct {
*gin.Engine
}
//MyContext 拓展gin.Context后的MyContext
type MyContext struct {
*gin.Context
ID int
}
//MyRouterGroup 拓展后的自定义gin.RouterGroup
type MyRouterGroup struct {
*gin.RouterGroup
}
//NewMyServer 新建gin服务器
func NewMyServer() *MyServer {
myServer := &MyServer{Engine: gin.Default()}
return myServer
}
//handleFunc 实现gin.Context到自定义Context的转换。
func handleFunc(handler func(c *MyContext)) func(ctx *gin.Context) {
return func(c *gin.Context) {
handler(&MyContext{Context: c})
}
}
//Group 重写路由组注册
func (server *MyServer) Group(relativePath string, handlers ...func(c *MyContext)) *MyRouterGroup {
RHandles := make([]gin.HandlerFunc, 0)
for _, handle := range handlers {
RHandles = append(RHandles, handleFunc(handle))
}
return &MyRouterGroup{server.Engine.Group(relativePath, RHandles...)}
}
//GET 拓展Get请求(根)
func (server *MyServer) GET(relativePath string, handlers ...func(c *MyContext)) gin.IRoutes {
RHandles := make([]gin.HandlerFunc, 0)
for _, handle := range handlers {
RHandles = append(RHandles, handleFunc(handle))
}
return server.Engine.GET(relativePath, RHandles...)
}
//POST 拓展POST请求(根)
func (server *MyServer) POST(relativePath string, handlers ...func(c *MyContext)) gin.IRoutes {
RHandles := make([]gin.HandlerFunc, 0)
for _, handle := range handlers {
RHandles = append(RHandles, handleFunc(handle))
}
return server.Engine.POST(relativePath, RHandles...)
}
//GET 拓展Get请求(子)
func (r *MyRouterGroup) GET(relativePath string, handlers ...func(c *MyContext)) gin.IRoutes {
rHandles := make([]gin.HandlerFunc, 0)
for _, handle := range handlers {
rHandles = append(rHandles, handleFunc(handle))
}
return r.RouterGroup.GET(relativePath, rHandles...)
}
//POST 拓展Post请求(子)
func (r *MyRouterGroup) POST(relativePath string, handlers ...func(c *MyContext)) gin.IRoutes {
rHandles := make([]gin.HandlerFunc, 0)
for _, handle := range handlers {
rHandles = append(rHandles, handleFunc(handle))
}
return r.RouterGroup.POST(relativePath, rHandles...)
}
//Use 拓展中间件注册
func (r *MyRouterGroup) Use(middlewares ...func(c *MyContext)) gin.IRoutes {
rMiddlewares := make([]gin.HandlerFunc, 0)
for _, middleware := range middlewares {
rMiddlewares = append(rMiddlewares, handleFunc(middleware))
}
return r.RouterGroup.Use(rMiddlewares...)
}
//Hello 打印Hello!+MyContext中的ID
func (c *MyContext) Hello() {
c.JSON(http.StatusOK, gin.H{
"code": 0,
"message": "测试成功",
"data": "Hello!" + strconv.Itoa(c.ID),
})
}
1、控制器代码分Root和Node
2、此处代码存放在project → controller→ example.go
func RootExample(c *core.MyContext) {
c.ID = 10
c.Hello()
}
func NodeExample(c *core.MyContext) {
c.ID = 20
c.Hello()
}
1、主函数运行测试
2、此处代码存放在project → main.go
func main() {
server := core.NewMyServer()
server.GET("/root", controller.RootExample)
v1 := server.Group("/v1")
{
v1.GET("/node", controller.NodeExample)
}
_ = server.Run()
}
★测试结果