当前位置:网站首页>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
边栏推荐
- 七夕力扣刷不停,343. 整数拆分(剑指 Offer 14- I. 剪绳子、剑指 Offer 14- II. 剪绳子 II)
- GIN文件上传与返回
- 陈强教授《机器学习及R应用》课程 第十四章作业
- Time series analysis course lab report
- 从NPU-SLAM-EDA技术分析
- Standing wave ratio calculation method
- FFmpeg多媒体文件处理(ffmpeg处理流数据的基本概念)
- Dry+Bean+Dataset R语言数据分析,报告英文
- NFS pays special attention to the problem of permissions
- gin的中间件和路由分组
猜你喜欢
![[HCIP Continuous Update] Principle and Configuration of IS-IS Protocol](/img/4f/035432ac84644c4bd46573aa0ab7cd.png)
[HCIP Continuous Update] Principle and Configuration of IS-IS Protocol

GIN文件上传与返回

Flutter Getting Started and Advanced Tour (8) Button Widget

Periodic sharing of Alibaba Da Tao system model governance

电脑重装系统后桌面图标如何调小尺寸

安踏携手华为运动健康共同验证冠军跑鞋 创新引领中国体育

ctfshow七夕杯2022

FFmpeg多媒体文件处理(ffmpeg处理流数据的基本概念)

FPGA-近日工作总结

Anta and Huawei Sports Health jointly verify the champion running shoes and lead Chinese sports with innovation
随机推荐
Periodic sharing of Alibaba Da Tao system model governance
Redis源码剖析之跳表(skiplist)
Time series analysis course lab report
陈强教授《机器学习及R应用》课程 第十八章作业
jenkins api创建自定义pipeline
剑指 Offer 57 - II. 和为s的连续正数序列(滑动窗口)
阿里大淘系模型治理阶段性分享
ftplib+ tqdm 上传下载进度条
正则表达式-re模块
Redis源码剖析之robj(redisObject)
5G China unicom 一般性异常处理
Ledong Fire Rescue Brigade was invited to carry out fire safety training for cadres
Redis源码剖析之字典(dict)
GIN Bind mode to get parameters and form validation
技术分享 | 接口自动化测试如何处理 Header cookie
Record the system calls and C library functions used in this project-2
[极客大挑战 2019]Upload
GIN file upload and return
19、学习MySQL 索引
NFS pays special attention to the problem of permissions