middleware

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2025 License: MIT Imports: 15 Imported by: 0

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

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 CORS

func CORS(origins, headers, methods []string) func(http.Handler) http.Handler

CORS creates a simple CORS middleware.

func Compression

func Compression() func(http.Handler) http.Handler

Compression creates a compression middleware (placeholder for now).

func ContentType

func ContentType() func(http.Handler) http.Handler

ContentType creates a content type middleware that validates request content types.

func GetRequestID

func GetRequestID(r *http.Request) string

GetRequestID extracts the request ID from context.

func Logging

func Logging(logger *slog.Logger) func(http.Handler) http.Handler

Logging creates a logging middleware that logs HTTP requests and responses.

func Metrics

func Metrics(metricsRegistry metrics.MetricsRegistry) func(http.Handler) http.Handler

Metrics creates a metrics middleware that collects HTTP request metrics.

func RateLimit

func RateLimit(requests int, window time.Duration, logger *slog.Logger) func(http.Handler) http.Handler

RateLimit creates a rate limiting middleware.

func Recovery

func Recovery(logger *slog.Logger) func(http.Handler) http.Handler

Recovery creates a recovery middleware that catches panics.

func RequestTimeout

func RequestTimeout(timeout time.Duration) func(http.Handler) http.Handler

RequestTimeout creates a request timeout middleware.

func SecurityHeaders

func SecurityHeaders() func(http.Handler) http.Handler

SecurityHeaders adds common security headers.

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.

Jump to

Keyboard shortcuts

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