Documentation
¶
Overview ¶
Package middleware provides HTTP middleware for the IngestKit API.
Middleware components:
- API Key Authentication: Maps API keys to tenant IDs
- Rate Limiting: Token bucket algorithm per tenant
- CORS: Cross-origin resource sharing configuration
- Request ID: Unique ID tracking for each request
- Error Handling: Standardized JSON error responses
All middleware is designed to work with Fiber v2 framework.
Index ¶
- Constants
- func APIKeyAuth(config *APIKeyConfig) fiber.Handler
- func CORS(config *CORSConfig) fiber.Handler
- func ErrorHandler() fiber.Handler
- func RateLimit(limiter *RateLimiter) fiber.Handler
- func RequestID() fiber.Handler
- func SendError(c *fiber.Ctx, status int, code string, message string) error
- type APIKeyConfig
- type CORSConfig
- type ErrorResponse
- type RateLimiter
Constants ¶
const ( ErrCodeValidation = "validation_failed" ErrCodeAuth = "authentication_failed" ErrCodeRateLimit = "rate_limit_exceeded" ErrCodeInternal = "internal_error" ErrCodeBadRequest = "bad_request" ErrCodeNotFound = "not_found" ErrCodeUnknownEvent = "unknown_event_type" )
ErrorCode constants for common errors
Variables ¶
This section is empty.
Functions ¶
func APIKeyAuth ¶
func APIKeyAuth(config *APIKeyConfig) fiber.Handler
APIKeyAuth creates an authentication middleware
func CORS ¶
func CORS(config *CORSConfig) fiber.Handler
CORS creates a CORS middleware with the given configuration
func ErrorHandler ¶
ErrorHandler is a middleware that catches panics and converts them to error responses
func RateLimit ¶
func RateLimit(limiter *RateLimiter) fiber.Handler
RateLimit creates a rate limiting middleware
Types ¶
type APIKeyConfig ¶
APIKeyConfig holds the API key to tenant_id mapping
func NewAPIKeyConfig ¶
func NewAPIKeyConfig() *APIKeyConfig
NewAPIKeyConfig creates a new API key configuration
func (*APIKeyConfig) AddKey ¶
func (c *APIKeyConfig) AddKey(apiKey, tenantID string)
AddKey adds an API key with its associated tenant_id
func (*APIKeyConfig) ValidateKey ¶
func (c *APIKeyConfig) ValidateKey(apiKey string) (string, bool)
ValidateKey checks if an API key is valid and returns the tenant_id
type CORSConfig ¶
CORSConfig holds CORS configuration
func NewCORSConfig ¶
func NewCORSConfig() *CORSConfig
NewCORSConfig creates a default CORS configuration
type ErrorResponse ¶
type ErrorResponse struct {
Error string `json:"error"`
Message string `json:"message"`
RequestID string `json:"request_id,omitempty"`
Timestamp time.Time `json:"timestamp"`
}
ErrorResponse represents a standardized API error response
type RateLimiter ¶
type RateLimiter struct {
// contains filtered or unexported fields
}
RateLimiter implements a token bucket rate limiter
func NewRateLimiter ¶
func NewRateLimiter(requestsPerSecond int) *RateLimiter
NewRateLimiter creates a new rate limiter with automatic cleanup Buckets are evicted after 5 minutes of inactivity
func NewRateLimiterWithTTL ¶
func NewRateLimiterWithTTL(requestsPerSecond int, bucketTTL time.Duration) *RateLimiter
NewRateLimiterWithTTL creates a new rate limiter with custom TTL