Documentation
¶
Overview ¶
Package middleware provides HTTP middleware functionality. Includes rate limiting, compression, request body limiting, metrics collection and other middleware.
Package middleware provides HTTP middleware functionality. Includes rate limiting, compression, request body limiting, metrics collection and other middleware.
Package middleware provides HTTP middleware functionality. Includes rate limiting, compression, request body limiting, metrics collection and other middleware.
Package middleware provides HTTP middleware functionality. Includes rate limiting, compression, request body limiting, metrics collection and other middleware.
Package middleware provides HTTP middleware functionality. Includes internationalization language detection middleware.
Package middleware provides HTTP middleware functionality. Includes rate limiting, compression, request body limiting, metrics collection and other middleware.
Package middleware provides HTTP middleware functionality. Includes rate limiting, compression, request body limiting, metrics collection and other middleware.
Package middleware provides HTTP middleware functionality. Includes rate limiting, compression, request body limiting, metrics collection and other middleware.
Package middleware provides HTTP middleware functionality. Includes rate limiting, compression, request body limiting, metrics collection and other middleware.
Index ¶
- func AuthMiddleware(apiKey string) func(http.Handler) http.Handler
- func BodyLimitMiddleware(next http.Handler) http.Handler
- func CompressMiddleware(next http.Handler) http.Handler
- func ErrorHandlerMiddleware(appMode string) func(http.Handler) http.Handler
- func GetLanguage(r *http.Request) i18n.Language
- func I18nMiddleware() func(http.Handler) http.Handler
- func IPWhitelistMiddleware(whitelist string) func(http.Handler) http.Handler
- func MetricsMiddleware(next http.Handler) http.Handler
- func OptionalAuthMiddleware(apiKey string) func(http.Handler) http.Handler
- func RateLimitMiddlewareWithLimiter(limiter *RateLimiter) func(http.Handler) http.Handler
- func SafeError(w http.ResponseWriter, r *http.Request, statusCode int, err error, ...)
- func SecurityHeadersMiddleware(next http.Handler) http.Handler
- type ErrorResponse
- type RateLimiter
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AuthMiddleware ¶
AuthMiddleware creates API Key authentication middleware
This middleware verifies whether requests are authorized by checking the X-API-Key in request headers. If API Key is empty, all requests will be rejected (API Key should be set in production environment). If API Key is not empty, only requests providing the correct API Key can pass.
Parameters:
- apiKey: API Key value, if empty then authentication is disabled (not recommended for production use)
Returns:
- func(http.Handler) http.Handler: HTTP middleware function
Usage example:
authMiddleware := AuthMiddleware("your-api-key-here")
handler := authMiddleware(protectedHandler)
func BodyLimitMiddleware ¶
BodyLimitMiddleware creates request body size limiting middleware Limits request body size to prevent malicious requests Note: http.MaxBytesReader will automatically check size when reading, returns error if limit is exceeded
func CompressMiddleware ¶
CompressMiddleware creates gzip compression middleware Automatically detects if client supports gzip, compresses response if supported
func ErrorHandlerMiddleware ¶
ErrorHandlerMiddleware creates error handling middleware
This middleware hides detailed error information in production environment, only returning generic error messages. Detailed error information is only recorded in logs, not returned to clients.
Parameters:
- appMode: application mode ("production" or "prod" indicates production environment)
Returns:
- func(http.Handler) http.Handler: HTTP middleware function
func GetLanguage ¶ added in v0.2.0
GetLanguage gets language from request (helper function)
func I18nMiddleware ¶ added in v0.2.0
I18nMiddleware creates internationalization language detection middleware
This middleware detects user language from HTTP requests with the following priority: 1. Query parameter ?lang=xx 2. Accept-Language request header 3. Default language (English)
Detected language will be stored in request context for subsequent processing.
Returns:
- func(http.Handler) http.Handler: HTTP middleware function
func IPWhitelistMiddleware ¶
IPWhitelistMiddleware creates IP whitelist middleware
This middleware only allows IP addresses in the whitelist to access protected endpoints. Whitelist is configured via environment variable IP_WHITELIST, supports comma-separated multiple IPs or CIDR networks.
Parameters:
- whitelist: IP whitelist (comma-separated IP addresses or CIDR networks)
Returns:
- func(http.Handler) http.Handler: HTTP middleware function
func MetricsMiddleware ¶
MetricsMiddleware creates Prometheus metrics collection middleware
func OptionalAuthMiddleware ¶
OptionalAuthMiddleware creates optional API Key authentication middleware
Unlike AuthMiddleware, if API Key is empty, this middleware will not reject requests. This is suitable for scenarios where certain endpoints require optional authentication.
Parameters:
- apiKey: API Key value, if empty then no authentication is performed
Returns:
- func(http.Handler) http.Handler: HTTP middleware function
func RateLimitMiddlewareWithLimiter ¶
func RateLimitMiddlewareWithLimiter(limiter *RateLimiter) func(http.Handler) http.Handler
RateLimitMiddlewareWithLimiter creates rate limiting middleware (using specified RateLimiter instance) Recommended to use this method to avoid global variables
func SafeError ¶
func SafeError(w http.ResponseWriter, r *http.Request, statusCode int, err error, detailMessage string)
SafeError safely returns error response (decides whether to hide detailed information based on environment)
func SecurityHeadersMiddleware ¶
SecurityHeadersMiddleware creates security response headers middleware
This middleware adds security-related HTTP response headers to improve application security. Includes: - X-Content-Type-Options: prevents MIME type sniffing - X-Frame-Options: prevents clickjacking - X-XSS-Protection: enables browser XSS filter - Referrer-Policy: controls referrer information - Content-Security-Policy: content security policy (optional)
Returns:
- func(http.Handler) http.Handler: HTTP middleware function
Types ¶
type ErrorResponse ¶
type ErrorResponse struct {
Error string `json:"error"`
Message string `json:"message,omitempty"`
Code string `json:"code,omitempty"`
}
ErrorResponse error response structure
type RateLimiter ¶
type RateLimiter struct {
// contains filtered or unexported fields
}
RateLimiter implements simple in-memory rate limiting
func NewRateLimiter ¶
func NewRateLimiter(rate int, window time.Duration) *RateLimiter
NewRateLimiter creates a new rate limiter rate: number of allowed requests window: time window (e.g., 1 * time.Minute)
func (*RateLimiter) AddToWhitelist ¶
func (rl *RateLimiter) AddToWhitelist(ip string) bool
AddToWhitelist adds IP to whitelist
func (*RateLimiter) Allow ¶
func (rl *RateLimiter) Allow(ip string) bool
Allow checks if request is allowed
func (*RateLimiter) IsWhitelisted ¶
func (rl *RateLimiter) IsWhitelisted(ip string) bool
IsWhitelisted checks if IP is in whitelist
func (*RateLimiter) RemoveFromWhitelist ¶
func (rl *RateLimiter) RemoveFromWhitelist(ip string)
RemoveFromWhitelist removes IP from whitelist
func (*RateLimiter) Stop ¶
func (rl *RateLimiter) Stop()
Stop stops the rate limiter Uses sync.Once to ensure it's executed only once, avoiding panic from repeatedly closing channel