This commit is contained in:
oneao committed 2025-08-13 14:51:19 +08:00
1 parent ed8bee3d77
commit 7552670e4a
23 files changed
+380 -154

No files matched your search

@@ -37,47 +37,48 @@ func (b *BodyCache) Load(r *http.Request) ([]byte, error) {
return b.Data, b.Err
}
// Context 自定义请求上下文,组合 BodyCache
// Context 自定义请求上下文,封装了请求信息、响应写入器、路由参数、中间件处理等功能
type Context struct {
Writer http.ResponseWriter
Request *http.Request
Params map[string]string
Index int
Handlers []HandlerFunc
writer http.ResponseWriter // HTTP 响应写入器,用于构造响应数据
request *http.Request // HTTP 请求对象,包含请求相关信息
params map[string]string // 路由参数,如动态路径中的变量值
index int // 当前执行的中间件/处理函数索引,用于控制 Next 调用流程
handlers []HandlerFunc // 本次请求的中间件和最终处理函数链
BodyCache BodyCache // 请求体缓存
bodyCache BodyCache // 请求体缓存,确保请求体只读一次且可多次读取
keys map[string]interface{} // 用于存储请求生命周期内的自定义数据(如用户信息、orgId等)
}
// Next 执行下一个中间件或处理函数
func (c *Context) Next() {
c.Index++
if c.Index < len(c.Handlers) {
c.Handlers[c.Index](c)
c.index++
if c.index < len(c.handlers) {
c.handlers[c.index](c)
}
}
// Param 获取路由参数
func (c *Context) Param(key string) string {
return c.Params[key]
return c.params[key]
}
// Header 获取请求头
func (c *Context) Header(key string) string {
return c.Request.Header.Get(key)
return c.request.Header.Get(key)
}
// JSON 返回 JSON 格式响应
func (c *Context) JSON(statusCode int, data interface{}) {
c.Writer.Header().Set("Content-Type", "application/json; charset=utf-8")
c.Writer.WriteHeader(statusCode)
if err := json.NewEncoder(c.Writer).Encode(data); err != nil {
http.Error(c.Writer, err.Error(), http.StatusInternalServerError)
c.writer.Header().Set("Content-Type", "application/json; charset=utf-8")
c.writer.WriteHeader(statusCode)
if err := json.NewEncoder(c.writer).Encode(data); err != nil {
http.Error(c.writer, err.Error(), http.StatusInternalServerError)
}
}
// Body 方便读取请求体,实际调用 BodyCache 的 Load 方法
func (c *Context) Body() ([]byte, error) {
return c.BodyCache.Load(c.Request)
return c.bodyCache.Load(c.request)
}
// BindJSON 反序列化 JSON 请求体到 obj
@@ -91,8 +92,25 @@ func (c *Context) BindJSON(obj interface{}) error {
// PostForm 获取表单参数
func (c *Context) PostForm(key string) string {
if err := c.Request.ParseForm(); err != nil {
if err := c.request.ParseForm(); err != nil {
return ""
}
return c.Request.FormValue(key)
return c.request.FormValue(key)
}
// Set 存储键值对
func (c *Context) Set(key string, value interface{}) {
if c.keys == nil {
c.keys = make(map[string]interface{})
}
c.keys[key] = value
}
// Get 取值
func (c *Context) Get(key string) (interface{}, bool) {
if c.keys == nil {
return nil, false
}
val, ok := c.keys[key]
return val, ok
}