-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
36 lines (28 loc) · 1.08 KB
/
Copy pathserver.js
File metadata and controls
36 lines (28 loc) · 1.08 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
const express = require('express');
const http = require('http');
const { WebSocketServer } = require('ws');
const path = require('path');
const { initDb, purgeOldEvents } = require('./src/db');
const { createRouter } = require('./src/routes');
const { createWsManager } = require('./src/ws');
const PORT = process.env.PORT || 3000;
const PURGE_DAYS = parseInt(process.env.PURGE_DAYS || '7', 10);
const db = initDb();
const app = express();
const server = http.createServer(app);
// WebSocket
const wss = new WebSocketServer({ server });
const wsManager = createWsManager(wss);
// Trust proxy for correct req.ip behind Caddy
app.set('trust proxy', true);
// Body parsing — capture raw body for all content types
app.use(express.raw({ type: '*/*', limit: '10mb' }));
app.use(express.static(path.join(__dirname, 'public')));
// Routes
app.use(createRouter(db, wsManager));
// Auto-purge on startup and every hour
purgeOldEvents(db, PURGE_DAYS);
setInterval(() => purgeOldEvents(db, PURGE_DAYS), 60 * 60 * 1000);
server.listen(PORT, () => {
console.log(`Webhook catcher running on port ${PORT}`);
});