#!/usr/bin/env node // Point d'entrée du serveur d'IA-Fit (lot 2, ADR 0005 et 0007) : Node seul, // sans dépendance. // // Variables : // PORT port d'écoute (3000) // DATA_DIR journaux chiffrés et secret du serveur (/data en production) // SITE_DIR site assemblé par make site (/srv/site en production) // PUBLIC_URL adresse publique (https://ia-fit.sans.pub), pour OAuth et MCP import http from "node:http"; import fs from "node:fs"; import path from "node:path"; import { createApp } from "./app.js"; const PORT = Number(process.env.PORT ?? 3000); const DATA_DIR = path.resolve(process.env.DATA_DIR ?? "data"); const SITE_DIR = path.resolve(process.env.SITE_DIR ?? "build/site"); const PUBLIC_URL = (process.env.PUBLIC_URL ?? "https://ia-fit.sans.pub").replace(/\/+$/, ""); /** @param {string} message */ function fail(message) { console.error("IA-Fit ne démarre pas : " + message); process.exit(1); } // Refus de démarrer sans dossier de données inscriptible : sinon le premier // dépôt échouerait, et le secret OAuth ne pourrait pas être gardé. try { fs.mkdirSync(DATA_DIR, { recursive: true }); const probe = path.join(DATA_DIR, `.ecriture-${process.pid}`); fs.writeFileSync(probe, ""); fs.unlinkSync(probe); } catch (e) { fail(`DATA_DIR (${DATA_DIR}) n'est pas inscriptible : ${e instanceof Error ? e.message : e}`); } if (!fs.existsSync(path.join(SITE_DIR, "index.html"))) fail(`SITE_DIR (${SITE_DIR}) ne contient pas index.html (make site).`); if (!/^https?:\/\/[^/]+$/.test(PUBLIC_URL)) fail(`PUBLIC_URL (${PUBLIC_URL}) doit être de la forme https://domaine, sans chemin.`); const app = createApp({ dataDir: DATA_DIR, siteDir: SITE_DIR, publicUrl: PUBLIC_URL }); const server = http.createServer(app.handler); server.headersTimeout = 20e3; server.requestTimeout = 60e3; // Effacement des journaux inactifs : au démarrage, puis chaque jour. const purge = () => app.relay.purgeInactive() .then((n) => n && console.log(`Journaux inactifs effacés : ${n}`)) .catch((e) => console.error("Effacement des journaux inactifs :", e instanceof Error ? e.message : e)); purge(); setInterval(purge, 864e5).unref(); server.listen(PORT, () => console.log(`IA-Fit : port ${PORT}, site ${SITE_DIR}, données ${DATA_DIR}, adresse ${PUBLIC_URL}`)); for (const signal of /** @type {const} */ (["SIGTERM", "SIGINT"])) process.on(signal, () => server.close(() => process.exit(0)));