import path from "node:path"; import fs from "node:fs"; import { RelayServer } from "./relay/index.js"; import { loadDbConfig, createStores } from "./relay/db/index.js"; const port = parseInt(process.env.PORT ?? "8080", 10); const host = process.env.HOST ?? "0.0.0.0"; async function main() { // Load saved database config (if any) const dbConfig = loadDbConfig(); let stores; if (dbConfig) { console.log(`Loading ${dbConfig.type} database...`); stores = await createStores(dbConfig); } else { console.log("No database configured — starting with in-memory storage."); console.log("Configure a database during setup to enable persistent storage."); } // Resolve frontend public directory: check ./public (production) then ../web/dist (development) let publicDir: string | undefined; const candidates = [ path.resolve("public"), path.resolve(__dirname, "..", "web", "dist"), ]; for (const dir of candidates) { if (fs.existsSync(path.join(dir, "index.html"))) { publicDir = dir; break; } } const server = new RelayServer({ port, host, stores, publicDir }); await server.start(); console.log(`CookieBridge relay server listening on ${host}:${port}`); if (publicDir) { console.log(`Serving admin UI from ${publicDir}`); } else { console.log("No frontend build found — admin UI not available. Run: npm run build:web"); } process.on("SIGINT", async () => { console.log("\nShutting down..."); await server.stop(); process.exit(0); }); process.on("SIGTERM", async () => { await server.stop(); process.exit(0); }); } main().catch((err) => { console.error("Failed to start server:", err); process.exit(1); });