1dfccf513a
- 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`
27 lines
599 B
Go
27 lines
599 B
Go
package server
|
|
|
|
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.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")
|
|
|
|
setupPublicRoutes(v1, webApp)
|
|
setupProtectedRoutes(v1, webApp)
|
|
|
|
return router
|
|
}
|