80 lines
3.4 KiB
JavaScript
80 lines
3.4 KiB
JavaScript
#!/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);
|
|
}
|
|
|
|
// Lancé en root (image de production) : on rend DATA_DIR à l'utilisateur du
|
|
// serveur, puis on abandonne les droits root avant tout le reste. Un volume
|
|
// CapRover créé avant l'image appartient à root : sans cela, le serveur ne
|
|
// pourrait pas y écrire (incident du 2026-11-19).
|
|
if (typeof process.getuid === "function" && process.getuid() === 0) {
|
|
const uid = Number(process.env.RUN_AS_UID ?? 1000);
|
|
const gid = Number(process.env.RUN_AS_GID ?? uid);
|
|
try {
|
|
if (!fs.existsSync(DATA_DIR)) fs.mkdirSync(DATA_DIR);
|
|
/** @param {string} p */
|
|
const own = (p) => {
|
|
fs.chownSync(p, uid, gid);
|
|
if (fs.statSync(p).isDirectory()) for (const f of fs.readdirSync(p)) own(path.join(p, f));
|
|
};
|
|
own(DATA_DIR);
|
|
/** @type {any} */ (process).setgid(gid);
|
|
/** @type {any} */ (process).setuid(uid);
|
|
} catch (e) {
|
|
fail(`impossible d'abandonner les droits root : ${e instanceof Error ? e.message : e}`);
|
|
}
|
|
}
|
|
|
|
// 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 {
|
|
// Sans `recursive` : le parent doit exister (sous /proc, Node 24 boucle sur
|
|
// un mkdir récursif).
|
|
if (!fs.existsSync(DATA_DIR)) fs.mkdirSync(DATA_DIR);
|
|
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)));
|