当前位置:网站首页>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)的值,来阻止后续中间件的执行。
边栏推荐
- Unity exploration plot access design analysis & process + code specific implementation
- Leetcode-592: fraction addition and subtraction
- Overview of database system
- Teacher wangshuyao's operations research course notes 07 linear programming and simplex method (standard form, base, base solution, base feasible solution, feasible base)
- 'function VTable for error: undefined reference to... 'cause and solution of the problem
- MySQL: what happens in the bufferpool when you crud? Ten pictures can make it clear
- 上采样之反卷积操作
- Unity探索地块通路设计分析 & 流程+代码具体实现
- 如何优雅的写 Controller 层代码?
- Teacher Wu Enda's machine learning course notes 02 univariate linear regression
猜你喜欢
Jetpack Compose 中的键盘处理
要不要满足客户所有的需求
王树尧老师运筹学课程笔记 04 线性代数基础
新同事写了几段小代码,把系统给搞崩了,被老板爆怼一顿!
Cvpr2022oral special series (I): low light enhancement
Flink实时仓库-DWD层(交易域-加购维度退化处理)模板代码
spark学习笔记(七)——sparkcore核心编程-RDD序列化/依赖关系/持久化/分区器/累加器/广播变量
Record - step on the pit - real-time data warehouse development - doris/pg/flink
Windows 上 php 7.4 连接 oracle 配置
Some tips of vim text editor
随机推荐
Cesium reflection
How to write controller layer code gracefully?
Teacher Wu Enda's machine learning course notes 00 are written in the front
Summary of 2022 SQL classic interview questions (with analysis)
Dbasql interview questions
游戏资产的革命
Pytorch多GPU条件下DDP集群分布训练实现(简述-从无到有)
Apisik health check test
模拟卷Leetcode【普通】222. 完全二叉树的节点个数
模拟卷Leetcode【普通】093. 复原 IP 地址
模拟卷Leetcode【普通】061. 旋转链表
Flink实时仓库-DWD层(交易域-加购维度退化处理)模板代码
Jetpack Compose 中的键盘处理
Ali gave several SQL messages and asked how many tree search operations need to be performed?
Is online legend software testing training really so black hearted? Are they all scams?
Invalid access control
Idea cannot find a database solution
王树尧老师运筹学课程笔记 04 线性代数基础
[C language brush leetcode] 2332. The latest time to get on the bus (m)
实战!聊聊如何解决MySQL深分页问题