router.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package main
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "github.com/kairo/haven/internal/auth"
  5. "github.com/kairo/haven/internal/httpx"
  6. )
  7. // newRouter 组装 Gin 路由。
  8. //
  9. // 依据 docs/architecture.md 5.3:中间件顺序为 日志 → 鉴权 → 领域 handler。
  10. // 本期接入认证相关路由(api.md 第二、三、四、五、十一节)。
  11. func newRouter(h *auth.Handler, store auth.Store) *gin.Engine {
  12. // 鉴权中间件通过回调使用统一错误响应,避免 auth ↔ httpx 包循环
  13. httpx.InstallAuthErrorHandlers()
  14. auth.SetErrorCallbacks(
  15. httpx.FailBadRequest,
  16. httpx.FailValidation,
  17. httpx.FailInternal,
  18. httpx.FailNotFound,
  19. httpx.FailConflict,
  20. httpx.FailUnauthorizedWith,
  21. httpx.FailUnprocessable,
  22. )
  23. gin.SetMode(gin.ReleaseMode)
  24. r := gin.New()
  25. r.Use(gin.Recovery())
  26. r.Use(auth.Authenticate(store))
  27. r.GET("/api/health", func(c *gin.Context) {
  28. c.JSON(200, gin.H{"status": "ok"})
  29. })
  30. auth.RegisterMeRoute(r, h)
  31. auth.RegisterRoutes(r, h)
  32. auth.RegisterManageRoutes(r, h)
  33. return r
  34. }
苏ICP备2026082406号
苏公网安备 待审核