Skip to content

Commit

Permalink
introduce rate limit variables
Browse files Browse the repository at this point in the history
  • Loading branch information
schmidp committed Apr 5, 2023
1 parent 415eb21 commit 129336c
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,16 @@ export default class ChatServer {
this.app.use(express.json({ limit: '1mb' }));
this.app.use(compression());

if (process.env.DISABLE_RATE_LIMIT !== 'true') {
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
});

this.app.use(limiter);
}

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: rateLimitWindowMs,
max: rateLimitMax,
});
this.app.use(limiter);

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

0 comments on commit 129336c

Please sign in to comment.