Add structured logging and centralized error handling

- Initialize slog in the serve command with terminal/file support
- Introduce `AppError` with HTTP status for unified service-layer errors
- Replace ad-hoc `api.Error` calls with `api.RespondError`
- Wrap internal errors with `model.NewInternalError` and add reference
  IDs
- Add `RequestID` middleware and switch router from `gin.Default` to
  `gin.New`
This commit is contained in:
2026-07-04 16:24:22 +08:00
parent a78d43b166
commit 1dfccf513a
16 changed files with 281 additions and 134 deletions
+8 -1
View File
@@ -4,11 +4,18 @@ import (
"github.com/gin-gonic/gin"
"github.com/dhao2001/mygo/internal/app"
"github.com/dhao2001/mygo/internal/middleware"
)
// NewRouter builds the Gin router and registers API routes.
func NewRouter(webApp *app.WebApp) *gin.Engine {
router := gin.Default()
router := gin.New()
// Request ID must be first — every subsequent middleware and handler
// gets access to req_id in the context.
router.Use(middleware.RequestID())
router.Use(gin.Logger())
router.Use(gin.Recovery())
v1 := router.Group("/api/v1")