middleware

package
v0.1.9 Latest Latest
Warning

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

Go to latest
Published: Jan 11, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func APIKeyExtractor

func APIKeyExtractor(r *http.Request) string

APIKeyExtractor extracts API key from request

func FromContext

func FromContext(ctx context.Context) (*ratelimit.Result, bool)

FromContext retrieves rate limit result from context

func PerEndpointKeyGenerator

func PerEndpointKeyGenerator(userIDExtractor func(*http.Request) string) func(*http.Request) string

PerEndpointKeyGenerator generates a rate limit key per endpoint

func PerUserKeyGenerator

func PerUserKeyGenerator(userIDExtractor func(*http.Request) string) func(*http.Request) string

PerUserKeyGenerator generates a rate limit key per user

func WithContext

func WithContext(ctx context.Context, result *ratelimit.Result) context.Context

WithContext adds rate limit result to request context

Types

type RateLimiterConfig

type RateLimiterConfig struct {
	// PerUser enables per-user rate limiting
	PerUser bool

	// PerEndpoint enables per-endpoint rate limiting
	PerEndpoint bool

	// UserIDExtractor extracts user ID from request
	UserIDExtractor func(*http.Request) string

	// IPExtractor extracts IP address from request
	IPExtractor func(*http.Request) string

	// KeyGenerator generates a rate limit key from request
	KeyGenerator func(*http.Request) string

	// OnRateLimitExceeded is called when rate limit is exceeded
	OnRateLimitExceeded func(http.ResponseWriter, *http.Request, *ratelimit.Result)

	// SkipPaths defines paths that should skip rate limiting
	SkipPaths []string

	// EndpointLimits defines custom limits for specific endpoints
	EndpointLimits map[string]int
}

RateLimiterConfig contains middleware configuration

type RateLimiterMiddleware

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

RateLimiterMiddleware provides HTTP rate limiting middleware

func NewRateLimiterMiddleware

func NewRateLimiterMiddleware(limiter *ratelimit.Limiter, config RateLimiterConfig) *RateLimiterMiddleware

NewRateLimiterMiddleware creates a new rate limiter middleware

func (*RateLimiterMiddleware) GetMetrics

func (m *RateLimiterMiddleware) GetMetrics() ratelimit.Stats

GetMetrics returns the current metrics

func (*RateLimiterMiddleware) Handler

func (m *RateLimiterMiddleware) Handler(next http.Handler) http.Handler

Handler returns an HTTP middleware handler

func (*RateLimiterMiddleware) Middleware

Middleware returns a middleware function that can wrap http.HandlerFunc

func (*RateLimiterMiddleware) ResetMetrics

func (m *RateLimiterMiddleware) ResetMetrics()

ResetMetrics resets all metrics

Jump to

Keyboard shortcuts

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