济南网站优化排名,公众号 创意名字,网站建设优化开发公司排名,江西营销网站建设一.gin中的context gin.Context
1.概念 在 Gin 中#xff0c;Context 是一个非常重要的概念#xff0c;它是Gin的核心结构体之一,用于处理 HTTP 请求和响应,在 Gin 的处理流程中#xff0c;Context 贯穿整个处理过程#xff0c;用于传递请求和响应的信息Gin 的 Context 是…一.gin中的context gin.Context
1.概念 在 Gin 中Context 是一个非常重要的概念它是Gin的核心结构体之一,用于处理 HTTP 请求和响应,在 Gin 的处理流程中Context 贯穿整个处理过程用于传递请求和响应的信息Gin 的 Context 是一个结构体类型核心定义如下 type Context struct {// 定义了一些私有成员变量用于存储请求和响应等信息...writermem responseWriterRequest *http.Request // 保存request请求Writer ResponseWriter // 回写response Params Paramshandlers HandlersChain // 该次请求所有的中间件函数和处理函数index int8 // HandlersChain的下标用来调用某个具体的HandlerFuncfullPath string // 请求的url路径engine *Engine // 对server Engine的引用Keys map[string]any // 用于上下游之间传递参数params *ParamsskippedNodes *[]skippedNode
mu sync.RWMutex// Keys is a key/value pair exclusively for the context of each request.Keys map[string]any//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 caches the query result from c.Request.URL.Query().queryCache url.Values// formCache caches c.Request.PostForm, which 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...// ...
}
2.中间件 Context 中封装了原生的 Go HTTP 请求和响应对象同时还提供了一些处理请求和生成响应的各种方法用于获取请求和响应的信息、设置响应头、设置响应状态码等操作.在Gin中Context 是通过中间件来传递的,在处理 HTTP 请求时Gin 会依次执行注册的中间件每个中间件可以对 Context 进行一些操作然后将 Context 传递给下一个中间件。 例如下面是一个简单的中间件用于在请求头中设置一个自定义的 X-Request-ID: func RequestIDMiddleware() gin.HandlerFunc {return func(c *gin.Context) {requestID : generateRequestID()c.Request.Header.Set(X-Request-ID, requestID)c.Next() // 将 Context 传递给下一个中间件}
} 在上面的中间件中生成了一个请求 ID然后将其设置到请求头中,接着,调用 c.Next() 方法将Context 传递给下一个中间件,这样,下一个中间件就可以通过 c.Request.Header.Get(X-Request-ID) 获取到这个请求 ID 从上面可以知道:一次请求的所有中间件函数和请求处理函数都在Context.handlers中,因此,当请求到来时只需要依次调用Context.handlers中的所有HandlerFunc即可,这就是调用中间件中的一个最重要的函数Next()定义如下: func (c *Context) Next() {c.index // 依次遍历所有的中间件函数并调用他们for c.index int8(len(c.handlers)) {c.handlers[c.index](c) c.index}
} 除了顺序执行所有中间件还要有在某个中间件函数中终止处理的能力,比如某个中间件负责权限校验如果用户的校验没通过直接返回Not Authorized跳过后续的处理,这个是通过Abort函数实现的: func (c *Context) Abort() {c.index abortIndex // abortIndex是个常量63
} abort()的原理非常简单: 直接让c.Index等于最大值这样剩余的中间件函数都没机会执行,从这个函数中可以看出中间件的数量是有上限的上限就是63个 正常的执行流是依次调用各个中间件函数但是如果在某个中间件函数中显式调用了Next()会先执行后续的中间件执行完成了再返回当前中间件继续执行流程如下 3.参数传递 Context中有成员Keys上游需要传递的变量可以放在里面下游处理时再从里面取出来实现上下游参数传递,对应Set()和Get()方法 func (c *Context) Set(key string, value any) {c.mu.Lock() // 用来保护c.Keys并发安全if c.Keys nil {c.Keys make(map[string]any)}c.Keys[key] valuec.mu.Unlock()
}func (c *Context) Get(key string) (value any, exists bool) {c.mu.RLock()value, exists c.Keys[key]c.mu.RUnlock()return
} Get()函数还有很多衍生函数,比如GetStringGetStringMapStringGetStringMapStringSlice等都是在Get的基础上将数据转换成我们需要的类型再返回 4.参数绑定 请求放到Context.Request里需要解析成结构体或map才好在业务代码里使用,这一部分是通过Bind()系列函数实现的,Bind()系列函数的作用就是根据request的数据类型将其解析到结构体或map里,简单的看一个参数绑定的实现 func (c *Context) ShouldBindWith(obj any, b binding.Binding) error {return b.Bind(c.Request, obj) // 底层调用的是binding相关的函数
} 针对不同的请求gin提供了很多函数用于解析对应的参数常用的函数如下: Param(key string) string // 用于获取url参数比如/welcome/:user_id中的user_id// 获取GET请求中携带的参数
GetQueryArray(key string) ([]string, bool)
GetQuery(key string)(string, bool)
Query(key string) string
DefaultQuery(key, defaultValue string) string// 获取POST请求参数
GetPostFormArray(key string) ([]string, bool)
PostFormArray(key string) []string
GetPostForm(key string) (string, bool)
PostForm(key string) string
DefaultPostForm(key, defaultValue string) string// data binding
Bind (obj interface {}) error // bind data according to Content-Type
BindJSON(obj interface{}) error
BindQuery(obj interface{}) errorShouldBind(obj interface{}) error
ShouldBindJSON(obj interface{}) error
ShouldBindQuery(obj interface{}) error
... 其中Bind相关的函数是一种比较通用的方法它允许我们将请求参数填充到一个map或struct中这样在后续请求处理时能够方便的使用传入的参数。Bind相关的函数分为三大类 // 1. Bind函数
Bind (obj interface {}) error // 根据请求中content-type的类型来选择对应的具体Bind函数,底层调用的是BindJson, BindQuery这种具体的Bind函数// 2. BindXXX()具体的Bind函数用于绑定一种参数类型底层调用MustBindWith或者ShouldBindWith
BindJSON(obj interface{}) error
BindQuery(obj interface{}) error// 3. 最底层的基础函数
MustBindWith(obj any, b binding.Binding) error // 当出现参数校验问题时会直接返回400底层仍然是ShouldBindWith
ShouldBindWith(obj any, b binding.Binding) error 其中各种类型的Bind函数最底层的调用都是ShouldBindWith()函数该函数有两个参数第一个参数obj为需要将参数填充进去的对象本文称为填充对象第二个参数为binding.Binding类型该类型的定义在package binding中如下: type Binding interface {Name() stringBind(*http.Request, any) error
} Bingding为一个接口提供了Name()和Bind()两个函数Name()负责返回对应的Bind类型比如JSONXML等Bind()函数则负责实现具体类型的参数绑定。为了完成常用参数类型的绑定gin给每种参数类型都定义了一个类并实现Binding接口具体实现了Binding接口的类如下 // 实现Binding接口的具体类
var (JSON jsonBinding{}XML xmlBinding{}Form formBinding{}Query queryBinding{}FormPost formPostBinding{}FormMultipart formMultipartBinding{}ProtoBuf protobufBinding{}MsgPack msgpackBinding{}YAML yamlBinding{}Uri uriBinding{}Header headerBinding{}TOML tomlBinding{}
) 这样,每当一个HTTP请求到来的时候,用户可以直接调用Bind()函数,Bind()函数可以通过content-type选择对应的实现了Bind()方法的对应类的实例,调用其Bind()方法,完成参数绑定. 常用的HTTP请求参数类型为HTTP GET query参数类型和HTTP POST json参数类型,以这两个为例看一下参数绑定的一些细节: BindJSON: jsonBinding对Binding的实现如下: json的参数绑定比较简单使用json.Decoder完成 func (jsonBinding) Name() string {return json
}func (jsonBinding) Bind(req *http.Request, obj any) error {if req nil || req.Body nil {return errors.New(invalid request)}return decodeJSON(req.Body, obj)
}func decodeJSON(r io.Reader, obj any) error {decoder : json.NewDecoder(r) // 使用json.Decoder对json进行解析if EnableDecoderUseNumber {decoder.UseNumber()}if EnableDecoderDisallowUnknownFields {decoder.DisallowUnknownFields()}if err : decoder.Decode(obj); err ! nil {return err}return validate(obj) // 参数校验
}
BindQuery: 一个HTTP GET请求的query参数在go中可以通过request.URL.Query()获取到获取到的query参数类型为url.Values,因此BindQuery()首先获取到url.Values类型的query参数然后设置对应的值,BindQuery()根据传进来的要将参数填充进去的对象类型本文称为填充对象是map类型还是struct ptr分成了两个填充函数: /* mapFormByTag是queryBinding.Bind()的底层核心函数
* ptr: 填充对象可能是map或strcut的指针
* form: url.Values包含所有query参数
* tag: 值为form
*/
func mapFormByTag(ptr any, form map[string][]string, tag string) error {// Check if ptr is a mapptrVal : reflect.ValueOf(ptr)var pointed anyif ptrVal.Kind() reflect.Ptr {ptrVal ptrVal.Elem()pointed ptrVal.Interface()}if ptrVal.Kind() reflect.Map ptrVal.Type().Key().Kind() reflect.String {if pointed ! nil {ptr pointed}return setFormMap(ptr, form) // 如果填充对象是map类型}return mappingByPtr(ptr, formSource(form), tag) // 填充对象是ptr struct类型
} 接下来分别看一下两个核心的填充函数setFormMap()和mappingByPtr() // setFormMap本身比较简单因为query参数url.Values是map[string][]string类型的别称本身就是map[string][]string类型只需要判断填充对象是map[string]string还是map[string][]string类型
func setFormMap(ptr any, form map[string][]string) error {el : reflect.TypeOf(ptr).Elem() // 因为ptr本身是map类型Elem返回该map的value值// 如果map填充对象的value值为[]string类型直接填充if el.Kind() reflect.Slice {ptrMap, ok : ptr.(map[string][]string)if !ok {return ErrConvertMapStringSlice}for k, v : range form {ptrMap[k] v}return nil}// 否则,map填充对象为map[string]string类型取url.Values每个key对应的value值类型为[]string的最后一个元素填充到填充对象中ptrMap, ok : ptr.(map[string]string)if !ok {return ErrConvertToMapString}for k, v : range form {ptrMap[k] v[len(v)-1] // pick last}return nil
} mappingByPtr()的逻辑比较复杂核心是遍历struct的每一个字段获取该struct字段的json tag如果tag的值跟某一个url.Values的key的值相等填充对应的值 func mappingByPtr(ptr any, setter setter, tag string) error {_, err : mapping(reflect.ValueOf(ptr), emptyField, setter, tag)return err
}/* mapping是一个递归函数因为struct填充对象有可能嵌套了ptr成员
* value填充对象
* fieldstruct填充对象的某个具体成员变量
* setter内部包含了url.Values
* tag: 等于form
*/
func mapping(value reflect.Value, field reflect.StructField, setter setter, tag string) (bool, error) {if field.Tag.Get(tag) - { // just ignoring this fieldreturn false, nil}vKind : value.Kind()// 如果填充对象是ptr获取其指向的对象递归调用mappingif vKind reflect.Ptr {var isNew boolvPtr : valueif value.IsNil() {isNew truevPtr reflect.New(value.Type().Elem())}isSet, err : mapping(vPtr.Elem(), field, setter, tag) if err ! nil {return false, err}if isNew isSet {value.Set(vPtr)}return isSet, nil}// 递归到最底层每个value都是StructField类型开始填充值if vKind ! reflect.Struct || !field.Anonymous {ok, err : tryToSetValue(value, field, setter, tag)if err ! nil {return false, err}if ok {return true, nil}}// 如果填充对象是struct针对每一个struct field递归调用mappingif vKind reflect.Struct {tValue : value.Type()var isSet boolfor i : 0; i value.NumField(); i {sf : tValue.Field(i)if sf.PkgPath ! !sf.Anonymous { // unexportedcontinue}ok, err : mapping(value.Field(i), sf, setter, tag)if err ! nil {return false, err}isSet isSet || ok}return isSet, nil}return false, nil
} 总结: Gin通过区分不同的参数类型每种参数类型实现了统一的Bind()函数来完成对应的参数绑定用户只需要调用统一的函数不用关系底层实现细节即可完成参数绑定 从上面介绍可以知道: 在 Gin 中Context 是通过中间件来传递的,每个中间件都可以对 Context 进行一些操作然后将其传递给下一个中间件最终由处理函数来使用Context中的信息进行处理 二.gin.Context和context.Context的区别 Gin 的 gin.Context 和 Go标准库中的 context.Context 是两个不同的概念用途也不同 gin.Context是 Gin 框架中的一个结构体类型用于封装 HTTP 请求和响应的信息以及提供一些方法用于获取请求和响应的信息、设置响应头、设置响应状态码等操作,gin.Context 只在 Gin 框架内部使用用于处理 HTTP 请求和响应,它与 HTTP 请求和响应一一对应每个 HTTP 请求都会创建一个新的 gin.Context 对象并在处理过程中传递context.Context是 Go 标准库中的一个接口类型用于在 Goroutine 之间传递上下文信息,context.Context 可以在 Goroutine 之间传递信息例如传递请求 ID、数据库连接、请求超时等信息,context.Context 的具体实现是由各种库和框架提供的例如 Gin 框架中也提供了一个 gin.Context 的实现用于在 Gin 框架中使用 context.Context 总之,gin.Context是Gin框架中用于处理HTTP请求和响应的上下文对象,而context.Context是Go标准库中用于在Goroutine之间传递上下文信息的接口类型,在使用 Gin 框架时可以通过 gin.Context的Request.Context()方法来访问 context.Context对象从而在 Gin 框架中使用上下文信息 例如可以在中间件中设置一个请求 ID并将其保存在 context.Context 中然后在处理函数中获取这个请求 ID,下面是一个示例 func RequestIDMiddleware() gin.HandlerFunc {return func(c *gin.Context) {requestID : generateRequestID()ctx : context.WithValue(c.Request.Context(), requestID, requestID)c.Request c.Request.WithContext(ctx)c.Next()}
}func HelloHandler(c *gin.Context) {requestID : c.Request.Context().Value(requestID).(string)c.String(http.StatusOK, Hello, request ID %s, requestID)
} 在上面的示例中,中间件 RequestIDMiddleware会生成一个请求ID,并将其保存在context.Context中,然后将这个 context.Context 对象保存到 http.Request 中,在处理函数 HelloHandler 中可以通过 c.Request.Context().Value(requestID) 来获取这个请求 ID 总之在 Gin 中可以通过 gin.Context 来访问 context.Context从而在 Gin 框架中传递上下文信息,开发者可以在中间件中设置 context.Context然后在处理函数中获取这个上下文信息