feat: rate limit api routes (#336)
* feat: install rate-limiter-flexible and ioredis * feat: setup redis client * feat: add withRateLimit wrapper * feat: wrap trpc endpoint in withRateLimit * feat: wrap withRateLimit on remaining routes * feat: exclude webhook route from rate limiting * chore: update compose files and readme * refactor: move into api/db packages
This commit is contained in:
31
packages/db/src/redis.ts
Normal file
31
packages/db/src/redis.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import Redis from "ioredis";
|
||||
|
||||
let redisClient: Redis | null = null;
|
||||
|
||||
export function getRedisClient(): Redis | null {
|
||||
if (redisClient) {
|
||||
return redisClient;
|
||||
}
|
||||
|
||||
const redisUrl = process.env.REDIS_URL;
|
||||
|
||||
if (!redisUrl) {
|
||||
return null;
|
||||
}
|
||||
|
||||
redisClient = new Redis(redisUrl, {
|
||||
maxRetriesPerRequest: 3,
|
||||
enableReadyCheck: true,
|
||||
lazyConnect: true,
|
||||
});
|
||||
|
||||
return redisClient;
|
||||
}
|
||||
|
||||
export async function closeRedisClient(): Promise<void> {
|
||||
if (redisClient) {
|
||||
await redisClient.quit();
|
||||
redisClient = null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user