edtech / apps /api /src /middleware /rateLimit.ts
CognxSafeTrack
chore: stabilization, audit fixes and project synchronization
7b0c22b
import { FastifyInstance, FastifyRequest } from 'fastify';
import rateLimit from '@fastify/rate-limit';
import { logger } from '../logger';
export async function setupRateLimit(server: FastifyInstance) {
try {
await server.register(rateLimit, {
max: 100,
timeWindow: '1 minute',
keyGenerator: (req: FastifyRequest) => req.ip as string,
errorResponseBuilder: (_request: FastifyRequest, context: { after: string }) => {
return {
statusCode: 429,
error: 'Too Many Requests',
message: `Rate limit exceeded. Try again in ${context.after}`
};
}
});
logger.info('[RATE-LIMIT] Configured successfully.');
} catch (err) {
logger.error({ err }, "[RATE-LIMIT] Initialization failed:");
}
}