Documentation
¶
Index ¶
- func AdminCORS(cfg *config.Config) func(http.Handler) http.Handler
- func BodyLimit(limitBytes int) func(http.Handler) http.Handler
- func BodyLimitPathAware(defaultLimitBytes, fileUploadLimitBytes int) func(http.Handler) http.Handler
- func CORS() func(http.Handler) http.Handler
- func CSPNonceFromContext(ctx context.Context) string
- func New(cfg *config.Config, webFS embed.FS) chi.Router
- func Recoverer(next http.Handler) http.Handler
- func RequestIDFromContext(r *http.Request) string
- func RequestLogger(next http.Handler) http.Handler
- func SecurityHeaders(next http.Handler) http.Handler
- func TrustedRealIP(cfg *config.Config) func(http.Handler) http.Handler
- func WithRequestID(next http.Handler) http.Handler
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AdminCORS ¶
AdminCORS returns a CORS middleware for admin API routes. By default it does not allow cross-origin browser access; operators can opt in with ADMIN_CORS_ALLOWED_ORIGINS for separately hosted admin frontends.
func BodyLimit ¶
BodyLimit enforces a maximum request body size using http.MaxBytesReader. Returns a middleware that wraps the request body so that reads beyond the limit return an error, causing the handler to receive a closed body.
func BodyLimitPathAware ¶ added in v0.13.0
func BodyLimitPathAware(defaultLimitBytes, fileUploadLimitBytes int) func(http.Handler) http.Handler
BodyLimitPathAware enforces a maximum request body size, selecting between a general limit and a higher file-upload limit based on the request path. Multipart upload routes (/v1/files, /v1/images/*) use the higher fileUploadLimitBytes; all other paths use defaultLimitBytes.
A Content-Length that already exceeds the selected limit is rejected early with a 413 JSON response before the handler runs, so the client gets a clean error instead of a truncated read. For chunked requests (no Content-Length) http.MaxBytesReader still caps the body as a safety net.
func CORS ¶
CORS returns a CORS middleware handler configured for Metapi. It is intentionally permissive for public health and proxy endpoints.
func CSPNonceFromContext ¶ added in v0.16.20
CSPNonceFromContext returns the per-request CSP nonce minted by SecurityHeaders, or "" when the middleware is not in the chain (test routers that mount handlers directly). Callers must treat "" as "nonce unavailable" and degrade instead of guessing.
func New ¶
New creates and configures the Chi router with the full middleware stack, route groups, SPA fallback, and asset caching.
func Recoverer ¶
Recoverer catches panics, logs the panic + stack via slog (with request_id for correlation), and returns 500. Equivalent to chi middleware.Recoverer but keeps panic diagnostics in the structured log instead of stderr.
func RequestIDFromContext ¶
RequestIDFromContext extracts the request ID from context. Returns an empty string if not set.
func RequestLogger ¶
RequestLogger logs every incoming request using slog after it completes, including status, duration, and bytes written for latency/error triage. Includes request_id for log correlation when RequestID middleware is active. Equivalent to TS Fastify `logger: true`, extended with outcome fields.
func SecurityHeaders ¶
SecurityHeaders adds standard security HTTP response headers. Applied globally to all routes including the admin SPA and API.
func TrustedRealIP ¶
TrustedRealIP reads X-Forwarded-For / X-Real-IP only from explicitly configured proxy CIDRs. Direct clients cannot spoof rate-limit or admin allowlist identity by sending forwarded headers themselves.
Types ¶
This section is empty.