import express from 'express'; import cors from 'cors'; import { resolve, dirname } from 'path'; import { fileURLToPath } from 'url'; import { existsSync } from 'fs'; import { initDb } from './db.js'; import mapsRouter from './routes/maps.js'; import hexesRouter from './routes/hexes.js'; const __dirname = dirname(fileURLToPath(import.meta.url)); const PORT = Number(process.env.PORT) || 3002; const DIST_DIR = resolve(__dirname, '..', 'dist'); const TILES_DIR = resolve(__dirname, '..', 'tiles'); const PUBLIC_TILES_DIR = resolve(__dirname, '..', 'public', 'tiles'); async function main() { await initDb(); const app = express(); app.use(cors()); app.use(express.json({ limit: '10mb' })); // API routes app.use('/api/v1/maps', mapsRouter); app.use('/api/v1/maps', hexesRouter); // Serve tiles (check multiple locations) const tilesPath = existsSync(TILES_DIR) ? TILES_DIR : PUBLIC_TILES_DIR; if (existsSync(tilesPath)) { app.use('/tiles', express.static(tilesPath, { maxAge: '30d', immutable: true })); console.log(`[server] Serving tiles from ${tilesPath}`); } // Serve static frontend if (existsSync(DIST_DIR)) { app.use(express.static(DIST_DIR)); // SPA fallback (Express 5 syntax) app.get('/{*splat}', (_req, res) => { res.sendFile(resolve(DIST_DIR, 'index.html')); }); console.log(`[server] Serving frontend from ${DIST_DIR}`); } app.listen(PORT, () => { console.log(`[server] Hexifyer running on http://localhost:${PORT}`); }); } main().catch(err => { console.error('Failed to start:', err); process.exit(1); });