当前位置:网站首页>【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实现
边栏推荐
- WordPress登录页面美化插件:Login Designer推荐
- msf cs openssl流量加密
- Jerry's acquisition of ble to check the causes of abnormal conditions such as abnormal code reset [chapter]
- Exness: the progress of Russia Ukraine negotiations is limited, and the RBA's decision remains unchanged
- [issue 31] 360 background development practice experience - two rounds of technical aspects
- AcWing 1944. 记录保存(哈希,STL)
- CPU负载怎么理解
- 如何养成一个好习惯?靠毅力?靠决心?都不是!
- WP Super Cache静态缓存插件简明使用教程
- Don't be a fake worker
猜你喜欢

Characteristics and classification of creation mode (single case, factory)

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

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

Cube 技术解读 | Cube 渲染设计的前世今生

985 University doctors became popular because of their thanks in classical Chinese! The tutor commented that he not only wrote well in sci

找到自己的优势,才能干活不累,事半功倍!

設置默認收貨地址【項目 商城】

Surrounddepth: self supervised multi camera look around depth estimation

使用Yolov5训练好模型调用电脑自带摄像头时出现问题:TypeError: argument of type “int‘ is not iterable的解决方法

Adapter mode -- can you talk well?
随机推荐
Summary of English thesis reading knowledge
Digital collection app applet official account source code
What is the best annuity insurance product in 2022?
WordPress登录页面定制插件推荐
MyCat-分库分表
Jerry's blepr0 and PR1 are used as ordinary IO ports [chapter]
Can't you be free without wealth?
企业微信小程序避坑指南,欢迎补充。。。
mongoDB 使用
启牛帮开通的证券账户是安全可信的吗?
設置默認收貨地址【項目 商城】
Problems encountered when using nailing intranet to penetrate and upload PHP projects
木瓜移动CFO刘凡 释放数字时代女性创新力量
收货地址列表展示【项目 商城】
The application of the spingboot+quartrz production environment supports distributed, custom corn, reflective execution of multiple tasks
Processing of uci-har datasets
Etcd的运行时重配置
AI security and Privacy Forum issue 11 - stable learning: finding common ground between causal reasoning and machine learning
my.cnf中 [mysql]与[mysqld] 的区别 引起的binlog启动失败的问题
没有财富就不能自由吗?