GO——gin中间件和路由

中间件

参考:https://learnku.com/articles/66234

  • 结构
    • 中间件是函数
    • 中间件函数被放在调用链上
    • 调用链的末尾是路由path对应的函数
  • 执行过程
    • net/http包调用到gin的serverHTTP
      • 参考:go/pkg/mod/github.com/gin-gonic/gin@v1.7.7/gin.go:506
    • 通过path找到路由对应的处理链,赋值给context
      • 参考:go/pkg/mod/github.com/gin-gonic/gin@v1.7.7/gin.go:553
    • 执行c.next()启动链条
问题:中间件函数中为啥要调用next,不调用,能继续执行完后面的中间件吗

参考:https://blog.dianduidian.com/post/gin-中间件next方法原理解析/

  • 可以
  • 即使中间件没有调用next函数,那么后续中间件和handles也会执行
  • 中间件中调用next函数,会先执行后续中间件,然后回到这个中间件,继续执行next函数之后的代码
    • 参考:/Users/didi/go/pkg/mod/github.com/gin-gonic/gin@v1.7.7/context.go:165
      • 这里的c.index是一个共用的值
      • c.handlers[c.index](c)执行的时候可能会变,所以虽然在中间件中有调用next函数,但是中间价并不会重复执行
  • 提前结束,不能使用return,要使用abort
    • abort:c.index = abortIndex
代码参考
复制代码
package main

import (
	"encoding/json"
	"fmt"
)

/****  上下文
 */

// Context 上下文
type Context struct {
	Keys    map[string]interface{}
	handles HandleChain
	index   int
}

type HandleChain []func(ctx *Context)

func (c *Context) Render(resultCode int, data interface{}) {
	d, _ := json.Marshal(data)
	fmt.Println("resultCode:", resultCode, " , data:", string(d))
}

func (c *Context) Next() {
	c.index++
	for c.index < len(c.handles) {
		c.handles[c.index](c)
		c.index++
	}
}

func (c *Context) reset() {
	c.index = -1
}

/** 路由
 */

// router 路由
type router struct {
	Handlers map[string]HandleChain
}

type Group struct {
	Handles HandleChain
}

func (r *router) RegisterRoute(url string, f func(*Context), group Group) () {
	if r.Handlers == nil {
		r.Handlers = make(map[string]HandleChain)
	}
	r.Handlers[url] = append(r.Handlers[url], group.Handles...)
	r.Handlers[url] = append(r.Handlers[url], f)
}

func (g *Group) Use(f func(*Context)) {
	g.Handles = append(g.Handles, f)
}

func (r *router) Run(url string, c *Context) () {
	c.handles = r.Handlers[url]
	c.reset()
	c.Next()
}

/** 业务代码
 */

// PlusController 加法
func PlusController(c *Context) {
	a := c.Keys["a"].(int)
	b := c.Keys["b"].(int)
	c.Render(200, plus(a, b))
}

// plus 加法函数
func plus(a, b int) int {
	return a + b
}

// MultiController 乘法
func MultiController(c *Context) {
	a := c.Keys["a"].(int)
	b := c.Keys["b"].(int)
	c.Render(200, multi(a, b))
}

// multi 加法函数
func multi(a, b int) int {
	return a * b
}

/** 主函数
 */

func main() {
	r := router{}
	group1 := Group{Handles: HandleChain{
		func(ctx *Context) {
			fmt.Println("testMiddle1")
		},
		func(ctx *Context) {
			ctx.Next()
			fmt.Println("testMiddle2")
		},
	}}

	group2 := Group{Handles: HandleChain{
		func(ctx *Context) {
			fmt.Println("testMiddle3")
			ctx.Next()
			fmt.Println("testMiddle4")
		},
	}}

	r.RegisterRoute("*", MultiController,group1)
	r.RegisterRoute("+", PlusController,group2)

	fmt.Println("----------")
	r.Run("*", &Context{Keys: map[string]interface{}{"a": 34, "b": 245}})
	fmt.Println("----------")
	r.Run("+", &Context{Keys: map[string]interface{}{"a": 34, "b": 245}})
}
相关推荐
晴空了无痕7 小时前
从 Go 基础到 K8s:一条可落地的 Go 服务端成长路线
开发语言·后端·golang·kubernetes
jieyucx8 小时前
Nuxt4阶段六:工程化与进阶 —— 模块、中间件、插件、TS 与测试
中间件·vue·web·nuxt·全栈·ssr
qq_4523962311 小时前
第五篇:《接口与错误处理:Go 的哲学》
开发语言·后端·golang
布朗克16812 小时前
Go 入门到精通-29-测试与基准
开发语言·后端·golang·测试与基准
止语Lab18 小时前
从 PHP 到 Go:真正迁移的是复杂度的归属
android·golang·php
灯澜忆梦1 天前
GO_并发编程---定时器
开发语言·后端·golang
Wang's Blog1 天前
Go-Zero项目开发10: 微服务治理中心实现原理分析
开发语言·微服务·golang
qq_452396232 天前
第二篇:《Go 开发环境搭建:SDK、IDE、Module 与 Hello World》
开发语言·ide·golang
FellAveal2 天前
【Go语言入门学习笔记】Part13.结构体与接口
笔记·学习·golang
microrain2 天前
从设备孤岛到智能协同:SagooIoT场景联动实战解析
物联网·golang·sagooiot