2026-04-06 19:36:13 +02:00
|
|
|
import express from 'express';
|
|
|
|
|
import { join, dirname } from 'path';
|
|
|
|
|
import { fileURLToPath } from 'url';
|
|
|
|
|
import { getDb } from './db.js';
|
|
|
|
|
|
|
|
|
|
// Routes
|
|
|
|
|
import indexRouter from './routes/index.js';
|
|
|
|
|
import testRouter from './routes/test.js';
|
|
|
|
|
import statusRouter from './routes/status.js';
|
|
|
|
|
import resultsRouter from './routes/results.js';
|
|
|
|
|
import historyRouter from './routes/history.js';
|
|
|
|
|
import siteRouter from './routes/site.js';
|
|
|
|
|
|
|
|
|
|
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
|
|
|
|
|
|
|
|
const app = express();
|
|
|
|
|
const PORT = process.env.PORT || 3132;
|
|
|
|
|
|
|
|
|
|
// Initialize DB on startup
|
|
|
|
|
getDb();
|
|
|
|
|
|
|
|
|
|
// View engine
|
|
|
|
|
app.set('views', join(__dirname, 'views'));
|
|
|
|
|
app.set('view engine', 'pug');
|
|
|
|
|
|
|
|
|
|
// Middleware
|
|
|
|
|
app.use(express.urlencoded({ extended: true }));
|
|
|
|
|
app.use(express.json());
|
|
|
|
|
|
2026-04-07 10:49:20 +02:00
|
|
|
// Static reports — use REPORTS_DIR env var so Docker and local both work
|
|
|
|
|
const REPORTS_DIR = process.env.REPORTS_DIR || join(__dirname, 'reports');
|
|
|
|
|
app.use('/reports', express.static(REPORTS_DIR));
|
2026-04-06 19:36:13 +02:00
|
|
|
|
|
|
|
|
// Routes
|
|
|
|
|
app.use('/', indexRouter);
|
|
|
|
|
app.use('/test', testRouter);
|
|
|
|
|
app.use('/status', statusRouter);
|
|
|
|
|
app.use('/results', resultsRouter);
|
|
|
|
|
app.use('/history', historyRouter);
|
|
|
|
|
app.use('/site', siteRouter);
|
|
|
|
|
|
|
|
|
|
// 404
|
|
|
|
|
app.use((req, res) => {
|
|
|
|
|
res.status(404).render('error', { title: '404 Not Found', message: 'Page not found.' });
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Error handler
|
|
|
|
|
app.use((err, req, res, _next) => {
|
|
|
|
|
console.error(err);
|
|
|
|
|
res.status(500).render('error', { title: 'Server Error', message: err.message });
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
app.listen(PORT, () => {
|
|
|
|
|
console.log(`Speedboard running at http://localhost:${PORT}`);
|
|
|
|
|
});
|