-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
71 lines (62 loc) · 1.89 KB
/
Copy pathserver.js
File metadata and controls
71 lines (62 loc) · 1.89 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
63
64
65
66
67
68
69
70
71
import 'dotenv/config';
import express from 'express';
import helmet from 'helmet';
import rateLimit from 'express-rate-limit';
import compression from 'compression';
import cors from 'cors';
import { PORT, RATE_LIMIT_WINDOW_MS, RATE_LIMIT_MAX, PAYLOAD_LIMIT } from './config/constants.js';
import analyzeRouter from './routes/analyze.js';
import chatRouter from './routes/chat.js';
import insightsRouter from './routes/insights.js';
const app = express();
app.use(helmet({
contentSecurityPolicy: {
directives: {
defaultSrc: ["'self'"],
scriptSrc: ["'self'", "'unsafe-inline'", "https://cdn.jsdelivr.net"],
styleSrc: ["'self'", "'unsafe-inline'", "https://fonts.googleapis.com"],
fontSrc: ["'self'", "https://fonts.gstatic.com"],
imgSrc: ["'self'", "data:"],
connectSrc: ["'self'"],
},
},
}));
app.use(cors());
app.use(express.json({ limit: PAYLOAD_LIMIT }));
app.use(compression());
const limiter = rateLimit({
windowMs: RATE_LIMIT_WINDOW_MS,
max: RATE_LIMIT_MAX,
standardHeaders: true,
legacyHeaders: false,
message: { error: 'Too many requests, please try again later.' },
});
app.use('/api/', limiter);
app.use(express.static('public', {
maxAge: '7d',
etag: true,
lastModified: true,
setHeaders(res, path) {
if (path.endsWith('.css') || path.endsWith('.js')) {
res.setHeader('Cache-Control', 'public, max-age=604800, immutable');
}
},
}));
app.use('/api', analyzeRouter);
app.use('/api', chatRouter);
app.use('/api', insightsRouter);
app.get('/api/health', (req, res) => {
res.json({
status: 'ok',
service: 'MindMate AI',
timestamp: new Date().toISOString(),
});
});
const isDirectRun = process.argv[1] &&
import.meta.url.endsWith(process.argv[1].replace(/\\/g, '/'));
if (isDirectRun) {
app.listen(PORT, () => {
console.log(` MindMate AI server running on http://localhost:${PORT}`);
});
}
export default app;