"use strict"; const express = require("express"); const path = require("path"); const db = require("./db"); const app = express(); const PORT = process.env.PORT || 7860; app.use(express.json()); app.use(express.static(__dirname)); // Serve static files (html, css, js) // API: Get all rosters app.get("/api/roster", (req, res) => { try { const data = db.getRosters(); res.json(data); } catch (err) { console.error("[WEB] Error fetching roster:", err.message); res.status(500).json({ error: "Failed to fetch roster" }); } }); // API: Add roster (optional, mainly used by web UI if needed) app.post("/api/roster", (req, res) => { const { team, user, role } = req.body; if (!team || !user || !role) { return res.status(400).json({ error: "Missing team, user, or role" }); } try { db.addRoster(team, user, role); res.json({ success: true }); } catch (err) { console.error("[WEB] Error adding roster:", err.message); res.status(500).json({ error: "Failed to add roster" }); } }); // Serve Dashboard app.get("/", (req, res) => { res.sendFile(path.resolve(__dirname, "dashboard.html")); }); // Serve League Page app.get("/league", (req, res) => { res.sendFile(path.resolve(__dirname, "league.html")); }); // Serve Roster Page app.get("/roster", (req, res) => { res.sendFile(path.resolve(__dirname, "roster.html")); }); app.listen(PORT, "0.0.0.0", () => { console.log(`[WEB] Server running on port ${PORT}`); console.log(`[WEB] Dashboard: http://localhost:${PORT}/`); console.log(`[WEB] League: http://localhost:${PORT}/league`); console.log(`[WEB] Roster: http://localhost:${PORT}/roster`); }); module.exports = app;