This commit is contained in:
oneao committed 2025-08-11 17:29:58 +08:00
1 parent 7c8e2906bc
commit 72a1a9184a
25 files changed
+744 -125

No files matched your search

@@ -0,0 +1,42 @@
package router
import (
"encoding/json"
"net/http"
)
type Context struct {
Writer http.ResponseWriter
Request *http.Request
Params map[string]string
index int
handlers []HandlerFunc
}
// Next 执行下一个中间件或处理函数
func (c *Context) Next() {
c.index++
for c.index < len(c.handlers) {
c.handlers[c.index](c)
c.index++
}
}
// Param 获取路由参数
func (c *Context) Param(key string) string {
return c.Params[key]
}
// Header 获取请求头
func (c *Context) Header(key string) string {
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)
}
}