Skip to content

Commit 63a2990

Browse files
committed
optional rate limiter
1 parent b49bcd8 commit 63a2990

File tree

4 files changed

+43
-0
lines changed

4 files changed

+43
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ This is the complete complete list of environmental variables that can be set.
4949
| CACHE_PRIVACY | No | private | [Cache response directive](https://github.com/fastify/fastify-caching) |
5050
| CACHE_EXPIRESIN | No | 3600 | [Max age in seconds](https://github.com/fastify/fastify-caching) |
5151
| CACHE_SERVERCACHE | No | undefined | Max age in seconds for [shared cache](https://github.com/fastify/fastify-caching) (i.e. CDN) |
52+
| RATE_MAX | No | undefined | Requests per minute rate limiter (limiter not used if RATE_LIMIT not set) |
5253

5354

5455
### Step 3: fire it up!

index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ fastify.register(
3131
// CORS
3232
fastify.register(require('@fastify/cors'))
3333

34+
// OPTIONAL RATE LIMITER
35+
if ("RATE_MAX" in process.env) {
36+
fastify.register(import('@fastify/rate-limit'), {
37+
max: process.env.RATE_MAX,
38+
timeWindow: '1 minute'
39+
})
40+
}
41+
3442
// INITIALIZE SWAGGER
3543
fastify.register(require('@fastify/swagger'), {
3644
exposeRoute: true,

package-lock.json

Lines changed: 33 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"@fastify/compress": "^6.1.0",
1515
"@fastify/cors": "^8.0.0",
1616
"@fastify/postgres": "^5.0.0",
17+
"@fastify/rate-limit": "^7.0.0",
1718
"@fastify/swagger": "^7.4.0",
1819
"dotenv": "^16.0.1",
1920
"fastify": "^4.1.0",

0 commit comments

Comments
 (0)