Events: everything the backend serves is under /api (for 2.3.0)

The site and its API can share one address with one proxy rule (/api to
the backend, everything else to the frontend). Status page at /api (/
redirects), /api/health, /api/docs/v2, /api/uploads. Stored upload
paths stay /uploads/… and are given out as /api/uploads/…. Old paths
still answer (/health, /uploads) or redirect (/docs, /docs/v2), so
two-address sites keep working. DEPLOYMENT.md and the agent plan
describe the single rule.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-17 11:11:46 +02:00
co-authored by Claude Opus 5.5
parent e8853cf0c8
commit afaccf79d1
14 changed files with 107 additions and 37 deletions
+25 -13
View File
@@ -153,8 +153,12 @@ app.use((req, res, next) => {
// Licence suspended, revoked or missing: no new or edited events, registrations or payments (src/hub/readOnly.js).
app.use(require('./hub/readOnly').licenceReadOnly);
// Everything the backend serves is under /api (2.3.0), so the site and its API can share one
// address: the web server sends /api to the backend and everything else to the frontend. The old
// paths (/, /health, /docs/v2, /uploads) still work for sites with a separate API address.
// Health for monitoring (and the Hub): no secrets, no personal information.
app.get('/health', async (req, res) => {
app.get(['/api/health', '/health'], async (req, res) => {
const report = require('./hub/report');
let db = true;
try { await prisma.$queryRaw`SELECT 1`; } catch { db = false; }
@@ -174,11 +178,11 @@ app.use('/api/v2', linksRouter);
app.use('/api/v2', require('./v2').v2Router());
// Links in messages already sent: v1 addresses answer 308 with their v2 one.
app.use(legacyLinkRedirects);
app.use('/docs/v2', require('./v2').v2DocsRouter());
// v1 (/api/…) was removed in 2.0. Still answered: provider webhooks (above, and WAWP's old
// address) and links in messages already sent (308s above, a mail app's one-click POST here).
app.use('/api', legacyV1Router);
app.use('/api', v1Removed);
app.use('/api/docs/v2', require('./v2').v2DocsRouter());
app.get('/api/docs', (req, res) => res.redirect(301, '/api/docs/v2'));
// Before 2.3.0 the reference was at /docs/v2 (bookmarks, the old status page).
app.use('/docs/v2', (req, res) => res.redirect(301, `/api${req.originalUrl}`));
app.get('/docs', (req, res) => res.redirect(301, '/api/docs/v2'));
// Pre-warm the settings cache so synchronous helpers have DB values from startup
const { getSettingSync, warmCache } = require('./utils/settingsCache');
@@ -187,12 +191,16 @@ warmCache().catch(() => {});
// handlers. Serving them inline lets a compromised/malicious upload run script
// in the site's origin if opened directly, so pin the safe response headers
// (no inline execution, no MIME-sniffing to HTML/script) on every asset here.
app.use('/uploads', express.static('public/uploads', {
// Stored paths stay "/uploads/…"; they're given out as /api/uploads/… (v2/schemas.ts absoluteUrl).
// /uploads still answers for links made before 2.3.0.
const uploads = express.static('public/uploads', {
setHeaders: (res) => {
res.setHeader('Content-Security-Policy', "default-src 'none'; style-src 'unsafe-inline'; sandbox");
res.setHeader('X-Content-Type-Options', 'nosniff');
},
}));
});
app.use('/api/uploads', uploads, notFound); // a missing file is 404, not "API v1 removed"
app.use('/uploads', uploads);
// ── Shared page helpers ────────────────────────────────────────────────────────
@@ -268,8 +276,9 @@ app.get('/favicon.ico', (req, res) => {
res.sendFile(path.join(__dirname, '..', 'src', 'favicon.ico'));
});
// ── Root route — status page ────────────────────────────────────────────────
app.get('/', async (req, res) => {
// ── Status page, at /api (2.3.0); / sends you there ─────────────────────────
app.get('/', (req, res) => res.redirect(302, '/api'));
app.get('/api', async (req, res) => {
const mem = process.memoryUsage();
const uptimeSec = process.uptime();
const now = new Date().toISOString();
@@ -335,15 +344,18 @@ app.get('/', async (req, res) => {
</div>
<div class="note">
API documentation (OpenAPI) is at <a href="/docs/v2">/docs/v2</a> — it needs an admin account token.
API documentation (OpenAPI) is at <a href="/api/docs/v2">/api/docs/v2</a> — it needs an admin account token.
</div>
`);
res.send(html);
});
// /docs: the v1 reference went with v1 in 2.0; the API is documented at /docs/v2 (OpenAPI).
app.get('/docs', (req, res) => res.redirect(301, '/docs/v2'));
// v1 (/api/…) was removed in 2.0. Still answered: provider webhooks (above, and WAWP's old
// address) and links in messages already sent (308s above, a mail app's one-click POST here).
// Mounted after everything else under /api, since v1Removed answers 410 for any other /api path.
app.use('/api', legacyV1Router);
app.use('/api', v1Removed);
// Error middleware
app.use(notFound);