-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
62 lines (51 loc) · 2.07 KB
/
server.ts
File metadata and controls
62 lines (51 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import express from 'express';
import cors from 'cors';
import helmet from 'helmet';
import path from 'path';
import authRoutes from './src/server/routes/auth';
import notesRoutes from './src/server/routes/notes';
import agentsRoutes from './src/server/routes/agents';
import { purgeExpiredTokens } from './src/server/db';
import { lobsterRateLimiter } from './src/server/middleware/rateLimiter';
async function startServer() {
const app = express();
const PORT = process.env.PORT ? parseInt(process.env.PORT, 10) : 8383;
// Initialize Reef: Purge expired tokens on startup
purgeExpiredTokens();
// Set interval to purge every hour
setInterval(purgeExpiredTokens, 60 * 60 * 1000);
// Security Middlewares
app.use(helmet({
contentSecurityPolicy: false, // Disabled for Vite HMR in dev
crossOriginEmbedderPolicy: false, // Don't restrict cross-origin resources
crossOriginResourcePolicy: false, // Don't restrict cross-origin resource loading
crossOriginOpenerPolicy: false, // Don't isolate — let crypto.subtle use JS fallback
originAgentCluster: false, // Don't force origin-keyed clusters (fixes console warning)
}));
app.use(cors({
origin: process.env.CORS_ORIGIN
? process.env.CORS_ORIGIN.split(',').map(o => o.trim())
: true, // 'true' = mirror request origin — allows any LAN IP without configuration
credentials: true,
}));
app.use(express.json());
// API Routes
app.use('/api/auth', authRoutes);
app.use('/api/notes', lobsterRateLimiter, notesRoutes);
app.use('/api/agents', agentsRoutes);
app.get('/api/health', (req, res) => {
res.json({ status: 'ok' });
});
// In production (or when decoupling frontend/backend), serve static files if dist exists
if (process.env.NODE_ENV === 'production') {
const distPath = path.join(process.cwd(), 'dist');
app.use(express.static(distPath));
app.get('*', (req, res) => {
res.sendFile(path.join(distPath, 'index.html'));
});
}
app.listen(PORT, '0.0.0.0', () => {
console.log(`Server running on http://localhost:${PORT}`);
});
}
startServer();