middleware

package
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2025 License: MIT Imports: 14 Imported by: 1

Documentation

Overview

Package middleware provides a collection of HTTP middleware components for the SRouter framework.

Package middleware provides a collection of HTTP middleware components for the SRouter framework.

Package middleware provides a collection of HTTP middleware components for the SRouter framework. These middleware components can be used to add functionality such as logging, recovery from panics, authentication, request timeouts, and more to your HTTP handlers.

Package middleware provides a collection of HTTP middleware components for the SRouter framework.

Package middleware provides a collection of HTTP middleware components for the SRouter framework.

Index

Constants

This section is empty.

Variables

View Source
var ClientIPKey = clientIPKey{}

ClientIPKey is the key used to store the client IP in the request context

View Source
var TraceIDKey = traceIDKey{}

Functions

func AddTraceIDToRequest added in v1.0.0

func AddTraceIDToRequest(r *http.Request, traceID string) *http.Request

AddTraceIDToRequest adds a trace ID to the request context. This is useful for testing or for manually setting a trace ID.

func Authentication

func Authentication[T comparable](authFunc func(*http.Request) (T, bool)) common.Middleware

Authentication is a middleware that checks if a request is authenticated using a simple auth function. The type parameter T represents the user ID type, which can be any comparable type. It allows for custom authentication logic to be provided as a simple function.

func AuthenticationBool added in v1.0.0

func AuthenticationBool(authFunc func(*http.Request) bool) common.Middleware

AuthenticationBool is a middleware that checks if a request is authenticated using a simple auth function. This is a convenience wrapper for backward compatibility. It allows for custom authentication logic to be provided as a simple function that returns a boolean. It adds a boolean value (true) to the request context if authentication is successful.

func AuthenticationWithProvider

func AuthenticationWithProvider[T comparable](provider AuthProvider[T], logger *zap.Logger) common.Middleware

AuthenticationWithProvider is a middleware that checks if a request is authenticated using the provided auth provider. If authentication fails, it returns a 401 Unauthorized response. This middleware allows for flexible authentication mechanisms by accepting any AuthProvider implementation. The type parameter T represents the user ID type, which can be any comparable type.

func AuthenticationWithUser added in v1.0.0

func AuthenticationWithUser[T any](authFunc func(*http.Request) (*T, error)) common.Middleware

AuthenticationWithUser is a middleware that uses a custom auth function that returns a user object and adds it to the request context if authentication is successful.

func AuthenticationWithUserProvider added in v1.0.0

func AuthenticationWithUserProvider[T any](provider UserAuthProvider[T], logger *zap.Logger) common.Middleware

AuthenticationWithUserProvider is a middleware that uses an auth provider that returns a user object and adds it to the request context if authentication is successful.

func ClientIP added in v1.0.0

func ClientIP(r *http.Request) string

ClientIP extracts the client IP from the request context

func ClientIPMiddleware added in v1.0.0

func ClientIPMiddleware(config *IPConfig) func(http.Handler) http.Handler

ClientIPMiddleware creates a middleware that extracts the client IP from the request and adds it to the request context

func CreateRateLimitMiddleware added in v1.0.2

func CreateRateLimitMiddleware[T comparable, U any](
	bucketName string,
	limit int,
	window time.Duration,
	strategy RateLimitStrategy,
	userIDFromUser func(U) T,
	userIDToString func(T) string,
	logger *zap.Logger,
) func(http.Handler) http.Handler

CreateRateLimitMiddleware is a helper function to create a rate limit middleware with generic type parameters This function is useful when you want to create a rate limit middleware with a specific user ID type and user type The type parameter T represents the user ID type, which can be any comparable type. The type parameter U represents the user type, which can be any type.

func GetTraceID added in v1.0.0

func GetTraceID(r *http.Request) string

GetTraceID extracts the trace ID from the request context. Returns an empty string if no trace ID is found.

func GetTraceIDFromContext added in v1.0.0

func GetTraceIDFromContext(ctx context.Context) string

GetTraceIDFromContext extracts the trace ID from a context. Returns an empty string if no trace ID is found.

func GetUser added in v1.0.0

func GetUser[T any](r *http.Request) *T

GetUser retrieves the user from the request context. Returns nil if no user is found in the context.

func GetUserID added in v1.0.0

func GetUserID[T comparable](r *http.Request) (T, bool)

GetUserID retrieves the user ID from the request context. Returns the zero value of T and false if no user ID is found in the context.

func NewAPIKeyMiddleware

func NewAPIKeyMiddleware[T comparable](validKeys map[string]T, header, query string, logger *zap.Logger) common.Middleware

NewAPIKeyMiddleware creates a middleware that uses API Key Authentication. It takes a map of valid API keys, the header and query parameter names to check, and a logger for authentication failures. The type parameter T represents the user ID type, which can be any comparable type.

func NewAPIKeyWithUserMiddleware added in v1.0.0

func NewAPIKeyWithUserMiddleware[T any](getUserFunc func(key string) (*T, error), header, query string, logger *zap.Logger) common.Middleware

NewAPIKeyWithUserMiddleware creates a middleware that uses API Key Authentication and returns a user object.

func NewBearerTokenMiddleware

func NewBearerTokenMiddleware[T comparable](validTokens map[string]T, logger *zap.Logger) common.Middleware

NewBearerTokenMiddleware creates a middleware that uses Bearer Token Authentication. It takes a map of valid tokens and a logger for authentication failures. The type parameter T represents the user ID type, which can be any comparable type.

func NewBearerTokenValidatorMiddleware

func NewBearerTokenValidatorMiddleware[T comparable](validator func(string) (T, bool), logger *zap.Logger) common.Middleware

NewBearerTokenValidatorMiddleware creates a middleware that uses Bearer Token Authentication with a custom validator function. This allows for more complex token validation logic, such as JWT validation or integration with external authentication services. The type parameter T represents the user ID type, which can be any comparable type.

func NewBearerTokenWithUserMiddleware added in v1.0.0

func NewBearerTokenWithUserMiddleware[T any](getUserFunc func(token string) (*T, error), logger *zap.Logger) common.Middleware

NewBearerTokenWithUserMiddleware creates a middleware that uses Bearer Token Authentication and returns a user object.

func RateLimit added in v1.0.0

func RateLimit[T comparable, U any](config *RateLimitConfig[T, U], limiter RateLimiter, logger *zap.Logger) func(http.Handler) http.Handler

RateLimit creates a middleware that enforces rate limits using generic type parameters The type parameter T represents the user ID type, which can be any comparable type. The type parameter U represents the user type, which can be any type.

func TraceMiddleware added in v1.0.0

func TraceMiddleware() common.Middleware

TraceMiddleware creates a middleware that generates a unique trace ID for each request and adds it to the request context. This allows for request tracing across logs.

Types

type APIKeyProvider

type APIKeyProvider[T comparable] struct {
	ValidKeys map[string]T // key -> user ID
	Header    string       // header name (e.g., "X-API-Key")
	Query     string       // query parameter name (e.g., "api_key")
}

APIKeyProvider provides API Key Authentication. It can validate API keys provided in a header or query parameter. The type parameter T represents the user ID type, which can be any comparable type.

func (*APIKeyProvider[T]) Authenticate

func (p *APIKeyProvider[T]) Authenticate(r *http.Request) (T, bool)

Authenticate authenticates a request using API Key Authentication. It checks for the API key in either the specified header or query parameter and validates it against the stored valid keys. Returns the user ID if authentication is successful, the zero value of T and false otherwise.

type APIKeyUserAuthProvider added in v1.0.0

type APIKeyUserAuthProvider[T any] struct {
	GetUserFunc func(key string) (*T, error)
	Header      string // header name (e.g., "X-API-Key")
	Query       string // query parameter name (e.g., "api_key")
}

APIKeyUserAuthProvider provides API Key Authentication with user object return.

func (*APIKeyUserAuthProvider[T]) AuthenticateUser added in v1.0.0

func (p *APIKeyUserAuthProvider[T]) AuthenticateUser(r *http.Request) (*T, error)

AuthenticateUser authenticates a request using API Key Authentication. It checks for the API key in either the specified header or query parameter and validates it using the GetUserFunc. Returns the user object if authentication is successful, nil and an error otherwise.

type AuthProvider

type AuthProvider[T comparable] interface {
	// Authenticate authenticates a request and returns the user ID if authentication is successful.
	// It examines the request for authentication credentials (such as headers, cookies, or query parameters)
	// and validates them according to the provider's implementation.
	// Returns the user ID if the request is authenticated, the zero value of T otherwise.
	Authenticate(r *http.Request) (T, bool)
}

AuthProvider defines an interface for authentication providers. Different authentication mechanisms can implement this interface to be used with the AuthenticationWithProvider middleware. The framework includes several implementations: BasicAuthProvider, BearerTokenProvider, and APIKeyProvider. The type parameter T represents the user ID type, which can be any comparable type.

type BasicUserAuthProvider added in v1.0.0

type BasicUserAuthProvider[T any] struct {
	GetUserFunc func(username, password string) (*T, error)
}

BasicUserAuthProvider provides HTTP Basic Authentication with user object return.

func (*BasicUserAuthProvider[T]) AuthenticateUser added in v1.0.0

func (p *BasicUserAuthProvider[T]) AuthenticateUser(r *http.Request) (*T, error)

AuthenticateUser authenticates a request using HTTP Basic Authentication. It extracts the username and password from the Authorization header and validates them using the GetUserFunc. Returns the user object if authentication is successful, nil and an error otherwise.

type BearerTokenProvider

type BearerTokenProvider[T comparable] struct {
	ValidTokens map[string]T                 // token -> user ID
	Validator   func(token string) (T, bool) // optional token validator
}

BearerTokenProvider provides Bearer Token Authentication. It can validate tokens against a predefined map or using a custom validator function. The type parameter T represents the user ID type, which can be any comparable type.

func (*BearerTokenProvider[T]) Authenticate

func (p *BearerTokenProvider[T]) Authenticate(r *http.Request) (T, bool)

Authenticate authenticates a request using Bearer Token Authentication. It extracts the token from the Authorization header and validates it using either the validator function (if provided) or the ValidTokens map. Returns the user ID if authentication is successful, the zero value of T and false otherwise.

type BearerTokenUserAuthProvider added in v1.0.0

type BearerTokenUserAuthProvider[T any] struct {
	GetUserFunc func(token string) (*T, error)
}

BearerTokenUserAuthProvider provides Bearer Token Authentication with user object return.

func (*BearerTokenUserAuthProvider[T]) AuthenticateUser added in v1.0.0

func (p *BearerTokenUserAuthProvider[T]) AuthenticateUser(r *http.Request) (*T, error)

AuthenticateUser authenticates a request using Bearer Token Authentication. It extracts the token from the Authorization header and validates it using the GetUserFunc. Returns the user object if authentication is successful, nil and an error otherwise.

type IPConfig added in v1.0.0

type IPConfig struct {
	// Source specifies where to extract the client IP from
	Source IPSourceType

	// CustomHeader is the name of the custom header to use when Source is IPSourceCustomHeader
	CustomHeader string

	// TrustProxy determines whether to trust proxy headers like X-Forwarded-For
	// If false, RemoteAddr will be used as a fallback for all sources
	TrustProxy bool
}

IPConfig defines configuration for IP extraction

func DefaultIPConfig added in v1.0.0

func DefaultIPConfig() *IPConfig

DefaultIPConfig returns the default IP configuration

type IPSourceType added in v1.0.0

type IPSourceType string

IPSourceType defines the source for client IP addresses

const (
	// IPSourceRemoteAddr uses the request's RemoteAddr field
	IPSourceRemoteAddr IPSourceType = "remote_addr"

	// IPSourceXForwardedFor uses the X-Forwarded-For header
	IPSourceXForwardedFor IPSourceType = "x_forwarded_for"

	// IPSourceXRealIP uses the X-Real-IP header
	IPSourceXRealIP IPSourceType = "x_real_ip"

	// IPSourceCustomHeader uses a custom header specified in the configuration
	IPSourceCustomHeader IPSourceType = "custom_header"
)

type Middleware

type Middleware = common.Middleware

Middleware is an alias for the common.Middleware type. It represents a function that wraps an http.Handler to provide additional functionality.

func CORS

func CORS(origins []string, methods []string, headers []string) Middleware

CORS is a middleware that adds Cross-Origin Resource Sharing (CORS) headers to the response. It allows you to specify which origins, methods, and headers are allowed for cross-origin requests. This middleware also handles preflight OPTIONS requests automatically.

func Chain

func Chain(middlewares ...Middleware) Middleware

Chain chains multiple middlewares together into a single middleware. The middlewares are applied in reverse order, so the first middleware in the list will be the outermost wrapper (the first to process the request and the last to process the response).

func Logging

func Logging(logger *zap.Logger) Middleware

Logging is a middleware that logs HTTP requests and responses. It captures the request method, path, status code, and duration. The log level is determined by the status code and duration: - 500+ status codes are logged at Error level - 400-499 status codes are logged at Warn level - Requests taking longer than 1 second are logged at Warn level - All other requests are logged at Debug level

func MaxBodySize

func MaxBodySize(maxSize int64) Middleware

MaxBodySize is a middleware that limits the size of the request body. It prevents clients from sending excessively large requests that could consume too much memory or cause denial of service.

func Recovery

func Recovery(logger *zap.Logger) Middleware

Recovery is a middleware that recovers from panics in HTTP handlers. It logs the panic and stack trace using the provided logger and returns a 500 Internal Server Error response. This prevents the server from crashing when a panic occurs in a handler.

func Timeout

func Timeout(timeout time.Duration) Middleware

Timeout is a middleware that sets a timeout for the request processing. If the handler takes longer than the specified timeout to respond, the middleware will cancel the request context and return a 408 Request Timeout response. This prevents long-running requests from blocking server resources indefinitely.

type RateLimitConfig added in v1.0.0

type RateLimitConfig[T comparable, U any] struct {
	// Unique identifier for this rate limit bucket
	// If multiple routes/subrouters share the same BucketName, they share the same rate limit
	BucketName string

	// Maximum number of requests allowed in the time window
	Limit int

	// Time window for the rate limit (e.g., 1 minute, 1 hour)
	Window time.Duration

	// Strategy for identifying clients (IP, User, Custom)
	// - "ip": Use client IP address
	// - "user": Use authenticated user ID
	// - "custom": Use a custom key extractor
	Strategy RateLimitStrategy

	// Function to extract user ID from user object (only used when Strategy is StrategyUser)
	// This allows for efficient user ID extraction without trying multiple types
	UserIDFromUser func(U) T

	// Function to convert user ID to string (only used when Strategy is StrategyUser)
	// This allows for efficient user ID conversion without type assertions
	UserIDToString func(T) string

	// Custom key extractor function (used when Strategy is "custom")
	// This allows for complex rate limiting scenarios
	KeyExtractor func(*http.Request) (string, error)

	// Response to send when rate limit is exceeded
	// If nil, a default 429 Too Many Requests response is sent
	ExceededHandler http.Handler
}

RateLimitConfig defines configuration for rate limiting with generic type parameters The type parameter T represents the user ID type, which can be any comparable type. The type parameter U represents the user type, which can be any type.

type RateLimitStrategy added in v1.0.0

type RateLimitStrategy int
const (
	// StrategyIP uses the client's IP address as the key for rate limiting
	StrategyIP RateLimitStrategy = iota
	// StrategyUser uses the authenticated user's ID as the key for rate limiting
	StrategyUser
	// StrategyCustom uses a custom key extractor function for rate limiting
	StrategyCustom
)

type RateLimiter added in v1.0.0

type RateLimiter interface {
	// Allow checks if a request is allowed based on the key and rate limit config
	// Returns true if the request is allowed, false otherwise
	// Also returns the number of remaining requests and time until reset
	Allow(key string, limit int, window time.Duration) (bool, int, time.Duration)
}

RateLimiter defines the interface for rate limiting algorithms

type UberRateLimiter added in v1.0.0

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

UberRateLimiter implements RateLimiter using Uber's ratelimit library

func NewUberRateLimiter added in v1.0.0

func NewUberRateLimiter() *UberRateLimiter

NewUberRateLimiter creates a new rate limiter using Uber's ratelimit library

func (*UberRateLimiter) Allow added in v1.0.0

func (u *UberRateLimiter) Allow(key string, limit int, window time.Duration) (bool, int, time.Duration)

Allow checks if a request is allowed based on the key and rate limit config This implementation uses only the leaky bucket algorithm for simplicity and efficiency

type UserAuthProvider added in v1.0.0

type UserAuthProvider[T any] interface {
	// AuthenticateUser authenticates a request and returns the user object if authentication is successful.
	// It examines the request for authentication credentials (such as headers, cookies, or query parameters)
	// and validates them according to the provider's implementation.
	// Returns the user object if the request is authenticated, nil and an error otherwise.
	AuthenticateUser(r *http.Request) (*T, error)
}

UserAuthProvider defines an interface for authentication providers that return a user object. Different authentication mechanisms can implement this interface to be used with the AuthenticationWithUserProvider middleware.

Jump to

Keyboard shortcuts

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