middleware

package
v1.6.5 Latest Latest
Warning

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

Go to latest
Published: Jun 18, 2025 License: Apache-2.0 Imports: 10 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var RateLimiterConfig = limiter.Config{
	Max:        config.GetEnvAsIntOrDefault("RATE_LIMIT", 100),
	Expiration: time.Duration(config.GetEnvAsIntOrDefault("RATE_LIMIT_DURATION", 1)) * time.Minute,
	KeyGenerator: func(c *fiber.Ctx) string {
		return c.IP()
	},
	LimitReached: func(c *fiber.Ctx) error {
		return c.Status(fiber.StatusTooManyRequests).JSON(fiber.Map{
			"success": false,
			"message": "Rate limit exceeded. Please try again later.",
		})
	},
	SkipFailedRequests:     false,
	SkipSuccessfulRequests: false,
}

RateLimiterConfig default rate limiter configuration

Functions

func DefaultAdvancedRateLimiter

func DefaultAdvancedRateLimiter() fiber.Handler

DefaultAdvancedRateLimiter returns a default rate limiter middleware (100 requests per minute)

func DefaultRateLimiter

func DefaultRateLimiter() fiber.Handler

DefaultRateLimiter creates a rate limiter with default configuration

func DeleteLimiter

func DeleteLimiter(cfg RateLimitConfig) fiber.Handler

DeleteLimiter returns specific rate limiter for delete endpoints

func NewAdvancedRateLimiter

func NewAdvancedRateLimiter(max int, duration time.Duration) fiber.Handler

NewAdvancedRateLimiter creates a new rate limiter middleware with Redis storage

func NewRateLimiter

func NewRateLimiter(max int, duration time.Duration) fiber.Handler

NewRateLimiter creates a new rate limiter middleware with custom config

func RateLimitKey

func RateLimitKey(c *fiber.Ctx) string

RateLimitKey generates a unique key for rate limiting based on IP and token

func SecurityMiddleware

func SecurityMiddleware(cfg SecurityConfig) []fiber.Handler

SecurityMiddleware returns security middleware chain

func UploadLimiter

func UploadLimiter(cfg RateLimitConfig) fiber.Handler

UploadLimiter returns specific rate limiter for upload endpoints

Types

type CORSConfig

type CORSConfig struct {
	AllowOrigins     []string
	AllowMethods     []string
	AllowHeaders     []string
	ExposeHeaders    []string
	MaxAge           int
	AllowCredentials bool
}

type RateLimitConfig

type RateLimitConfig struct {
	MaxRequests    int
	WindowDuration time.Duration
	ExemptedPaths  []string
	UploadLimit    int
	UploadWindow   time.Duration
	DeleteLimit    int
	DeleteWindow   time.Duration
}

type RedisStorage

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

RedisStorage implements fiber.Storage interface for Redis

func NewRedisStorage

func NewRedisStorage() (*RedisStorage, error)

NewRedisStorage creates a new Redis storage adapter

func (*RedisStorage) Close

func (r *RedisStorage) Close() error

Close closes the Redis connection

func (*RedisStorage) Delete

func (r *RedisStorage) Delete(key string) error

Delete removes a value from Redis

func (*RedisStorage) Get

func (r *RedisStorage) Get(key string) ([]byte, error)

Get retrieves a value from Redis

func (*RedisStorage) Reset

func (r *RedisStorage) Reset() error

Reset clears all values from Redis

func (*RedisStorage) Set

func (r *RedisStorage) Set(key string, val []byte, exp time.Duration) error

Set stores a value in Redis

type SecurityConfig

type SecurityConfig struct {
	Token              string
	RateLimit          RateLimitConfig
	CORS               CORSConfig
	TrustedProxies     []string
	MaxRequestBodySize int
	RequestTimeout     time.Duration
}

SecurityConfig represents security middleware configuration

func DefaultSecurityConfig

func DefaultSecurityConfig() SecurityConfig

DefaultSecurityConfig returns default security configuration

Jump to

Keyboard shortcuts

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