Download apps/api/src/middleware/rateLimit.ts from safetrack/edtech: direct link, hf CLI and curl.
- Browser
- Download file 876 Bytes
-
https://huggingface.co/spaces/safetrack/edtech/resolve/main/apps/api/src/middleware/rateLimit.ts
- Command line
-
hf download hf://spaces/safetrack/edtech/apps/api/src/middleware/rateLimit.ts
-
curl -L -o rateLimit.ts https://huggingface.co/spaces/safetrack/edtech/resolve/main/apps/api/src/middleware/rateLimit.ts
876 Bytes
| 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:"); | |
| } | |
| } | |