使用 fasthttp 替代 net/http,实现 Phase 2 核心模块: - HTTP 服务器:fasthttp.Server 配置超时和连接限制 - 路由系统:fasthttp/router 基于 radix tree 匹配 - 静态文件服务:安全检查、索引文件支持 - 日志系统:zerolog 结构化日志 - 中间件框架:链式组合接口 - 虚拟主机管理:按 Host 头选择处理器 Co-Authored-By: Claude <noreply@anthropic.com>
75 lines
1.6 KiB
Go
75 lines
1.6 KiB
Go
package server
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/valyala/fasthttp"
|
|
"rua.plus/lolly/internal/config"
|
|
"rua.plus/lolly/internal/handler"
|
|
"rua.plus/lolly/internal/logging"
|
|
"rua.plus/lolly/internal/middleware"
|
|
)
|
|
|
|
// Server HTTP 服务器
|
|
type Server struct {
|
|
config *config.Config
|
|
fastServer *fasthttp.Server
|
|
handler fasthttp.RequestHandler
|
|
running bool
|
|
}
|
|
|
|
// New 创建服务器
|
|
func New(cfg *config.Config) *Server {
|
|
return &Server{config: cfg}
|
|
}
|
|
|
|
// Start 启动服务器
|
|
func (s *Server) Start() error {
|
|
// 初始化日志
|
|
logging.Init(s.config.Logging.Error.Level, true)
|
|
|
|
// 创建路由
|
|
router := handler.NewRouter()
|
|
|
|
// 静态文件服务
|
|
staticHandler := handler.NewStaticHandler(
|
|
s.config.Server.Static.Root,
|
|
s.config.Server.Static.Index,
|
|
)
|
|
|
|
// 注册路由 - 处理所有路径
|
|
router.GET("/{filepath:*}", staticHandler.Handle)
|
|
router.HEAD("/{filepath:*}", staticHandler.Handle)
|
|
|
|
// 应用中间件
|
|
chain := middleware.NewChain()
|
|
s.handler = chain.Apply(router.Handler())
|
|
|
|
// 创建 fasthttp 服务器
|
|
s.fastServer = &fasthttp.Server{
|
|
Handler: s.handler,
|
|
ReadTimeout: 30 * time.Second,
|
|
WriteTimeout: 30 * time.Second,
|
|
IdleTimeout: 120 * time.Second,
|
|
MaxConnsPerIP: 1000,
|
|
MaxRequestsPerConn: 10000,
|
|
}
|
|
|
|
s.running = true
|
|
return s.fastServer.ListenAndServe(s.config.Server.Listen)
|
|
}
|
|
|
|
// Stop 快速停止服务器
|
|
func (s *Server) Stop() error {
|
|
s.running = false
|
|
if s.fastServer != nil {
|
|
return s.fastServer.Shutdown()
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// GracefulStop 优雅停止
|
|
func (s *Server) GracefulStop(timeout time.Duration) error {
|
|
return s.Stop()
|
|
}
|