Documentation
¶
Overview ¶
Package middleware provides HTTP middleware, including per-IP rate limiting.
Package middleware provides HTTP middleware, including per-IP rate limiting.
Package middleware provides HTTP middleware, including per-IP rate limiting.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CORS ¶
func CORS(config CORSConfig) func(http.Handler) http.Handler
CORS middleware handles Cross-Origin Resource Sharing
func IsOriginAllowed ¶
IsOriginAllowed checks if an origin is in the allowed list
Types ¶
type CORSConfig ¶
type CORSConfig struct {
// AllowedOrigins is a list of origins allowed for CORS
// If empty, CORS is disabled (most secure for local file sharing)
AllowedOrigins []string
// AllowCredentials indicates whether credentials are allowed
AllowCredentials bool
}
CORSConfig holds CORS configuration
func DefaultCORSConfig ¶
func DefaultCORSConfig() CORSConfig
DefaultCORSConfig returns a secure default CORS configuration By default, CORS is disabled for security
type RateLimiter ¶
type RateLimiter struct {
// contains filtered or unexported fields
}
RateLimiter enforces per-IP, per-tier rate limits.
func NewRateLimiter ¶
func NewRateLimiter(cfg RateLimiterConfig) *RateLimiter
NewRateLimiter creates a RateLimiter and starts a background goroutine that evicts stale entries every 5 minutes.
func (*RateLimiter) Close ¶
func (rl *RateLimiter) Close()
Close stops the background cleanup goroutine.
func (*RateLimiter) Middleware ¶
func (rl *RateLimiter) Middleware(next http.Handler) http.Handler
Middleware returns an http.Handler middleware that enforces rate limits.
type RateLimiterConfig ¶
type RateLimiterConfig struct {
// General rate: requests per minute for normal endpoints.
GeneralRate int
// AuthRate: requests per minute for auth endpoints (e.g. /auth/login).
AuthRate int
// UploadRate: requests per minute for upload endpoints.
UploadRate int
// Enabled can be set to false to skip rate limiting entirely.
Enabled bool
}
RateLimiterConfig holds the rates for different endpoint tiers.
func DefaultRateLimiterConfig ¶
func DefaultRateLimiterConfig() RateLimiterConfig
DefaultRateLimiterConfig returns sensible defaults.