CognxSafeTrack commited on
Commit
a7a1900
Β·
1 Parent(s): 9595ade

fix(index): use dynamic import for rate-limit to fix local TS module error

Browse files
Files changed (1) hide show
  1. apps/api/src/index.ts +16 -9
apps/api/src/index.ts CHANGED
@@ -1,6 +1,5 @@
1
  import Fastify from 'fastify';
2
  import cors from '@fastify/cors';
3
- import rateLimit from '@fastify/rate-limit';
4
  import { whatsappRoutes } from './routes/whatsapp';
5
  import { adminRoutes } from './routes/admin';
6
  import { aiRoutes } from './routes/ai';
@@ -14,19 +13,26 @@ const server = Fastify({
14
  // ── CORS ───────────────────────────────────────────────────────────────────────
15
  server.register(cors);
16
 
17
- // ── Rate Limiting (global defaults) ───────────────────────────────────────────
18
- server.register(rateLimit, {
19
- global: true,
20
- max: 300,
21
- timeWindow: '1 minute',
22
- });
 
 
 
 
 
 
 
 
23
 
24
  // ── Public Routes (no auth) ────────────────────────────────────────────────────
25
  server.register(whatsappRoutes, { prefix: '/v1/whatsapp' });
26
 
27
  // ── Private Routes (require ADMIN_API_KEY) ─────────────────────────────────────
28
- // Inline addHook on an isolated scope guarantees the auth check fires BEFORE
29
- // any child route handler, properly encapsulated in Fastify v4.
30
  server.register(async function guardedRoutes(scope) {
31
  scope.addHook('onRequest', async (request, reply) => {
32
  const apiKey = process.env.ADMIN_API_KEY;
@@ -64,6 +70,7 @@ server.get('/health', async () => {
64
  // ── Start Server ───────────────────────────────────────────────────────────────
65
  const start = async () => {
66
  try {
 
67
  const port = parseInt(process.env.PORT || '3001');
68
  await server.listen({ port, host: '0.0.0.0' });
69
  console.log(`Server listening on http://0.0.0.0:${port}`);
 
1
  import Fastify from 'fastify';
2
  import cors from '@fastify/cors';
 
3
  import { whatsappRoutes } from './routes/whatsapp';
4
  import { adminRoutes } from './routes/admin';
5
  import { aiRoutes } from './routes/ai';
 
13
  // ── CORS ───────────────────────────────────────────────────────────────────────
14
  server.register(cors);
15
 
16
+ // ── Rate Limiting (lazy import β€” package installed at runtime on HF) ───────────
17
+ async function setupRateLimit() {
18
+ try {
19
+ const rateLimit = await import('@fastify/rate-limit');
20
+ await server.register(rateLimit.default, {
21
+ global: true,
22
+ max: 300,
23
+ timeWindow: '1 minute',
24
+ });
25
+ console.log('[RATE-LIMIT] Rate limiting enabled (300 req/min global)');
26
+ } catch {
27
+ console.warn('[RATE-LIMIT] @fastify/rate-limit not available β€” skipping');
28
+ }
29
+ }
30
 
31
  // ── Public Routes (no auth) ────────────────────────────────────────────────────
32
  server.register(whatsappRoutes, { prefix: '/v1/whatsapp' });
33
 
34
  // ── Private Routes (require ADMIN_API_KEY) ─────────────────────────────────────
35
+ // Inline addHook on guardedRoutes scope β€” properly encapsulated in Fastify v4
 
36
  server.register(async function guardedRoutes(scope) {
37
  scope.addHook('onRequest', async (request, reply) => {
38
  const apiKey = process.env.ADMIN_API_KEY;
 
70
  // ── Start Server ───────────────────────────────────────────────────────────────
71
  const start = async () => {
72
  try {
73
+ await setupRateLimit();
74
  const port = parseInt(process.env.PORT || '3001');
75
  await server.listen({ port, host: '0.0.0.0' });
76
  console.log(`Server listening on http://0.0.0.0:${port}`);