• 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
    ", 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
    ", 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
  • 相关阅读:
    Linux 下面安装 nginx 以及进行TCP反向代理、负载均衡的过程
    AMD全新32核线程撕裂者GeekBench跑分曝光:超2950X近一倍
    Win7 & VS2013 编译 WebKit 总结
    窗宽窗位与其处理方法
    编写Delphi控件属性Stored和Default的理解及应用
    vSphere、Hyper-V与XenServer 你选哪个?
    写给那些常年战痘的痘友们~~~
    怎么解决xp系统不能安装NET Framework4.0?
    Kali Linux渗透基础知识整理(四):维持访问
    解决vista和win7在windows服务中交互桌面权限问题:穿透Session 0 隔离
  • 原文地址:https://www.cnblogs.com/lovezbs/p/14188340.html
Copyright © 2020-2023  润新知