当前位置:网站首页>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)的值,来阻止后续中间件的执行。
边栏推荐
- Teacher Wu Enda's machine learning course notes 03 review of linear algebra
- Can MySQL export tables regularly?
- 数据库使用psql及jdbc进行远程连接,不定时自动断开的解决办法
- Ali gave several SQL messages and asked how many tree search operations need to be performed?
- 模拟卷Leetcode【普通】172. 阶乘后的零
- Flink real time warehouse DWD layer (traffic domain) template code
- 聊天机器人有何用处?有何类型?看完这些就明白了!
- Excerpts from good essays
- Overview of database system
- MVFuseNet:Improving End-to-End Object Detection and Motion Forecasting through Multi-View Fusion of
猜你喜欢

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

阿里一面,给了几条SQL,问需要执行几次树搜索操作?

10 frequently asked JVM questions in interviews

Image noise and matrix inversion

C language memory stack and heap usage

buck电路boot电容短路和断路实测波形

Flink real time warehouse DWD layer (traffic domain) template code

Windows 上 php 7.4 连接 oracle 配置

spark学习笔记(七)——sparkcore核心编程-RDD序列化/依赖关系/持久化/分区器/累加器/广播变量

猜数字//第一次使用生成随机数
随机推荐
建木持续集成平台v2.5.2发布
Relative date used by filter in salesforce
[C language brush leetcode] 1054. Bar code with equal distance (m)
Decompilation of wechat applet
Sword finger offer II 115: reconstruction sequence
ECCV 2022丨轻量级模型架ParC-Net 力压苹果MobileViT代码和论文下载
Junda technology | applicable to "riyueyuan" brand ups wechat cloud monitoring card
MySQL queries are case sensitive
The core of openresty and cosocket
pytest合集(7)— 参数化
[C language brush leetcode] 2332. The latest time to get on the bus (m)
CVPR2022Oral专题系列(一):低光增强
leetcode-1331:数组序号转换
竣达技术 | 适用于”日月元”品牌UPS微信云监控卡
数组的子集能否累加出K
[cf1054h] epic Revolution -- number theory, convolution, arbitrary modulus NTT
模拟卷Leetcode【普通】150. 逆波兰表达式求值
Cesium反射
阿里一面,给了几条SQL,问需要执行几次树搜索操作?
Teacher wangshuyao wrote the notes of operations research course 00 in the front