当前位置:网站首页>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 .
边栏推荐
- 以太网接口介绍
- Homebrew brew update doesn't respond for a long time (or stuck in updating homebrew...)
- win11系统错误:由于找不到 iertutil.dll,无法继续执行代码。重新安装程序可能会解决此问题
- Redis Basics
- Operator3 - design an operator
- buck电路boot电容短路和断路实测波形
- 用户列表 圆形头像并跟随小板块
- [Charles' daily problems] when you open Charles, you can't use nails
- fillder使用
- Flink实时仓库-DWD层(流量域)模板代码
猜你喜欢

【OpenGL】着色器(Shader)的使用

2022-07-28: what is the output of the following go language code? A:AA; B:AB; C:BA; D:BB。 package main import ( “fmt“ ) func main() { f

win11VMware打开虚拟机就蓝屏重启以及启动不了的解决方案

使用VsCode配置MySQL实现连接、查询、等功能

After 4 years of development and 13K, if you want to change to automated testing, can your salary still rise···

Cvpr2021 | multi view stereo matching based on self supervised learning (cvpr2021)

WPF 界面布局必知基础

Excel文件读写(创建与解析)

Decompilation of wechat applet

解决CSDN因版权不明而无法发布博客的问题
随机推荐
约瑟夫环问题
【charles日常问题】开启charles,使用不了钉钉
Unity发送Post请求给GoLang服务端解析并返回
后缀自动机(SAM)讲解 + Luogu p3804【模板】后缀自动机 (SAM)
WPF嵌套布局案例
VMware16创建虚拟机:无法创建新虚拟机,不具备执行此操作的权限
Homebrew brew update 长时间没反应(或卡在 Updating Homebrew...)
我的个人网站不让接入微信登录,于是我做了这个
基于C语言设计的学生成绩排名系统
建木持续集成平台v2.5.2发布
[cf1054h] epic Revolution -- number theory, convolution, arbitrary modulus NTT
Summary of OCR optical character recognition methods
CAN&CANFD综合测试分析软件LKMaster与PCAN-Explorer 6分析软件的优势对比
H3C_利用设置缺省静态路由优先级实现出口双线路的主备功能
Operator3 - design an operator
JS chicken laying eggs and egg laying chickens. Who appeared earlier, object or function? Is function an instance of function?
win11系统错误:由于找不到 iertutil.dll,无法继续执行代码。重新安装程序可能会解决此问题
怎么会不喜欢呢,CICD中轻松发送邮件
对Vintage分析的一些学习理解
LeetCode 879. 盈利计划