Skip to content

Commit

Permalink
Merge pull request cogentapps#98 from openresearch/feature/no_rate_limit
Browse files Browse the repository at this point in the history
introduce env variable DISABLE_RATE_LIMIT to disable rate limit
  • Loading branch information
cogentapps authored Apr 12, 2023
2 parents 6814713 + 129336c commit 019cd42
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,15 @@ export default class ChatServer {
this.app.use(express.json({ limit: '1mb' }));
this.app.use(compression());


const rateLimitWindowMs = process.env.RATE_LIMIT_WINDOW_MS ? parseInt(process.env.RATE_LIMIT_WINDOW_MS, 10) : 15 * 60 * 1000; // 15 minutes
const rateLimitMax = process.env.RATE_LIMIT_MAX ? parseInt(process.env.RATE_LIMIT_MAX, 10) : 100; // limit each IP to 100 requests per windowMs

const { default: rateLimit } = await import('express-rate-limit'); // esm
const limiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 100, // limit each IP to 100 requests per windowMs
windowMs: rateLimitWindowMs,
max: rateLimitMax,
});

this.app.use(limiter);

this.app.get('/chatapi/health', (req, res) => new HealthRequestHandler(this, req, res));
Expand Down

0 comments on commit 019cd42

Please sign in to comment.