gin框架中间件的使用之Next()和Abort()
一、 Next() 和 Abort() 的含义
- Next() 的含义
语法:
func (c *Context) Next()
英文原文
Next should be used only inside middleware. It executes the pending handlers in the chain inside the calling handler.
大意:Next 应该仅可以在中间件中使用,它在调用的函数中的链中执行挂起的函数。 2. Abort() 的含义
语法:
func (c *Context)Abort()
英文原文
Abort prevents pending handlers from being called. Note that this will not stop the current handler. Let's say you have an authorization middleware that validates that the current request is authorized. If the authorization fails (ex: the password does not match), call Abort to ensure the remaining handlers for this request are not called.
大意:Abort 在被调用的函数中阻止挂起函数。注意这将不会停止当前的函数。例如,你有一个验证当前的请求是否是认证过的 Authorization 中间件。如果验证失败(例如,密码不匹配),调用 Abort 以确保这个请求的其他函数不会被调用。
https://blog.csdn.net/cyberspecter/article/details/100602552
原文
文章目录
一、 Next() 和 Abort() 的含义
- Next() 的含义
语法:
func (c *Context) Next()
1
英文原文
Next should be used only inside middleware. It executes the pending handlers in the chain inside the calling handler.
1
大意:Next 应该仅可以在中间件中使用,它在调用的函数中的链中执行挂起的函数。 2. Abort() 的含义
语法:
func (c *Context)Abort()
1
英文原文
Abort prevents pending handlers from being called. Note that this will not stop the current handler. Let's say you have an authorization middleware that validates that the current request is authorized. If the authorization fails (ex: the password does not match), call Abort to ensure the remaining handlers for this request are not called.
1
大意:Abort 在被调用的函数中阻止挂起函数。注意这将不会停止当前的函数。例如,你有一个验证当前的请求是否是认证过的 Authorization 中间件。如果验证失败(例如,密码不匹配),调用 Abort 以确保这个请求的其他函数不会被调用。
二、示例分析
func main(){
router := gin.Default()
router.Use(middleware.FirstMiddleware(), middleware.SecondMiddleware()) // (1)
router.Run()
}
func FirstMiddleware() gin.HandlerFunc {
return func( c *gin.Context) {
fmt.Println("first middleware before next()")
isAbort := c.Query("isAbort")
bAbort, err := strconv.ParseBool(isAbort)
if err != nil {
fmt.Printf("is abort value err, value %s\n", isAbort)
c.Next() // (2)
}
if bAbort {
fmt.Println("first middleware abort") //(3)
c.Abort()
//c.AbortWithStatusJSON(http.StatusOK, "abort is true")
return
} else {
fmt.Println("first middleware doesnot abort") //(4)
return
}
fmt.Println("first middleware after next()")
}
}
func SecondMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
fmt.Println("current inside of second middleware")
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
- 在调用时传递的 isAbort() 的值不能解析为布尔类型的值时,会执行到 (2) 处。此时调用的是 Next() 方法。由第一部分的内容可知。它执行挂起的函数,即在 (1) 处指定的函数,即 SecondMiddleware(),最后执行真正的业务处理函数,程序打印:
first middleware before next()
is abort value err, value d
current inside of second middleware
inner hello
first middleware after next()
1
2
3
4
5
由上面的打印可知,程序在执行完业务处理后仍然会回到当前的函数。然后打印其他的内容。在使用的过程中,可以根据其余部分是否继续执行而在 c.Next() 后添加 return 语句。 2. 在调用时传递的 isAbort() 的值如果是可以解析为布尔类型的值时,如果值为真,则执行到 (3) 处。由于此处调用了 Abort() ,根据第一部分的内容可知,它阻止了链中后续的函数的调用。所以 SecondMiddleware 函数和业务函数 Hello() 函数不会被调用。但它不能停止当前的函数,故其下面的打印仍会打印
first middleware before next()
first middleware abort
first middleware after next()
1
2
3
注意: Abort()和AbortWithStatusJSON` 一直都是中止链的调用,不同之处在于,前者不会返回给前端任何内容,而后者则可以返回给前端一个JSON串。 3. 在调用时传递的 isAbort() 的值可以解析为布尔类型,如果值为假,则流程会进入到 (4)处。它只是在当前的函数调用中返回,不会对整个链造成影响。
first middleware before next()
first middleware doesnot abort
current inside of second middleware
inner hello