import express from "express"; import path from "path"; import fs from "fs"; import { createServer as createViteServer } from "vite"; import { defaultCMSData } from "./src/data/defaultCMS.js"; const app = express(); const PORT = 3000; const DATA_FILE = path.join(process.cwd(), "cms-data.json"); app.use(express.json({ limit: "10mb" })); // Helper to read persistent CMS data function loadCMSData() { try { if (fs.existsSync(DATA_FILE)) { const raw = fs.readFileSync(DATA_FILE, "utf-8"); return JSON.parse(raw); } } catch (err) { console.error("Failed to load CMS file, using defaults:", err); } return defaultCMSData; } // Helper to save CMS data function saveCMSData(data: any) { try { fs.writeFileSync(DATA_FILE, JSON.stringify(data, null, 2), "utf-8"); } catch (err) { console.error("Failed to save CMS file:", err); } } // Initialize file if not existing if (!fs.existsSync(DATA_FILE)) { saveCMSData(defaultCMSData); } // CMS API Routes app.get("/api/cms", (req, res) => { const data = loadCMSData(); res.json(data); }); app.post("/api/cms", (req, res) => { const newCMS = req.body; if (!newCMS || typeof newCMS !== "object") { return res.status(400).json({ error: "Invalid CMS data payload" }); } saveCMSData(newCMS); res.json({ success: true, data: newCMS }); }); app.post("/api/cms/reset", (req, res) => { saveCMSData(defaultCMSData); res.json({ success: true, data: defaultCMSData }); }); // Start Express + Vite Dev or Production static server async function start() { if (process.env.NODE_ENV !== "production") { const vite = await createViteServer({ server: { middlewareMode: true }, appType: "spa", }); app.use(vite.middlewares); } else { const distPath = path.join(process.cwd(), "dist"); app.use(express.static(distPath)); app.get("*", (req, res) => { res.sendFile(path.join(distPath, "index.html")); }); } app.listen(PORT, "0.0.0.0", () => { console.log(`[Gothic2 Mod Server] Running on http://0.0.0.0:${PORT}`); }); } start();