当前位置:网站首页>gin 中间件
gin 中间件
2022-07-29 06:20:00 【Mar丶流年】
中间件格式
gin.HandlerFunc,满足这个函数即可
//gin.HandlerFunc
type HandlerFunc func(*Context)
使用
通过 engine.Use(gin.HandlerFunc) 注册
package main
import (
"fmt"
"github.com/gin-gonic/gin"
"net/http"
"time"
)
func log() gin.HandlerFunc {
return func(c *gin.Context) {
now := time.Now()
//执行下一个中间件
c.Next()
end := time.Since(now)
fmt.Printf("请求耗时 %s", end)
}
}
func main() {
r := gin.New()
r.Use(log())
r.GET("/ping", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"message": "pong",
})
})
r.Run(":8080")
}
终止中间件
一般中间件的实现有两种模式:
1.基于责任链的中间件,在中间件中return就不会执行下一个中间件
2.基于AOP切面的中间件,return 不一定就不会执行下一个中间件
gin的中间件就是基于AOP打造的。其中return还是会执行下一个中间件的,如果不想执行,需要调用gin.Context.Abort()方法
package main
import (
"fmt"
"github.com/gin-gonic/gin"
"net/http"
"time"
)
func log() gin.HandlerFunc {
return func(c *gin.Context) {
now := time.Now()
//想要终止中间件的执行,return 是无效的
c.Abort()
end := time.Since(now)
fmt.Printf("请求耗时 %s", end)
}
}
func main() {
r := gin.New()
r.Use(log())
r.GET("/ping", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"message": "pong",
})
})
r.Run(":8080")
}
return 不能终止的原因
//查看Next()方法源码
func (c *Context) Next() {
c.index++
for c.index < int8(len(c.handlers)) {
c.handlers[c.index](c)
c.index++
}
}
c.handlers 中是中间件的执行函数,c.index 决定应该执行那个中间件了。
假设现在c.haandlers中有A,B,C,D四个中间件。最开始gin会调用Next(),
执行顺序A,B,C,D。其中A终止了并不会阻碍B,C,D的执行。如果中间件中不使用
Next(),这时候他们是平行的。当A中调用了Next(),A中就嵌套了B,C,D中间件的执行。Abort() 将c.index 调整到一个远远大于len(c.handlers)的值,来阻止后续中间件的执行。
边栏推荐
- Difference between CNAME record and a record
- Student status management system based on C language design
- Summary of 2022 SQL classic interview questions (with analysis)
- 外包干了3年,跳槽后转自动化测试工资是原来的2倍,秘诀原来是......
- Junda technology | applicable to "riyueyuan" brand ups wechat cloud monitoring card
- Teacher Wang Shuyao's notes on operations research 09 linear programming and simplex method (Application of simplex table)
- 模拟卷Leetcode【普通】061. 旋转链表
- 新同事写了几段小代码,把系统给搞崩了,被老板爆怼一顿!
- 10 frequently asked JVM questions in interviews
- MySQL: what happens in the bufferpool when you crud? Ten pictures can make it clear
猜你喜欢

IDEA找不到Database解决方法

线程 - 线程安全 - 线程优化

外包干了3年,跳槽后转自动化测试工资是原来的2倍,秘诀原来是......

IO流 - File - properties

SSH免密登录-两台虚拟机建立免密通道 双向信任

IDEA中实现Mapper接口到映射文件xml的跳转

数组的子集不能累加出的最小正数

实现改变一段文字的部分颜色效果

Flink real-time warehouse DWD layer (transaction domain - additional purchase dimension degradation processing) template code

Flink real time warehouse DWD layer (traffic domain) template code
随机推荐
王树尧老师运筹学课程笔记 00 写在前面
Flink实时仓库-DWD层(kafka-关联mysql的lookup join)模板代码
【C语言刷LeetCode】67. 二进制求和(E)
Analog volume leetcode [normal] 093. Restore IP address
模拟卷Leetcode【普通】222. 完全二叉树的节点个数
[C language brush leetcode] 67. binary sum (E)
Student status management system based on C language design
上采样之反卷积操作
线程同步—— 生产者与消费者、龟兔赛跑、双线程打印
Ali gave several SQL messages and asked how many tree search operations need to be performed?
Excerpts from good essays
解决CSDN因版权不明而无法发布博客的问题
Teacher wangshuyao's notes on operations research course 10 linear programming and simplex method (discussion on detection number and degradation)
SSH免密登录-两台虚拟机建立免密通道 双向信任
Teacher wangshuyao's operations research course notes 07 linear programming and simplex method (standard form, base, base solution, base feasible solution, feasible base)
【charles日常问题】开启charles,使用不了钉钉
1172. 餐盘栈 有序列表+栈
图像加噪声与矩阵求逆
Teacher Wu Enda's machine learning course notes 04 multiple linear regression
Flink real time warehouse DWD layer (traffic domain) template code