c.netx()
测试代码
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") } }
访问 http://127.0.0.1:8080 执行结果如下,浏览器输出 “hello,world!”
middleware1 start
middleware2 start
process request
middleware2 end
middleware1 end
总结
中间件的执行顺序是按照注册的顺序执行的,在中间件中使用 c.Next() 方法,会先执行c.Next() 前面的,然后将控制权传递给下一个中间件或处理器,最后按照相反顺序执行中间件c.Next() 后面的代码。 c.abort() 测试代码
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") } }
访问 http://127.0.0.1:8080 执行结果如下,浏览器无输出
middleware1 start
middleware2 start
middleware2 end
middleware1 end
总结
中间件的执行顺序是按照注册顺序执行的,中间件可以通过 c.Abort() 来中止后续中间件或处理器的处理流程。
return 测试代码
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") } }
访问 http://127.0.0.1:8080 执行结果如下,浏览器输出 “hello,world!”
middleware1 start
middleware2 start
middleware3 start
process request
middleware3 end
middleware1 end
总结
中间件的执行顺序是按照注册顺序执行的,中间件可以通过 retrurn 来中止当前中间件的后续处理流程。
c.abort() +return 测试代码
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") } }
访问 http://127.0.0.1:8080 执行结果如下,浏览器无输出
middleware1 start
middleware2 start
middleware1 end
总结
中间件的执行顺序是按照注册顺序执行的,中间件可以通过 c.abort() + retrurn 来中止当前中间件,后续中间件和处理器的处理流程。