feat(api): define public problem errors, pagination, and retry contract
This commit is contained in:
1 parent
b2f6e87bfe
commit
565ca94bbb
86 files changed
+8200
-154
No files matched your search
@@ -10,36 +10,62 @@ import (
|
||||
|
||||
apiadapter "lincodex/backend/internal/adapter/primary/http/api"
|
||||
httpmiddleware "lincodex/backend/internal/adapter/primary/http/middleware"
|
||||
"lincodex/backend/internal/adapter/primary/http/problem"
|
||||
v1 "lincodex/backend/internal/adapter/primary/http/v1"
|
||||
)
|
||||
|
||||
const (
|
||||
healthPath = "/healthz"
|
||||
readinessPath = "/readyz"
|
||||
apiRoot = "/api"
|
||||
publicPrefix = apiRoot + "/v1"
|
||||
)
|
||||
|
||||
// NewRouter mounts the internal operational routes and the generated public API.
|
||||
// The generated server keeps ogen's default error handler, which answers
|
||||
// ht.ErrNotImplemented with HTTP 501; operations without a business
|
||||
// implementation in apiHandler rely on that mapping.
|
||||
// Every error under /api/v1 is an RFC 9457 problem response: problems maps
|
||||
// ht.ErrNotImplemented to 501, request decoding and validation errors to 400,
|
||||
// unknown routes under /api to 404, unsupported methods to 405, and any other
|
||||
// server error or panic to 500 without exposing its text. Operation errors go
|
||||
// through apiHandler.NewError.
|
||||
func NewRouter(
|
||||
health *v1.HealthHandler,
|
||||
apiHandler apiadapter.Handler,
|
||||
problems *problem.Responder,
|
||||
collector httpmiddleware.RequestMetrics,
|
||||
logger *slog.Logger,
|
||||
) (http.Handler, error) {
|
||||
apiServer, err := apiadapter.NewServer(apiHandler)
|
||||
apiServer, err := apiadapter.NewServer(
|
||||
apiHandler,
|
||||
apiadapter.WithErrorHandler(problems.HandleServerError),
|
||||
apiadapter.WithNotFound(problems.NotFound),
|
||||
apiadapter.WithMethodNotAllowed(problems.MethodNotAllowed),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("httpadapter.NewRouter - create API server: %w", err)
|
||||
}
|
||||
|
||||
router := chi.NewRouter()
|
||||
// Request ID, request logging, and metrics wrap every route, so both
|
||||
// recovery middlewares below run inside them: a recovered panic carries the
|
||||
// request ID and its 500 is logged and counted.
|
||||
router.Use(kitmiddleware.RequestID())
|
||||
router.Use(kitmiddleware.Recoverer(logger))
|
||||
router.Use(kitmiddleware.Logger(logger, nil, kitmiddleware.WithSkipPaths(healthPath, readinessPath)))
|
||||
router.Use(httpmiddleware.CollectMetrics(collector))
|
||||
router.Get(healthPath, health.Healthz)
|
||||
router.Get(readinessPath, health.Readyz)
|
||||
router.Handle("/api/v1/*", apiServer)
|
||||
router.Group(func(operational chi.Router) {
|
||||
operational.Use(kitmiddleware.Recoverer(logger))
|
||||
operational.Get(healthPath, health.Healthz)
|
||||
operational.Get(readinessPath, health.Readyz)
|
||||
})
|
||||
// The public API uses only problems.Recover, which answers panics with the
|
||||
// contract's internal problem and re-raises http.ErrAbortHandler so the
|
||||
// server aborts the response; the kit Recoverer would swallow that panic.
|
||||
router.Group(func(public chi.Router) {
|
||||
public.Use(problems.Recover)
|
||||
public.Handle(publicPrefix+"/*", apiServer)
|
||||
// Unknown /api paths outside the versioned prefix are also problems,
|
||||
// so every error under /api has the contract's format.
|
||||
public.Handle(apiRoot, http.HandlerFunc(problems.NotFound))
|
||||
public.Handle(apiRoot+"/*", http.HandlerFunc(problems.NotFound))
|
||||
})
|
||||
return router, nil
|
||||
}
|
||||
Reference in new issue
Block a user