GIN框架中间件 C.NEXT() C.ABORT() C.SET() C.GET 跨中间件取值 GIN中间件中使用GOROUTINE


GIN框架中间件 C.NEXT() C.ABORT() C.SET() C.GET 跨中间件取值 GIN中间件中使用GOROUTINE

标签: Go

 
 

1. C.NEXT()演示

//HandlerFunc func indexHandler(c *gin.Context) { fmt.Println("index") c.JSON(http.StatusOK, gin.H{ "msg": "index", }) } //定义一个中间件 func m1(c *gin.Context) { fmt.Println("m1 in ...") // 计时 start := time.Now() c.Next() //调用后续的处理函数 //c.Abort() //阻止调用后续的处理函数 cost := time.Since(start) fmt.Printf("cost:%v\n", cost) fmt.Printf("m1 out ...") } func m2(c *gin.Context) { fmt.Println("m2 in ...") c.Next() //调用后续的处理函数 fmt.Println("m2 out ...") } func main() { r := gin.Default() //点GET查看源码发现 //GET(relativePath string, handlers ...HandlerFunc) IRoutes 另外,...表示 可传多个HandlerFunc类型的函数 //r.GET("/index", m1, indexHandler) //r.GET("/shop", m1, func(c *gin.Context) { // c.JSON(http.StatusOK, gin.H{ // "msg": "shop", // }) //}) //r.GET("/user", m1, func(c *gin.Context) { // c.JSON(http.StatusOK, gin.H{ // "msg": "user", // }) //}) // 点开Use 查看源码发现 Use(middleware ...HandlerFunc) IRoutes r.Use(m1, m2) //全局注册中间件函数m1 m2 /*访问/index 执行indexHandler之前 去执行注册的中间件 总的执行打印顺序是:m1 in -> m2 in -> index -> m2 out -> m1 out */ r.GET("/index", indexHandler) r.GET("/shop", func(c *gin.Context) { c.JSON(http.StatusOK, gin.H{ "msg": "shop", }) }) r.GET("/user", func(c *gin.Context) { c.JSON(http.StatusOK, gin.H{ "msg": "user", }) }) r.Run(":9090") } 
  • 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
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66

执行结果:
在这里插入图片描述
执行逻辑:
在这里插入图片描述
代码执行图解:
在这里插入图片描述

2. C.ABORT()演示:

上面代码把m2这里改成阻止,其他地方不变,如下图:
在这里插入图片描述
此时执行,控制台打印输出为:

m1 in  -> m2 in ->m2 out -> m1 out 
  • 1

代码执行图解:
在这里插入图片描述
当然,如果连 m2 out 都不想让它输出的话,那直接在c.Abort() 语句 后return就可以了,如下图:
在这里插入图片描述

3. C.SET() C.GET 跨中间件取值

//HandlerFunc func indexHandler(c *gin.Context) { fmt.Println("index") name, ok := c.Get("name") //取值 实现了跨中间件取值 if !ok{ name = "default user" } c.JSON(http.StatusOK, gin.H{ "msg": name, }) } //定义一个中间件 func m1(c *gin.Context) { fmt.Println("m1 in ...") // 计时 start := time.Now() //gin中间件中使用goroutine //当在中间件或handler中启动新的goroutine时,不能使用原始的上下文(c *gin.Context),必须使用其只读副本(c.Copy()) go otherFunc(c.Copy()) // 在otherFunc中只能使用c的拷贝 c.Next() //调用后续的处理函数 //c.Abort() //阻止调用后续的处理函数 cost := time.Since(start) fmt.Printf("cost:%v\n", cost) fmt.Printf("m1 out ...") } func m2(c *gin.Context) { fmt.Println("m2 in ...") c.Set("name", "tony") //可以在请求上下文里面设置一些值,然后其他地方取值 c.Abort() //阻止调用后续的处理函数 也就是 m2它自己走完就行 //return fmt.Println("m2 out ...") } // 自定义认证中间件 通过这种方式实现一些灵活的控制 func authMiddleware(doCheck bool)gin.HandlerFunc { //连接数据库 // 或其他一些准备工作 return func(c *gin.Context) { if doCheck { //这里存放具体的逻辑 // 是否登录的判断 // if 是登录用户 c.Next() // else // c.Abort() }else { c.Next() } } } func main() { r := gin.Default() // 点开Use 查看源码发现 Use(middleware ...HandlerFunc) IRoutes r.Use(m1, m2, authMiddleware(true)) //全局注册中间件函数m1 m2 authMiddleware /*访问/index 执行indexHandler之前 去执行注册的中间件 总的执行打印顺序是:m1 in -> m2 in -> index -> m2 out -> m1 out */ r.GET("/index", indexHandler) r.GET("/shop", func(c *gin.Context) { c.JSON(http.StatusOK, gin.H{ "msg": "shop", }) }) r.GET("/user", func(c *gin.Context) { c.JSON(http.StatusOK, gin.H{ "msg": "user", }) }) /* 路由组注册中间件方法1 routeGroup1 := r.Group("/xx", authMiddleware(true)){ routeGroup1.GET("/index", func(c *gin.Context) { c.JSON(http.StatusOK, gin.H{"msg": "routeGroup1"}) }) } 路由组注册中间件方法2 routeGroup2 := r.Group("/xx2") routeGroup2.Use(authMiddleware(true)){ routeGroup2.GET("/index", func(c *gin.Context) { c.JSON(http.StatusOK, gin.H{"msg": "routeGroup2"}) }) } */ r.Run(":9090") } 
  • 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
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
版权声明:本文为qq_37767455原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qq_37767455/article/details/104712028


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM