使用 fasthttp 替代 net/http,实现 Phase 2 核心模块: - HTTP 服务器:fasthttp.Server 配置超时和连接限制 - 路由系统:fasthttp/router 基于 radix tree 匹配 - 静态文件服务:安全检查、索引文件支持 - 日志系统:zerolog 结构化日志 - 中间件框架:链式组合接口 - 虚拟主机管理:按 Host 头选择处理器 Co-Authored-By: Claude <noreply@anthropic.com>
28 lines
650 B
Go
28 lines
650 B
Go
package middleware
|
|
|
|
import "github.com/valyala/fasthttp"
|
|
|
|
// Middleware 中间件接口
|
|
type Middleware interface {
|
|
Name() string
|
|
Process(next fasthttp.RequestHandler) fasthttp.RequestHandler
|
|
}
|
|
|
|
// Chain 中间件链
|
|
type Chain struct {
|
|
middlewares []Middleware
|
|
}
|
|
|
|
// NewChain 创建中间件链
|
|
func NewChain(middlewares ...Middleware) *Chain {
|
|
return &Chain{middlewares: middlewares}
|
|
}
|
|
|
|
// Apply 应用中间件链(逆序包装)
|
|
func (c *Chain) Apply(final fasthttp.RequestHandler) fasthttp.RequestHandler {
|
|
handler := final
|
|
for i := len(c.middlewares) - 1; i >= 0; i-- {
|
|
handler = c.middlewares[i].Process(handler)
|
|
}
|
|
return handler
|
|
} |