当前位置:网站首页>gin's middleware and routing grouping

gin's middleware and routing grouping

2022-08-09 13:51:00 One Leaf Knows Autumn @qqy

前言

感谢开源项目gin-vue-admin,以及1010工作室的教程,项目文档
我只是在跟着学习,然后记录下笔记而已,可能会有新的代码加入,但是本质还是跟着学习的一个过程.

什么是路由分组

对router创建Group就是分组,There will be a unified prefix and unified middleware for the same group.
Indeed this is very important to project management

写法:router:=gin.Default()

v1 := router.Group("/v1")
v1.POST("/login", loginEndpoint)
v1.POST("/submit", submitEndpoint)
v1.POST("/read", readEndpoint)

这里的v1can be without underscore,也是被允许的
在main.goBuild the following code in

package main

import (
	"fmt"
	"github.com/gin-gonic/gin"
)

func main() {
    
	r := gin.Default() //启动gin路由,携带基础中间件启动 logger and recovery (crash-free) 中间件
	v1 := r.Group("v1")
	v1.GET("test", func(c *gin.Context){
    
		fmt.Println("I am inside the grouping method")
	})
	r.Run(":1010") // listen and serve on 0.0.0.0:8080
}

在postmanMedium component request,Directly with the groupv1进行请求.
在这里插入图片描述
在这里插入图片描述
修改代码如下,增加返回

func main() {
    
	r := gin.Default() //启动gin路由,携带基础中间件启动 logger and recovery (crash-free) 中间件
	v1 := r.Group("v1")
	v1.GET("test", func(c *gin.Context){
    
		fmt.Println("I am inside the grouping method")
		c.JSON(200, gin.H{
    
			"success":true,
		})
	})
	r.Run(":1010") // listen and serve on 0.0.0.0:8080
}

postman再次发起请求
在这里插入图片描述
在这里插入图片描述
值得注意的是,Here, although only a request is implemented on the creation of a routing group,But actually go ahead and create other requests,For example, used here yestest,there could be othertest1、test2、test3等等,But the common feature is that they all have a common prefixv1

Why group

  • Packet routing structure can be more clear
  • Easier routing management

什么是中间件&使用

A sequence of actions that take place before and after the method in which the request reaches the route
在GINProvide us with the basis of routing,In fact, two basic middleware have been configured internally,看下GIN.Default的源码

// Default returns an Engine instance with the Logger and Recovery middleware already attached.
func Default() *Engine {
    
	debugPrintWARNINGDefault()
	engine := New()
	engine.Use(Logger(), Recovery())
	return engine
}

Here it is usedLogger()中间件和Recovery()中间件.
通过Use()load middleware,If we write middleware,The same way is used when using loading

创建中间件

func middel()gin.HandlerFunc{
    
	return func(c *gin.Context) {
    
		fmt.Println("i before the method")
		c.Next()
		fmt.Println("I am after the method")
	}
}

在v1Mount middleware on the group

func main() {
    
	r := gin.Default() //启动gin路由,携带基础中间件启动 logger and recovery (crash-free) 中间件
	v1 := r.Group("v1").Use(middel())
	v1.GET("test", func(c *gin.Context){
    
		fmt.Println("I am inside the grouping method")
		c.JSON(200, gin.H{
    
			"success":true,
		})
	})
	r.Run(":1010") // listen and serve on 0.0.0.0:8080
}

If multiple middleware,可以Use(middel1(), middel2()),也可以Use(middel1()).Use(middel2()).
By the above method, thev1Mount the middleware,也可以说是v1With the middleware.

postman组织请求
在这里插入图片描述
在这里插入图片描述
It's not hard to find by looking at the print,中间件中,c.Next()The former is before entering the inside of the processing request method,而c.Next()Then after processing the request method,We can realize by means of middleware in processing the request before and after the implementation of different functions
When there are multiple middleware,structure similar to an onion
在这里插入图片描述
That is, execute the first middleware firstmiddel1前,执行middel2前,执行请求方法,执行middel2后,执行middel1后,Similar to an onion inserted.

Add another middleware,看下示例:

func middel()gin.HandlerFunc{
    
	return func(c *gin.Context) {
    
		fmt.Println("我在方法1前")
		c.Next()
		fmt.Println("我在方法1后")
	}
}
func middeltwo()gin.HandlerFunc{
    
	return func(c *gin.Context) {
    
		fmt.Println("我在方法2前")
		c.Next()
		fmt.Println("我在方法2后")
	}
}

func main() {
    
	r := gin.Default() //启动gin路由,携带基础中间件启动 logger and recovery (crash-free) 中间件
	v1 := r.Group("v1").Use(middel(), middeltwo())
	v1.GET("test", func(c *gin.Context){
    
		fmt.Println("I am inside the grouping method")
		c.JSON(200, gin.H{
    
			"success":true,
		})
	})
	r.Run(":1010") // listen and serve on 0.0.0.0:8080
}

组织postman请求

在这里插入图片描述
This clearly shows how multiple middleware is used,And inner is how to operate

原网站

版权声明
本文为[One Leaf Knows Autumn @qqy]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/221/202208091244282064.html