CognxSafeTrack commited on
Commit
6f87c06
·
1 Parent(s): 071d56f

fix: resolve IDE errors and missing middleware in API index

Browse files
apps/api/src/index.ts CHANGED
@@ -14,7 +14,18 @@ import { analyticsRoutes } from './routes/analytics';
14
  import { internalRoutes } from './routes/internal';
15
  import { authRoutes } from './routes/auth';
16
  import { setupRateLimit } from './middleware/rate-limit';
17
- import { startCleanupCron } from './scripts/cleanup-temp-files';
 
 
 
 
 
 
 
 
 
 
 
18
 
19
  const server: FastifyInstance = fastify({
20
  logger: true,
@@ -107,7 +118,7 @@ server.get('/health', async (_req, reply) => {
107
  try {
108
  await prisma.$queryRaw`SELECT 1`;
109
  return { status: 'ok', database: 'connected' };
110
- } catch (err) {
111
  reply.status(500).send({ status: 'error', database: 'disconnected' });
112
  }
113
  });
@@ -124,7 +135,7 @@ const start = async () => {
124
  logger.info(`🚀 Server listening on http://0.0.0.0:${port}`);
125
 
126
  // Background tasks
127
- await setupRateLimit().catch(err => logger.error('[STARTUP] Rate limit error:', err));
128
  startCleanupCron();
129
 
130
  } catch (err: any) {
 
14
  import { internalRoutes } from './routes/internal';
15
  import { authRoutes } from './routes/auth';
16
  import { setupRateLimit } from './middleware/rate-limit';
17
+ import { cleanupFiles as startCleanupCron } from './services/cleanup';
18
+
19
+ declare module 'fastify' {
20
+ interface FastifyRequest {
21
+ jwtVerify(): Promise<void>;
22
+ user: {
23
+ id: string;
24
+ organizationId: string;
25
+ role: string;
26
+ };
27
+ }
28
+ }
29
 
30
  const server: FastifyInstance = fastify({
31
  logger: true,
 
118
  try {
119
  await prisma.$queryRaw`SELECT 1`;
120
  return { status: 'ok', database: 'connected' };
121
+ } catch (err: any) {
122
  reply.status(500).send({ status: 'error', database: 'disconnected' });
123
  }
124
  });
 
135
  logger.info(`🚀 Server listening on http://0.0.0.0:${port}`);
136
 
137
  // Background tasks
138
+ await setupRateLimit(server).catch(err => logger.error('[STARTUP] Rate limit error:', err));
139
  startCleanupCron();
140
 
141
  } catch (err: any) {
apps/api/src/middleware/rate-limit.ts ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { FastifyInstance } from 'fastify';
2
+ import rateLimit from '@fastify/rate-limit';
3
+ import { logger } from '../logger';
4
+
5
+ export async function setupRateLimit(server: FastifyInstance) {
6
+ try {
7
+ await server.register(rateLimit, {
8
+ max: 100,
9
+ timeWindow: '1 minute',
10
+ errorResponseBuilder: (request, context) => {
11
+ return {
12
+ statusCode: 429,
13
+ error: 'Too Many Requests',
14
+ message: `Rate limit exceeded. Try again in ${context.after}`
15
+ };
16
+ }
17
+ });
18
+ logger.info('[RATE-LIMIT] Configured successfully.');
19
+ } catch (err) {
20
+ logger.error('[RATE-LIMIT] Initialization failed:', err);
21
+ }
22
+ }