middleware

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Feb 18, 2026 License: AGPL-3.0 Imports: 8 Imported by: 0

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

func IsOriginAllowed(origin string, allowedOrigins []string) bool

IsOriginAllowed checks if an origin is in the allowed list

func SecurityHeaders

func SecurityHeaders(enableHSTS bool) func(http.Handler) http.Handler

SecurityHeaders adds security-related HTTP headers to all responses

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.

Jump to

Keyboard shortcuts

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