Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
81 lines (66 loc) · 2.95 KB
/
Copy pathserver.js
File metadata and controls
81 lines (66 loc) · 2.95 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
72
73
74
75
76
77
78
79
80
81
require('dotenv').config();
const express = require('express');
const path = require('path');
const fs = require('fs');
const APP_VERSION = require('./package.json').version;
const app = express();
const PORT = process.env.PORT || 3233;
app.set('trust proxy', 1);
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
// Single source of truth for cache busting — views and the service worker
// all derive their asset versions from package.json
app.locals.assetV = APP_VERSION;
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// Serve the service worker with the app version injected so its cache name and
// precache list stay in sync with the views. Registered before express.static
// so this route wins over the raw file in public/.
const swBody = fs
.readFileSync(path.join(__dirname, 'public', 'sw.js'), 'utf8')
.replaceAll('__APP_VERSION__', APP_VERSION);
app.get('/sw.js', (req, res) => {
res.setHeader('Content-Type', 'application/javascript');
res.setHeader('Cache-Control', 'no-cache');
res.send(swBody);
});
app.use(express.static(path.join(__dirname, 'public')));
const { router: apiRouter } = require('./routes/api');
const trailerRouter = require('./routes/trailer');
const pagesRouter = require('./routes/pages');
const { runLoadJob } = require('./services/loader');
const db = require('./db/database');
app.get('/health', (req, res) => {
try {
db.healthCheck();
res.json({ ok: true, version: APP_VERSION });
} catch (err) {
res.status(500).json({ ok: false });
}
});
app.use('/api', apiRouter);
app.use('/api', trailerRouter);
app.use('/', pagesRouter);
// Safety net for fire-and-forget background jobs — log instead of crashing
process.on('unhandledRejection', (err) => {
console.error('[unhandled rejection]', err);
});
// ── Background refresh scheduler ─────────────────────────────────────────────
// Periodically re-fetches Steam data and rebuilds recommendations for active users.
// Staggered 30s apart to avoid hammering the Steam API.
const REFRESH_INTERVAL_MS = (parseFloat(process.env.REFRESH_INTERVAL_HOURS) || 24) * 60 * 60 * 1000;
async function runScheduledRefresh() {
const users = db.getActiveUsers(30); // active in last 30 days
if (!users.length) return;
console.log(`[refresh] Scheduled refresh starting for ${users.length} user(s)`);
for (let i = 0; i < users.length; i++) {
if (i > 0) await new Promise(r => setTimeout(r, 30_000));
console.log(`[refresh] Refreshing ${users[i]} (${i + 1}/${users.length})`);
runLoadJob(users[i]).catch(err => console.error('[refresh] job failed:', err));
}
}
setInterval(runScheduledRefresh, REFRESH_INTERVAL_MS).unref();
console.log(`[refresh] Background refresh scheduled every ${process.env.REFRESH_INTERVAL_HOURS || 24}h`);
app.listen(PORT, '0.0.0.0', () => {
console.log(`ShelfLife running on http://0.0.0.0:${PORT}`);
});