修复启动问题

This commit is contained in:
2026-06-10 10:01:12 +08:00
parent aaac7e9d98
commit 9853b6fc1b
2 changed files with 13 additions and 6 deletions
+12
View File
@@ -4,6 +4,9 @@ import (
"net/http"
"github.com/go-chi/chi/v5"
"log/slog"
"github.com/yan1h/agent-coding-workflow/internal/transport/http/middleware"
)
// RouterDeps bundles the dependencies needed to assemble the router.
@@ -20,6 +23,8 @@ type RouterDeps struct {
// SPAHandler 为 nil 时禁用前端 fallback;非 nil 时挂到 chi NotFound,
// 把未匹配的请求交给 SPA handler 处理(静态文件 + index.html 回退)。
SPAHandler http.Handler
// Logger 用于 Recover 和 Logger 中间件。为 nil 时跳过日志相关中间件。
Logger *slog.Logger
}
// NewRouter builds the application's chi.Router with the standard
@@ -29,6 +34,13 @@ type RouterDeps struct {
func NewRouter(deps RouterDeps) chi.Router {
r := chi.NewRouter()
// 所有中间件必须在注册路由之前添加(chi 的强制要求)。
r.Use(middleware.RequestID)
if deps.Logger != nil {
r.Use(middleware.Recover(deps.Logger))
r.Use(middleware.Logger(deps.Logger))
}
r.Get("/healthz", func(w http.ResponseWriter, req *http.Request) {
if deps.HealthCheck != nil {
if err := deps.HealthCheck(req); err != nil {