mirror of
https://github.com/betagouv/mon-entreprise
synced 2025-02-10 15:05:01 +00:00
34 lines
842 B
TypeScript
34 lines
842 B
TypeScript
|
import { BaseContext, Next } from 'koa'
|
||
|
import { RateLimiterMemory, RateLimiterRedis } from 'rate-limiter-flexible'
|
||
|
import IORedis from 'ioredis'
|
||
|
|
||
|
const Redis = IORedis.default
|
||
|
|
||
|
const rateLimiter =
|
||
|
process.env.NODE_ENV === 'production' && process.env.SCALINGO_REDIS_URL
|
||
|
? new RateLimiterRedis({
|
||
|
storeClient: new Redis(process.env.SCALINGO_REDIS_URL, {
|
||
|
enableOfflineQueue: false,
|
||
|
}),
|
||
|
keyPrefix: 'middleware',
|
||
|
points: 5, // 5 requests for ctx.ip
|
||
|
duration: 1, // per 1 second
|
||
|
})
|
||
|
: new RateLimiterMemory({
|
||
|
points: 5, // 5 requests for ctx.ip
|
||
|
duration: 1, // per 1 seconds
|
||
|
})
|
||
|
|
||
|
export const rateLimiterMiddleware = async (ctx: BaseContext, next: Next) => {
|
||
|
try {
|
||
|
await rateLimiter.consume(ctx.ip)
|
||
|
} catch (rejRes) {
|
||
|
ctx.status = 429
|
||
|
ctx.body = 'Too Many Requests'
|
||
|
|
||
|
return
|
||
|
}
|
||
|
|
||
|
await next()
|
||
|
}
|