当前位置:网站首页>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
边栏推荐
- Standing wave ratio calculation method
- Redis源码剖析之字典(dict)
- Uni - app - uview Swiper shuffling figure component, click on the links to jump (click to get the item after the row data, remove data operation)
- Deep Learning Based on R Language - Simple Regression Case
- FPGA-近日工作总结
- 5G China unicom 一般性异常处理
- Oracle Recovery Tools修复空闲坏块
- Data Mining-06
- 5G China unicom AP:B SMS ASCII 转码要求
- FPGA-在ISE中错误总结(更新中)
猜你喜欢
FFmpeg多媒体文件处理(ffmpeg处理流数据的基本概念)
Jenkins API groovy calling practice: Jenkins Core Api & Job DSL to create a project
剑指offer,剪绳子2
Redis源码剖析之字典(dict)
电脑重装系统后桌面图标如何调小尺寸
第六届”蓝帽杯“全国大学生网络安全技能大赛 半决赛
uni-app - uview Swiper 轮播图组件点击跳转链接(点击后拿到 item 行数据, 取出数据做操作)
ctfshow七夕杯2022
Map mixed density function and quantile added line
GIN文件上传与返回
随机推荐
【奖励公示】第23期 2022年7月奖励名单公示:社区明星评选 | 新人奖 | 博客同步 | 推荐奖
19、学习MySQL 索引
Data Mining-05
第六届”蓝帽杯“全国大学生网络安全技能大赛 半决赛
5G China unicom general exception handling
R 语言 2010.1至2021.12艾滋病每月发病人数 时间序列分析
WSA工具箱安装应用商店提示无法工作怎么解决?
The sword refers to the offer, cuts the rope 2
Map mixed density function and quantile added line
novel research
联通网管协议框图
SQL Server查询优化 (转载非原创)
Rmarkdown Tutorial
万物皆可柯里化的 Ramda.js
5G China unicom 一般性异常处理
基于 R 语言的判别分析介绍与实践 LDA和QDA
jenkins api create custom pipeline
农村区县域农业电商如何做?数字化转型如何进行?
问题系列-如何修改或更新localhost里的值
为什么文字不贴合边