router

package
v0.16.13 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 25, 2026 License: MIT Imports: 25 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AdminCORS

func AdminCORS(cfg *config.Config) func(http.Handler) http.Handler

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

func BodyLimit(limitBytes int) func(http.Handler) http.Handler

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

func CORS() func(http.Handler) http.Handler

CORS returns a CORS middleware handler configured for Metapi. It is intentionally permissive for public health and proxy endpoints.

func New

func New(cfg *config.Config, webFS embed.FS) chi.Router

New creates and configures the Chi router with the full middleware stack, route groups, SPA fallback, and asset caching.

func Recoverer

func Recoverer(next http.Handler) http.Handler

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

func RequestIDFromContext(r *http.Request) string

RequestIDFromContext extracts the request ID from context. Returns an empty string if not set.

func RequestLogger

func RequestLogger(next http.Handler) http.Handler

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

func SecurityHeaders(next http.Handler) http.Handler

SecurityHeaders adds standard security HTTP response headers. Applied globally to all routes including the admin SPA and API.

func TrustedRealIP

func TrustedRealIP(cfg *config.Config) func(http.Handler) http.Handler

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.

func WithRequestID

func WithRequestID(next http.Handler) http.Handler

WithRequestID wraps chi's middleware.RequestID for explicit import. It reads/generates a request ID, sets X-Request-Id response header, and stores it in the context for downstream use.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL