Skip to content

Commit

Permalink
optional rate limiter
Browse files Browse the repository at this point in the history
  • Loading branch information
tobinbradley committed Jun 27, 2022
1 parent b49bcd8 commit 63a2990
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ This is the complete complete list of environmental variables that can be set.
| CACHE_PRIVACY | No | private | [Cache response directive](https://github.com/fastify/fastify-caching) |
| CACHE_EXPIRESIN | No | 3600 | [Max age in seconds](https://github.com/fastify/fastify-caching) |
| CACHE_SERVERCACHE | No | undefined | Max age in seconds for [shared cache](https://github.com/fastify/fastify-caching) (i.e. CDN) |
| RATE_MAX | No | undefined | Requests per minute rate limiter (limiter not used if RATE_LIMIT not set) |


### Step 3: fire it up!
Expand Down
8 changes: 8 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ fastify.register(
// CORS
fastify.register(require('@fastify/cors'))

// OPTIONAL RATE LIMITER
if ("RATE_MAX" in process.env) {
fastify.register(import('@fastify/rate-limit'), {
max: process.env.RATE_MAX,
timeWindow: '1 minute'
})
}

// INITIALIZE SWAGGER
fastify.register(require('@fastify/swagger'), {
exposeRoute: true,
Expand Down
33 changes: 33 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"@fastify/compress": "^6.1.0",
"@fastify/cors": "^8.0.0",
"@fastify/postgres": "^5.0.0",
"@fastify/rate-limit": "^7.0.0",
"@fastify/swagger": "^7.4.0",
"dotenv": "^16.0.1",
"fastify": "^4.1.0",
Expand Down

0 comments on commit 63a2990

Please sign in to comment.