Documentation
¶
Index ¶
- Constants
- func APIKeyAuth(cfg AuthConfig, logger *zap.Logger) func(http.Handler) http.Handler
- func APIKeyFromContext(ctx context.Context) (string, bool)
- func Logger(logger *zap.Logger) func(next http.Handler) http.Handler
- func LoggerWithLevel(logger *zap.Logger) func(next http.Handler) http.Handler
- func RateLimit(ratePerSecond float64, burst int, logger *zap.Logger) func(http.Handler) http.Handler
- func Recovery(logger *zap.Logger) func(next http.Handler) http.Handler
- func RecoveryWithWriter(logger *zap.Logger, ...) func(next http.Handler) http.Handler
- type AuthConfig
- type RateLimiter
Constants ¶
const (
// APIKeyHeader is the header used to pass the API key.
APIKeyHeader = "X-API-Key"
)
Variables ¶
This section is empty.
Functions ¶
func APIKeyAuth ¶
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 ¶
APIKeyFromContext returns the API key from the request context, if present.
func LoggerWithLevel ¶
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
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