1
0
mirror of https://github.com/gofiber/fiber.git synced 2025-02-23 09:43:40 +00:00

147 lines
3.6 KiB
Markdown
Raw Normal View History

2020-11-21 12:36:16 -05:00
# Limiter Middleware
2020-11-17 12:12:49 -05:00
2020-11-21 12:36:16 -05:00
Limiter middleware for [Fiber](https://github.com/gofiber/fiber) that is used to limit repeat requests to public APIs and/or endpoints such as password reset. It is also useful for API clients, web crawling, or other tasks that need to be throttled.
2020-09-13 11:20:11 +02:00
2020-11-17 12:12:49 -05:00
_NOTE: This middleware uses our [Storage](https://github.com/gofiber/storage) package to support various databases through a single interface. The default configuration for this middleware saves data to memory, see the examples below for other databases._
**NOTE: this module does not share state with other processes/servers by default.**
## Table of Contents
2020-09-13 11:20:11 +02:00
2020-11-21 12:36:16 -05:00
- [Limiter Middleware](#limiter-middleware)
- [Table of Contents](#table-of-contents)
- [Signatures](#signatures)
- [Examples](#examples)
- [Default Config](#default-config)
- [Custom Config](#custom-config)
- [Custom Storage/Database](#custom-storagedatabase)
- [Config](#config)
- [Default Config](#default-config-1)
2020-09-13 11:20:11 +02:00
2020-11-17 12:12:49 -05:00
## Signatures
2020-09-13 11:20:11 +02:00
```go
func New(config ...Config) fiber.Handler
```
2020-11-17 12:12:49 -05:00
## Examples
First import the middleware from Fiber,
2020-09-13 11:20:11 +02:00
```go
import (
"github.com/gofiber/fiber/v2"
2020-12-26 14:22:17 +01:00
"github.com/gofiber/fiber/v2/middleware/limiter"
2020-09-13 11:20:11 +02:00
)
```
2020-11-17 12:12:49 -05:00
Then create a Fiber app with `app := fiber.New()`.
### Default Config
2020-09-13 11:20:11 +02:00
```go
// Default middleware config
app.Use(limiter.New())
2020-11-17 12:12:49 -05:00
```
### Custom Config
2020-09-13 11:20:11 +02:00
2020-11-17 12:12:49 -05:00
```go
2020-09-13 11:20:11 +02:00
// Or extend your config for customization
app.Use(limiter.New(limiter.Config{
Next: func(c *fiber.Ctx) bool {
return c.IP() == "127.0.0.1"
},
Max: 20,
Expiration: 30 * time.Second,
2020-11-16 21:01:20 -03:00
KeyGenerator: func(c *fiber.Ctx) string{
return "key"
}
2020-09-13 11:20:11 +02:00
LimitReached: func(c *fiber.Ctx) error {
return c.SendFile("./toofast.html")
},
Storage: myCustomStore{}
2020-09-13 11:20:11 +02:00
}))
```
2020-11-17 12:12:49 -05:00
### Custom Storage/Database
You can use any storage from our [storage](https://github.com/gofiber/storage/) package.
```go
storage := sqlite3.New() // From github.com/gofiber/storage/sqlite3
app.Use(limiter.New(limiter.Config{
Storage: storage,
}))
```
## Config
2020-09-13 11:20:11 +02:00
```go
// Config defines the config for middleware.
type Config struct {
// Next defines a function to skip this middleware when returned true.
//
// Optional. Default: nil
Next func(c *fiber.Ctx) bool
// Max number of recent connections during `Duration` seconds before sending a 429 response
//
// Default: 5
Max int
2020-11-11 18:19:53 +01:00
// KeyGenerator allows you to generate custom keys, by default c.IP() is used
2020-09-13 11:20:11 +02:00
//
// Default: func(c *fiber.Ctx) string {
// return c.IP()
// }
2020-11-11 18:19:53 +01:00
KeyGenerator func(*fiber.Ctx) string
// Expiration is the time on how long to keep records of requests in memory
//
// Default: 1 * time.Minute
Expiration time.Duration
2020-09-13 11:20:11 +02:00
// LimitReached is called when a request hits the limit
//
// Default: func(c *fiber.Ctx) error {
// return c.SendStatus(fiber.StatusTooManyRequests)
// }
LimitReached fiber.Handler
// When set to true, requests with StatusCode >= 400 won't be counted.
//
// Default: false
SkipFailedRequests bool
// When set to true, requests with StatusCode < 400 won't be counted.
//
// Default: false
SkipSuccessfulRequests bool
2020-11-11 18:19:53 +01:00
// Store is used to store the state of the middleware
//
2020-11-11 18:19:53 +01:00
// Default: an in memory store for this process only
Storage fiber.Storage
2020-09-13 11:20:11 +02:00
}
```
A custom store can be used if it implements the `Storage` interface - more details and an example can be found in `store.go`.
2020-09-13 11:20:11 +02:00
### Default Config
2020-11-17 12:12:49 -05:00
2020-09-13 11:20:11 +02:00
```go
var ConfigDefault = Config{
2020-11-11 18:19:53 +01:00
Max: 5,
Expiration: 1 * time.Minute,
KeyGenerator: func(c *fiber.Ctx) string {
2020-09-13 11:20:11 +02:00
return c.IP()
},
LimitReached: func(c *fiber.Ctx) error {
return c.SendStatus(fiber.StatusTooManyRequests)
},
SkipFailedRequests: false,
SkipSuccessfulRequests: false,
2020-09-13 11:20:11 +02:00
}
2020-11-16 15:53:53 -03:00
```