Documentation
¶
Overview ¶
Package middleware provides HTTP middleware functions for the Scanorama API server. This package implements logging, metrics, authentication, rate limiting, and other cross-cutting concerns for API requests.
Index ¶
- func Authentication(configKeys []string, database *db.DB, logger *slog.Logger) func(http.Handler) http.Handler
- func CORS(origins, headers, methods []string) func(http.Handler) http.Handler
- func Compression() func(http.Handler) http.Handler
- func ContentType() func(http.Handler) http.Handler
- func GetRequestID(r *http.Request) string
- func Logging(logger *slog.Logger) func(http.Handler) http.Handler
- func Metrics(metricsRegistry metrics.MetricsRegistry) func(http.Handler) http.Handler
- func RateLimit(requests int, window time.Duration, logger *slog.Logger) func(http.Handler) http.Handler
- func Recovery(logger *slog.Logger) func(http.Handler) http.Handler
- func RequestTimeout(timeout time.Duration) func(http.Handler) http.Handler
- func SecurityHeaders() func(http.Handler) http.Handler
- type ContextKey
- type RateLimiter
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Authentication ¶
func Authentication(configKeys []string, database *db.DB, logger *slog.Logger) func(http.Handler) http.Handler
Authentication creates an authentication middleware supporting both config and database API keys.
func Compression ¶
Compression creates a compression middleware (placeholder for now).
func ContentType ¶
ContentType creates a content type middleware that validates request content types.
func GetRequestID ¶
GetRequestID extracts the request ID from context.
func RateLimit ¶
func RateLimit(requests int, window time.Duration, logger *slog.Logger) func(http.Handler) http.Handler
RateLimit creates a rate limiting middleware.
func RequestTimeout ¶
RequestTimeout creates a request timeout middleware.
Types ¶
type ContextKey ¶
type ContextKey string
ContextKey represents a context key type.
const ( // RequestIDKey is the context key for request IDs. RequestIDKey ContextKey = "request_id" // StartTimeKey is the context key for request start time. StartTimeKey ContextKey = "start_time" // APIKeyInfoKey is the context key for API key information. APIKeyInfoKey ContextKey = "api_key_info" )
type RateLimiter ¶
type RateLimiter struct {
// contains filtered or unexported fields
}
RateLimiter implements a simple in-memory rate limiter.
func NewRateLimiter ¶
func NewRateLimiter(limit int, window time.Duration) *RateLimiter
NewRateLimiter creates a new rate limiter.
func (*RateLimiter) Allow ¶
func (rl *RateLimiter) Allow(ip string) bool
Allow checks if a request from the given IP is allowed.
func (*RateLimiter) Cleanup ¶
func (rl *RateLimiter) Cleanup()
Cleanup removes old entries from the rate limiter.