fertig-classic-games/server/index.js

44 lines
1.3 KiB
JavaScript

import http from 'node:http';
import path from 'node:path';
import express from 'express';
import cookieParser from 'cookie-parser';
import config from './config.js';
import './db/index.js';
import { loadUser } from './auth/middleware.js';
import authRoutes from './auth/routes.js';
import profileRoutes from './profile/routes.js';
import historyRoutes from './history/routes.js';
import { listGames } from './multiplayer/gameRegistry.js';
import { attachMultiplayer } from './multiplayer/index.js';
const app = express();
app.use(express.json({ limit: '1mb' }));
app.use(cookieParser());
app.use(loadUser);
app.get('/api/health', (_req, res) => res.json({ ok: true }));
app.get('/api/games', (_req, res) => res.json({ games: listGames() }));
app.use('/api/auth', authRoutes);
app.use('/api/profile', profileRoutes);
app.use('/api/history', historyRoutes);
app.use(express.static(config.publicDir, { extensions: ['html'] }));
app.get('*', (_req, res) => {
res.sendFile(path.join(config.publicDir, 'index.html'));
});
app.use((err, _req, res, _next) => {
console.error('[server]', err);
res.status(500).json({ error: 'Internal server error.' });
});
const server = http.createServer(app);
attachMultiplayer(server);
server.listen(config.port, config.host, () => {
console.log(`[server] listening on http://${config.host}:${config.port}`);
});