Documentation
¶
Overview ¶
Package middleware provides HTTP middleware for the Starmap API server. It includes logging, recovery, CORS, authentication, and rate limiting.
Index ¶
- func Auth(config AuthConfig, logger *zerolog.Logger) func(http.Handler) http.Handler
- func CORS(config CORSConfig) func(http.Handler) http.Handler
- func Chain(middlewares ...func(http.Handler) http.Handler) func(http.Handler) http.Handler
- func Logger(logger *zerolog.Logger) func(http.Handler) http.Handler
- func RateLimit(rl *RateLimiter) func(http.Handler) http.Handler
- func Recovery(logger *zerolog.Logger) func(http.Handler) http.Handler
- func RouteTimeouts(routes []RouteTimeout, logger *zerolog.Logger) func(http.Handler) http.Handler
- type AuthConfig
- type CORSConfig
- type RateLimiter
- type RouteTimeout
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CORS ¶
func CORS(config CORSConfig) func(http.Handler) http.Handler
CORS middleware adds CORS headers to responses.
func RateLimit ¶
func RateLimit(rl *RateLimiter) func(http.Handler) http.Handler
RateLimit middleware limits requests per IP address.
func RouteTimeouts ¶ added in v0.16.0
RouteTimeouts applies the write bound of the first matching route before the handler runs. A route without an entry keeps the server-wide write timeout.
The middleware runs before the handlers, so a streaming handler never races the server-wide bound that a short JSON response needs.
Types ¶
type AuthConfig ¶
type AuthConfig struct {
Enabled bool
APIKey string
HeaderName string
PublicPaths []string
BearerPrefix bool
FailureOverride func(http.ResponseWriter, *http.Request) bool
}
AuthConfig holds authentication configuration.
func DefaultAuthConfig ¶
func DefaultAuthConfig() AuthConfig
DefaultAuthConfig returns default authentication configuration.
type CORSConfig ¶
type CORSConfig struct {
AllowedOrigins []string
AllowedMethods []string
AllowedHeaders []string
AllowAll bool
}
CORSConfig holds CORS configuration.
func DefaultCORSConfig ¶
func DefaultCORSConfig() CORSConfig
DefaultCORSConfig returns the default CORS configuration.
type RateLimiter ¶
type RateLimiter struct {
// contains filtered or unexported fields
}
RateLimiter implements token bucket rate limiting per IP address.
func NewRateLimiter ¶
func NewRateLimiter(limit int, logger *zerolog.Logger) *RateLimiter
NewRateLimiter creates a new rate limiter. limit is requests per minute per IP.
type RouteTimeout ¶ added in v0.16.0
type RouteTimeout struct {
// Path matches the request path. An exact entry matches the whole path, and
// a prefix entry matches every path below it.
Path string
// Prefix selects prefix matching instead of whole-path matching.
Prefix bool
// Write bounds one response write. A zero value clears the deadline, so a
// streaming handler keeps its own per-chunk or per-frame bound.
Write time.Duration
}
RouteTimeout describes the write bound of one route group. A streaming route needs a different bound from an ordinary JSON route, so the policy splits them instead of forcing one server-wide value on both.