当前位置:网站首页>【Go】Gin源码解读
【Go】Gin源码解读
2022-06-11 11:13:00 【curd_boy】
1.中间件
中间件的实现 依照设计模式中责任链模式,依次调用当前路由 注册的中间件
gin.go
// HandlerFunc defines the handler used by gin middleware as return value.
type HandlerFunc func(*Context)
// HandlersChain defines a HandlerFunc array.
type HandlersChain []HandlerFunc
context.go
// Context is the most important part of gin. It allows us to pass variables between middleware,
// manage the flow, validate the JSON of a request and render a JSON response for example.
type Context struct {
writermem responseWriter
Request *http.Request
Writer ResponseWriter
Params Params
handlers HandlersChain
index int8
fullPath string
engine *Engine
params *Params
// This mutex protect Keys map
mu sync.RWMutex
// Keys is a key/value pair exclusively for the context of each request.
Keys map[string]interface{
}
// Errors is a list of errors attached to all the handlers/middlewares who used this context.
Errors errorMsgs
// Accepted defines a list of manually accepted formats for content negotiation.
Accepted []string
// queryCache use url.ParseQuery cached the param query result from c.Request.URL.Query()
queryCache url.Values
// formCache use url.ParseQuery cached PostForm contains the parsed form data from POST, PATCH,
// or PUT body parameters.
formCache url.Values
// SameSite allows a server to define a cookie attribute making it impossible for
// the browser to send this cookie along with cross-site requests.
sameSite http.SameSite
}
c.Next()
// Next should be used only inside middleware.
// It executes the pending handlers in the chain inside the calling handler.
// See example in GitHub.
func (c *Context) Next() {
c.index++
for c.index < int8(len(c.handlers)) {
c.handlers[c.index](c)
c.index++
}
}
Next() 这个方法只在中间件中使用有意义,调用后马上执行各个 handler(这些 handler 中包含你注册在路由上的方法),最后会返回到中间件。
c.Abort()
// Abort prevents pending handlers from being called. Note that this will not stop the current handler.
// Let's say you have an authorization middleware that validates that the current request is authorized.
// If the authorization fails (ex: the password does not match), call Abort to ensure the remaining handlers
// for this request are not called.
func (c *Context) Abort() {
c.index = abortIndex
}
让ctx当前index指向最后,直接返回
路由
context实现
边栏推荐
- Etcd的运行时重配置
- nft数字藏品系统开发搭建流程
- WordPress重新生成特色图像插件:Regenerate Thumbnails
- National multi-year solar radiation spatial distribution data 1981-2022, temperature distribution data, evapotranspiration data, evaporation data, rainfall distribution data, sunshine data, wind speed
- Source code construction of digital collection system
- 李飞飞:我更像物理学界的科学家,而不是工程师|深度学习崛起十年
- AcWing 1353. Ski resort design (greedy)
- Vscade -- vscode multi window name is configured as project name
- Exploration of kangaroo cloud data stack on spark SQL optimization based on CBO
- Don't be a fake worker
猜你喜欢
![Set the default receiving address [project mall]](/img/eb/2864b124b66d01849315a367948ed4.png)
Set the default receiving address [project mall]

Don't be a fake worker

Surrounddepth: self supervised multi camera look around depth estimation

National multi-year solar radiation spatial distribution data 1981-2022, temperature distribution data, evapotranspiration data, evaporation data, rainfall distribution data, sunshine data, wind speed

Command mode - attack, secret weapon

Typeerror: argument of type "Int 'is not Iterable

如何养成一个好习惯?靠毅力?靠决心?都不是!

外观模式--在各种套餐中早就用到啦!

全国多年太阳辐射空间分布数据1981-2022年、气温分布数据、蒸散量数据、蒸发量数据、降雨量分布数据、日照数据、风速数据

Use pydub to modify the bit rate of the wav file, and an error is reported: c:\programdata\anaconda3\lib\site packages\pydub\utils py:170: RuntimeWarning:
随机推荐
设置默认收货地址【项目 商城】
AI security and Privacy Forum issue 11 - stable learning: finding common ground between causal reasoning and machine learning
装饰模式--小美的生日蛋糕
MyCat-分库分表
Définir l'adresse de réception par défaut [Centre commercial du projet]
不做伪工作者
灵动边栏(Widget)插件:MO Widgets
Problems encountered when using nailing intranet to penetrate and upload PHP projects
AcWing 1353. Ski resort design (greedy)
Digital collection app applet official account source code
Jerry's ble chip power supply range and anti burn chip measures [chapter]
沒有財富就不能自由嗎?
使用国产MCU(国民技术 N32G031F8S7) 实现 PWM+DMA 控制 WS2812
Jerry's ble spp open pin_ Code function [chapter]
msf cs openssl流量加密
Want to be iron man? It is said that many big men use it to get started
my. Binlog startup failure caused by the difference between [mysql] and [mysqld] in CNF
985 University doctors became popular because of their thanks in classical Chinese! The tutor commented that he not only wrote well in sci
数字藏品系统app源码
Introduction to thread pool: ThreadPoolExecutor