76 lines
2.9 KiB
Go
76 lines
2.9 KiB
Go
package httpadapter
|
|
|
|
import (
|
|
"fmt"
|
|
"log/slog"
|
|
"net/http"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
kitmiddleware "github.com/wahrwelt-kit/go-httpkit/httputil/middleware"
|
|
|
|
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.
|
|
// 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. security decides every operation that requires
|
|
// a session; the generated server answers its rejection, and a request
|
|
// without the session cookie, with 401 unauthenticated through NewError.
|
|
func NewRouter(
|
|
health *v1.HealthHandler,
|
|
apiHandler apiadapter.Handler,
|
|
security apiadapter.SecurityHandler,
|
|
problems *problem.Responder,
|
|
collector httpmiddleware.RequestMetrics,
|
|
logger *slog.Logger,
|
|
) (http.Handler, error) {
|
|
apiServer, err := apiadapter.NewServer(
|
|
apiHandler,
|
|
security,
|
|
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.Logger(logger, nil, kitmiddleware.WithSkipPaths(healthPath, readinessPath)))
|
|
router.Use(httpmiddleware.CollectMetrics(collector))
|
|
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
|
|
}
|