当前位置:网站首页>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
边栏推荐
- 剑指 Offer 43. 1~n 整数中 1 出现的次数(递归、数学)
- 七夕力扣刷不停,343. 整数拆分(剑指 Offer 14- I. 剪绳子、剑指 Offer 14- II. 剪绳子 II)
- 【FPGA教程案例48】图像案例8——基于FPGA的RGB图像转化为HSV图像的实现,通过MATLAB进行辅助验证
- FFmpeg multimedia file processing (FFMPEG logging system)
- Jenkins API groovy调用实践: Jenkins Core Api & Job DSL创建项目
- Use RecyclerView to implement three-level collapsed list
- 5G China unicom 一般性异常处理
- How to solve the 0x80070005 error when the computer is reinstalled and the system is restored
- Flutter Getting Started and Advanced Tour (8) Button Widget
- Clock frequency and baud rate count for serial communication in FPGA
猜你喜欢
FFmpeg多媒体文件处理(ffmpeg操作目录及list的实现)
GIN a preliminary study, the environment is installed
Flutter entry and advanced tour (6) Layout Widget
某高校的R语言数据分析期末作业
Oracle Recovery Tools修复空闲坏块
Do you know the difference between comments, keywords, and identifiers?
电脑重装系统还原0x80070005错误如何解决
5G China unicom AP:B SMS ASCII 转码要求
5G China unicom 一般性异常处理
Unicom network management protocol block diagram
随机推荐
剑指offer,剪绳子2
从NPU-SLAM-EDA技术分析
LeetCode 37.解数独
Microsoft 10/11 命令行打开系统设置页(WUAP,!WIN32)
R language kaggle game data exploration and visualization
Final assignment of R language data analysis in a university
GIN Bind模式获取参数和表单验证
陈强教授《机器学习及R应用》课程 第十七章作业
万物皆可柯里化的 Ramda.js
ArcEngine(九)图形绘制
JVM常用监控工具解释以及使用
GIN初探,环境安装
剑指 Offer 21. 调整数组顺序使奇数位于偶数前面(循环不变量)
[HCIP Continuous Update] Principle and Configuration of IS-IS Protocol
5G China unicom AP:B SMS ASCII Transcoding Requirements
电脑重装系统后桌面图标如何调小尺寸
Ledong Fire Rescue Brigade was invited to carry out fire safety training for cadres
ARM板卡增加路由功能
LnReader编译
Data Mining-06