other: 优化 server 包 http 包装器使用体验

This commit is contained in:
kercylan98 2024-01-05 09:32:14 +08:00
parent 96953d74e2
commit 8eb9965839
2 changed files with 9 additions and 3 deletions

View File

@ -7,19 +7,19 @@ import (
// NewHttpContext 基于 gin.Context 创建一个新的 HttpContext // NewHttpContext 基于 gin.Context 创建一个新的 HttpContext
func NewHttpContext(ctx *gin.Context) *HttpContext { func NewHttpContext(ctx *gin.Context) *HttpContext {
hc := &HttpContext{ hc := &HttpContext{
ctx: ctx, Context: ctx,
} }
return hc return hc
} }
// HttpContext 基于 gin.Context 的 http 请求上下文 // HttpContext 基于 gin.Context 的 http 请求上下文
type HttpContext struct { type HttpContext struct {
ctx *gin.Context *gin.Context
} }
// Gin 获取 gin.Context // Gin 获取 gin.Context
func (slf *HttpContext) Gin() *gin.Context { func (slf *HttpContext) Gin() *gin.Context {
return slf.ctx return slf.Context
} }
// ReadTo 读取请求数据到指定结构体,如果失败则返回错误 // ReadTo 读取请求数据到指定结构体,如果失败则返回错误

View File

@ -146,3 +146,9 @@ func (slf *HttpRouter[Context]) Group(relativePath string, handlers ...HandlerFu
packer: slf.packer, packer: slf.packer,
} }
} }
// Use 将中间件附加到路由组。
func (slf *HttpRouter[Context]) Use(middleware ...HandlerFunc[Context]) *HttpRouter[Context] {
slf.group.Use(slf.handlesConvert(middleware)...)
return slf
}