使用 fasthttp 替代 net/http,实现 Phase 2 核心模块: - HTTP 服务器:fasthttp.Server 配置超时和连接限制 - 路由系统:fasthttp/router 基于 radix tree 匹配 - 静态文件服务:安全检查、索引文件支持 - 日志系统:zerolog 结构化日志 - 中间件框架:链式组合接口 - 虚拟主机管理:按 Host 头选择处理器 Co-Authored-By: Claude <noreply@anthropic.com>
63 lines
1.3 KiB
Go
63 lines
1.3 KiB
Go
package server
|
|
|
|
import (
|
|
"github.com/valyala/fasthttp"
|
|
)
|
|
|
|
// VHostManager 虚拟主机管理器
|
|
type VHostManager struct {
|
|
hosts map[string]*VirtualHost // 按 server_name 索引
|
|
defaultHost *VirtualHost // 默认主机
|
|
}
|
|
|
|
// VirtualHost 虚拟主机
|
|
type VirtualHost struct {
|
|
name string
|
|
handler fasthttp.RequestHandler
|
|
}
|
|
|
|
// NewVHostManager 创建虚拟主机管理器
|
|
func NewVHostManager() *VHostManager {
|
|
return &VHostManager{
|
|
hosts: make(map[string]*VirtualHost),
|
|
}
|
|
}
|
|
|
|
// AddHost 添加虚拟主机
|
|
func (v *VHostManager) AddHost(name string, handler fasthttp.RequestHandler) {
|
|
v.hosts[name] = &VirtualHost{
|
|
name: name,
|
|
handler: handler,
|
|
}
|
|
}
|
|
|
|
// SetDefault 设置默认主机
|
|
func (v *VHostManager) SetDefault(handler fasthttp.RequestHandler) {
|
|
v.defaultHost = &VirtualHost{
|
|
name: "default",
|
|
handler: handler,
|
|
}
|
|
}
|
|
|
|
// Handler 返回虚拟主机选择器
|
|
func (v *VHostManager) Handler() fasthttp.RequestHandler {
|
|
return func(ctx *fasthttp.RequestCtx) {
|
|
host := string(ctx.Host())
|
|
// 去除端口号
|
|
for i := 0; i < len(host); i++ {
|
|
if host[i] == ':' {
|
|
host = host[:i]
|
|
break
|
|
}
|
|
}
|
|
|
|
if vhost, ok := v.hosts[host]; ok {
|
|
vhost.handler(ctx)
|
|
} else if v.defaultHost != nil {
|
|
v.defaultHost.handler(ctx)
|
|
} else {
|
|
ctx.Error("Host not found", fasthttp.StatusNotFound)
|
|
}
|
|
}
|
|
}
|