c.netx()
go
复制代码
package main
import (
"fmt"
"github.com/gin-gonic/gin"
"log"
"net/http"
)
func main() {
g := gin.New()
g.Use(middleware1(), middleware2())
g.GET("/", func(c *gin.Context) {
fmt.Println("process request")
c.JSON(http.StatusOK, "hello,world!")
})
err := g.Run()
if err != nil {
log.Fatal(err)
}
}
func middleware1() gin.HandlerFunc {
return func(c *gin.Context) {
fmt.Println("middleware1 start")
c.Next()
fmt.Println("middleware1 end")
}
}
func middleware2() gin.HandlerFunc {
return func(c *gin.Context) {
fmt.Println("middleware2 start")
c.Next()
fmt.Println("middleware2 end")
}
}
bash
复制代码
middleware1 start
middleware2 start
process request
middleware2 end
middleware1 end
- 总结
中间件的执行顺序是按照注册的顺序执行的,在中间件中使用 c.Next() 方法,会先执行c.Next() 前面的,然后将控制权传递给下一个中间件或处理器,最后按照相反顺序执行中间件c.Next() 后面的代码。
c.abort()
bash
复制代码
package main
import (
"fmt"
"github.com/gin-gonic/gin"
"log"
"net/http"
)
func main() {
g := gin.New()
g.Use(middleware1(), middleware2(), middleware3())
g.GET("/", func(c *gin.Context) {
fmt.Println("process request")
c.JSON(http.StatusOK, "hello,world!")
})
err := g.Run()
if err != nil {
log.Fatal(err)
}
}
func middleware1() gin.HandlerFunc {
return func(c *gin.Context) {
fmt.Println("middleware1 start")
c.Next()
fmt.Println("middleware1 end")
}
}
func middleware2() gin.HandlerFunc {
return func(c *gin.Context) {
fmt.Println("middleware2 start")
c.Abort()
fmt.Println("middleware2 end")
}
}
func middleware3() gin.HandlerFunc {
return func(c *gin.Context) {
fmt.Println("middleware3 start")
c.Next()
fmt.Println("middleware3 end")
}
}
bash
复制代码
middleware1 start
middleware2 start
middleware2 end
middleware1 end
- 总结
中间件的执行顺序是按照注册顺序执行的,中间件可以通过 c.Abort() 来中止后续中间件或处理器的处理流程。
return
bash
复制代码
package main
import (
"fmt"
"github.com/gin-gonic/gin"
"log"
"net/http"
)
func main() {
g := gin.New()
g.Use(middleware1(), middleware2(), middleware3())
g.GET("/", func(c *gin.Context) {
fmt.Println("process request")
c.JSON(http.StatusOK, "hello,world!")
})
err := g.Run()
if err != nil {
log.Fatal(err)
}
}
func middleware1() gin.HandlerFunc {
return func(c *gin.Context) {
fmt.Println("middleware1 start")
c.Next()
fmt.Println("middleware1 end")
}
}
func middleware2() gin.HandlerFunc {
return func(c *gin.Context) {
fmt.Println("middleware2 start")
return
fmt.Println("middleware2 end")
}
}
func middleware3() gin.HandlerFunc {
return func(c *gin.Context) {
fmt.Println("middleware3 start")
c.Next()
fmt.Println("middleware3 end")
}
}
bash
复制代码
middleware1 start
middleware2 start
middleware3 start
process request
middleware3 end
middleware1 end
- 总结
中间件的执行顺序是按照注册顺序执行的,中间件可以通过 retrurn 来中止当前中间件的后续处理流程。
c.abort() +return
bash
复制代码
package main
import (
"fmt"
"github.com/gin-gonic/gin"
"log"
"net/http"
)
func main() {
g := gin.New()
g.Use(middleware1(), middleware2(), middleware3())
g.GET("/", func(c *gin.Context) {
fmt.Println("process request")
c.JSON(http.StatusOK, "hello,world!")
})
err := g.Run()
if err != nil {
log.Fatal(err)
}
}
func middleware1() gin.HandlerFunc {
return func(c *gin.Context) {
fmt.Println("middleware1 start")
c.Next()
fmt.Println("middleware1 end")
}
}
func middleware2() gin.HandlerFunc {
return func(c *gin.Context) {
fmt.Println("middleware2 start")
c.Abort()
return
fmt.Println("middleware2 end")
}
}
func middleware3() gin.HandlerFunc {
return func(c *gin.Context) {
fmt.Println("middleware3 start")
c.Next()
fmt.Println("middleware3 end")
}
}
bash
复制代码
middleware1 start
middleware2 start
middleware1 end
- 总结
中间件的执行顺序是按照注册顺序执行的,中间件可以通过 c.abort() + retrurn 来中止当前中间件,后续中间件和处理器的处理流程。