当前位置:网站首页>Gin Middleware
Gin Middleware
2022-07-29 07:17:00 【Mar, LiuNian】
List of articles
Middleware format
gin.HandlerFunc, Satisfy this function
//gin.HandlerFunc
type HandlerFunc func(*Context)
Use
adopt engine.Use(gin.HandlerFunc) register
package main
import (
"fmt"
"github.com/gin-gonic/gin"
"net/http"
"time"
)
func log() gin.HandlerFunc {
return func(c *gin.Context) {
now := time.Now()
// Execute next Middleware
c.Next()
end := time.Since(now)
fmt.Printf(" The request takes time %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")
}
Terminate Middleware
Generally, there are two modes for the implementation of middleware :
1. Middleware based on responsibility chain , In the middleware return Will not execute the next middleware
2. be based on AOP Faceted middleware ,return It is not necessary that the next middleware will not be executed
gin The middleware of is based on AOP To build the . among return The next middleware will be executed , If you don't want to execute , Need to call gin.Context.Abort() Method
package main
import (
"fmt"
"github.com/gin-gonic/gin"
"net/http"
"time"
)
func log() gin.HandlerFunc {
return func(c *gin.Context) {
now := time.Now()
// Want to terminate the execution of middleware ,return It's invalid
c.Abort()
end := time.Since(now)
fmt.Printf(" The request takes time %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 The reason why it cannot be terminated
// see Next() Method source code
func (c *Context) Next() {
c.index++
for c.index < int8(len(c.handlers)) {
c.handlers[c.index](c)
c.index++
}
}
c.handlers Is the execution function of middleware ,c.index Decide which middleware should be implemented .
Suppose now c.haandlers There is A,B,C,D Four Middleware . In the beginning gin Would call Next(),
Execution order A,B,C,D. among A Termination will not hinder B,C,D Implementation . If not used in middleware
Next(), At this time, they are parallel . When A Called in Next(),A Nested in B,C,D Execution of middleware .Abort() take c.index Adjust to a far greater than len(c.handlers) Value , To prevent the execution of subsequent middleware .
边栏推荐
- Thoroughly understand kubernetes scheduling framework and plug-ins
- Image noise and matrix inversion
- gin 中间件
- 要不要满足客户所有的需求
- 路由中的生命周期钩子 - activated与deactivated
- WPF简单登录页面的完成案例
- CAN&CANFD综合测试分析软件LKMaster与PCAN-Explorer 6分析软件的优势对比
- Student status management system based on C language design
- Improved Pillar with Fine-grained Feature for 3D Object Detection论文笔记
- 【C语言刷LeetCode】1054. 距离相等的条形码(M)
猜你喜欢
怎么会不喜欢呢,CICD中轻松发送邮件
[Charles' daily problems] when you open Charles, you can't use nails
Vite3.0 has been released, can you still roll it (list of new features)
Student status management system based on C language design
Improved Pillar with Fine-grained Feature for 3D Object Detection论文笔记
作业7.28 文件IO与标准IO
It's enough for MySQL to have this article (disgusting and crazy typing 37k words, just for Bo Jun's praise!!!)
spark学习笔记(七)——sparkcore核心编程-RDD序列化/依赖关系/持久化/分区器/累加器/广播变量
After three years of outsourcing, the salary of automatic testing after job hopping is twice that of the original. The secret is
win11VMware打开虚拟机就蓝屏重启以及启动不了的解决方案
随机推荐
Cesium reflection
以太网接口介绍
Hj37 statistics of the total number of rabbits per month Fibonacci series
pytest合集(7)— 参数化
Student achievement ranking system based on C language design
城市花样精~侬好!DESIGN#可视化电台即将开播
tp6 使用 ProtoBuf
基于C语言设计的学生成绩排名系统
vagrant box 集群 处理
20-40k | mecarmand 3D vision algorithm / software / Product Manager Recruitment
个人博客系统(附源码)
Record - step on the pit - real-time data warehouse development - doris/pg/flink
Fillder use
JS 鸡生蛋与蛋生鸡问题,Object与Function究竟谁出现的更早?Function算不算Function的实例?
Homebrew brew update 长时间没反应(或卡在 Updating Homebrew...)
接口测试实战项目03:执行测试用例
上采样之反卷积操作
route的meta配置项
fillder使用
1172. The plate stack has a sequence table + stack