当前位置:网站首页>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
边栏推荐
- 43. The sword refers to Offer 1 ~ 1 the number of occurrences of n integers (recursive, mathematics)
- 剑指 Offer 43. 1~n 整数中 1 出现的次数(递归、数学)
- FPGA中串口通信的时钟频率和波特率计数
- Periodic sharing of Alibaba Da Tao system model governance
- GIN Bind模式获取参数和表单验证
- 陈强教授《机器学习及R应用》课程 第十七章作业
- Draw a histogram with plot_hist_numeric()
- 乐东消防救援大队应邀为干部开展消防安全培训
- telnet+ftp 对设备进行 操控 和 升级
- 剑指 Offer 21. 调整数组顺序使奇数位于偶数前面(循环不变量)
猜你喜欢

快来扔鸡蛋。

Flutter entry and advanced tour (6) Layout Widget

5G China unicom repeater network management protocol real-time requirements

基于 R 语言的判别分析介绍与实践 LDA和QDA

Clock frequency and baud rate count for serial communication in FPGA

Final assignment of R language data analysis in a university

某高校的R语言数据分析期末作业

5G Unicom Network Management Design Ideas

5G China unicom 一般性异常处理

阿里大淘系模型治理阶段性分享
随机推荐
leetcode 20. Valid Parentheses 有效的括号(中等)
剑指offer,剪绳子2
卷积神经网络表征可视化研究综述(1)
Dry+Bean+Dataset R语言数据分析,报告英文
telnet+ftp to control and upgrade the device
5G Unicom Network Management Design Ideas
5G China unicom general exception handling
npm install失败
19、学习MySQL 索引
LeetCode 37. Solve Sudoku
陈强教授《机器学习及R应用》课程 第十八章作业
如何求最大公约数?
FFmpeg多媒体文件处理(ffmpeg处理流数据的基本概念)
JVM内存泄漏和内存溢出的原因
glibc 内存管理模型 释放 C库内存缓存
Data Mining-05
剑指 Offer 43. 1~n 整数中 1 出现的次数(递归、数学)
Professor Chen Qiang's "Machine Learning and R Application" course Chapter 15 Homework
ABP中的数据过滤器 (转载非原创)
Redis源码剖析之跳表(skiplist)