middleware

package
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Dec 24, 2025 License: Apache-2.0 Imports: 6 Imported by: 0

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

View Source
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

func ErrorHandler() fiber.Handler

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

func RequestID

func RequestID() fiber.Handler

RequestID adds a unique request ID to each request

func SendError

func SendError(c *fiber.Ctx, status int, code string, message string) error

SendError sends a standardized error response

Types

type APIKeyConfig

type APIKeyConfig struct {
	Keys map[string]string // map[api_key]tenant_id
}

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

type CORSConfig struct {
	AllowOrigins string
	AllowMethods string
	AllowHeaders string
}

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

func (*RateLimiter) Stop

func (rl *RateLimiter) Stop()

Stop stops the cleanup goroutine

Jump to

Keyboard shortcuts

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