mirror of
https://github.com/zap-proto/fiber.git
synced 2026-10-30 15:06:23 +00:00
Fork of gofiber/fiber v3 based on the v3.2.0 tag. Rewrites all self-imports github.com/gofiber/fiber/v3 -> github.com/zap-proto/fiber/v3. Upstream gofiber/schema and gofiber/utils/v2 dependencies are left untouched. Co-authored-by: Hanzo Dev <[email protected]>
44 lines
1.0 KiB
Go
44 lines
1.0 KiB
Go
package limiter
|
|
|
|
import (
|
|
"errors"
|
|
|
|
"github.com/zap-proto/fiber/v3"
|
|
)
|
|
|
|
const (
|
|
// X-RateLimit-* headers
|
|
xRateLimitLimit = "X-RateLimit-Limit"
|
|
xRateLimitRemaining = "X-RateLimit-Remaining"
|
|
xRateLimitReset = "X-RateLimit-Reset"
|
|
)
|
|
|
|
// Handler defines a rate-limiting strategy that can produce a middleware
|
|
// handler using the provided configuration.
|
|
type Handler interface {
|
|
New(config *Config) fiber.Handler
|
|
}
|
|
|
|
// New creates a new middleware handler
|
|
func New(config ...Config) fiber.Handler {
|
|
// Set default config
|
|
cfg := configDefault(config...)
|
|
|
|
// Return the specified middleware handler.
|
|
return cfg.LimiterMiddleware.New(&cfg)
|
|
}
|
|
|
|
// getEffectiveStatusCode returns the actual status code, considering both the error and response status
|
|
func getEffectiveStatusCode(c fiber.Ctx, err error) int {
|
|
// If there's an error and it's a *fiber.Error, use its status code
|
|
if err != nil {
|
|
var fiberErr *fiber.Error
|
|
if errors.As(err, &fiberErr) {
|
|
return fiberErr.Code
|
|
}
|
|
}
|
|
|
|
// Otherwise, use the response status code
|
|
return c.Response().StatusCode()
|
|
}
|