middleware

package
v0.0.0-...-5baff64 Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// APIKeyHeader is the header used to pass the API key.
	APIKeyHeader = "X-API-Key"
)

Variables

This section is empty.

Functions

func APIKeyAuth

func APIKeyAuth(cfg AuthConfig, logger *zap.Logger) func(http.Handler) http.Handler

APIKeyAuth returns a middleware that validates API keys from the X-API-Key header or the "api_key" query parameter. Requests without a valid key receive 401 Unauthorized. Paths in allowedPaths bypass authentication entirely.

func APIKeyFromContext

func APIKeyFromContext(ctx context.Context) (string, bool)

APIKeyFromContext returns the API key from the request context, if present.

func Logger

func Logger(logger *zap.Logger) func(next http.Handler) http.Handler

Logger returns a middleware that logs HTTP requests

func LoggerWithLevel

func LoggerWithLevel(logger *zap.Logger) func(next http.Handler) http.Handler

LoggerWithLevel returns a middleware that logs HTTP requests with different log levels based on status code

func RateLimit

func RateLimit(ratePerSecond float64, burst int, logger *zap.Logger) func(http.Handler) http.Handler

RateLimit returns a rate limiting middleware

func Recovery

func Recovery(logger *zap.Logger) func(next http.Handler) http.Handler

Recovery returns a middleware that recovers from panics and logs them

func RecoveryWithWriter

func RecoveryWithWriter(logger *zap.Logger, writeError func(w http.ResponseWriter, r *http.Request, err interface{})) func(next http.Handler) http.Handler

RecoveryWithWriter returns a middleware that recovers from panics with custom error writer

Types

type AuthConfig

type AuthConfig struct {
	// APIKeys is the set of valid API keys. Keys map to labels for logging.
	APIKeys map[string]string

	// AllowedPaths are paths that bypass authentication (e.g., /health, /metrics).
	AllowedPaths map[string]bool
}

AuthConfig holds configuration for the authentication middleware.

type RateLimiter

type RateLimiter struct {
	// contains filtered or unexported fields
}

RateLimiter provides IP-based rate limiting with automatic cleanup

func NewRateLimiter

func NewRateLimiter(ratePerSecond float64, burst int, logger *zap.Logger) *RateLimiter

NewRateLimiter creates a new rate limiter with automatic cleanup

func (*RateLimiter) Allow

func (rl *RateLimiter) Allow(ip string) bool

Allow checks if a request from the given IP is allowed

func (*RateLimiter) CleanupLimiters

func (rl *RateLimiter) CleanupLimiters()

CleanupLimiters removes old limiters to prevent memory leaks

func (*RateLimiter) LimiterCount

func (rl *RateLimiter) LimiterCount() int

LimiterCount returns the number of active limiters

Jump to

Keyboard shortcuts

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