fastify / fastify-rate-limit

A low overhead rate limiter for your routes
MIT License
497 stars 70 forks source link

allowPaths #282

Closed DiogoMarques2003 closed 1 year ago

DiogoMarques2003 commented 1 year ago

Prerequisites

🚀 Feature Proposal

It's possible add a system like allowList, but for paths in api ?

Motivation

I want this because one plugin register one route, and on this route I can't have rate limit

Example

allowPaths: ['/api/docs', '/docs']

allowPaths: (request) => {
 //code
}
DiogoMarques2003 commented 1 year ago

and like if i have '/api/docs/*' in array the system exclude rate limit of all paths started for this

gurgunday commented 1 year ago

Just a reminder that you can already register rate-limit to specific routes using Fastify’s encapsulation. It’s not exactly an allowPaths but it works just as well.

DiogoMarques2003 commented 1 year ago

Just a reminder that you can already register rate-limit to specific routes using Fastify’s encapsulation. It’s not exactly an allowPaths but it works just as well.

How to do this ? Can you give a example pls ?

mcollina commented 1 year ago
const app = fastify()

app.register(async function () {
  app.register(require('@fastify/rate-limit'), {
  })

  // put all the routes to rate limit here
})

app.register(async function () {
  // put all the routes to not rate limit here
})
DiogoMarques2003 commented 1 year ago

Hy @mcollina I already try this, but this don't work because the route is /api/docs ...

mcollina commented 1 year ago

I'm not sure how that prevents you to define /docs inside the other plugin.

DiogoMarques2003 commented 1 year ago

@mcollina Because in /api/docs I don't want to have ratelimit

mcollina commented 1 year ago

This should work:


const app = fastify()

app.register(async function () {
  app.register(require('@fastify/rate-limit'), {
  })

  // put all the routes to rate limit here
})

app.register(async function () {
  // put all the routes to not rate limit here

  app.get('/api/docs', ()=> ...)
})
DiogoMarques2003 commented 1 year ago

Ok, but the /api/docs in one plugin, exists some problem ?

mcollina commented 1 year ago

There should not be any problems.

DiogoMarques2003 commented 1 year ago

Okok, but if I make this feature, will it be accepted?

mcollina commented 1 year ago

No, I don't think so.

There is even a better way to declare a route without rate limiting (https://github.com/fastify/fastify-rate-limit/blob/4b78b2efabb9abddc746cc0c88597230157403d1/index.js#L139):

app.get('/', { config: { rateLimit: false } }, () => ...)