42 lines
1.4 KiB
JavaScript
42 lines
1.4 KiB
JavaScript
import express from 'express';
|
|
import path from 'node:path';
|
|
import { fileURLToPath } from 'node:url';
|
|
import { config, ensureRuntimeDirectories } from './src/config.js';
|
|
import { openDatabase } from './src/database.js';
|
|
import { createApiRouter } from './src/routes/api.js';
|
|
import { notFoundHandler, errorHandler } from './src/middleware/errors.js';
|
|
|
|
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
ensureRuntimeDirectories();
|
|
const db = openDatabase();
|
|
const app = express();
|
|
|
|
app.disable('x-powered-by');
|
|
app.use(express.json({ limit: '2mb' }));
|
|
app.use(express.urlencoded({ extended: true, limit: '2mb' }));
|
|
app.use('/media', express.static(path.join(config.storageDir, 'active'), {
|
|
fallthrough: false,
|
|
immutable: false,
|
|
setHeaders(res) {
|
|
res.setHeader('X-Content-Type-Options', 'nosniff');
|
|
}
|
|
}));
|
|
app.use('/api', createApiRouter(db));
|
|
app.use(express.static(path.join(__dirname, 'public'), { extensions: ['html'] }));
|
|
app.use(notFoundHandler);
|
|
app.use(errorHandler);
|
|
|
|
const server = app.listen(config.port, config.host, () => {
|
|
console.log(`${config.instanceName}: Wegwichtel läuft auf http://${config.host}:${config.port}`);
|
|
});
|
|
|
|
function shutdown(signal) {
|
|
console.log(`${signal}: Server wird beendet.`);
|
|
server.close(() => {
|
|
db.close();
|
|
process.exit(0);
|
|
});
|
|
}
|
|
process.on('SIGINT', () => shutdown('SIGINT'));
|
|
process.on('SIGTERM', () => shutdown('SIGTERM'));
|