tfrere's picture
tfrere HF Staff
refactor(backend): modular server split with new routes, persistence and agent layer
f6678ab
import "dotenv/config";
import { isHfStorageEnabled, getDatasetId, flushAll } from "./hf-storage.js";
import { isOAuthEnabled } from "./auth.js";
import { createApp } from "./create-app.js";
const PORT = parseInt(process.env.PORT || "8080", 10);
if (isHfStorageEnabled()) {
console.log("[server] HF Dataset persistence enabled");
} else if (getDatasetId()) {
console.log("[server] HF Dataset ID ready, waiting for first user login to activate persistence");
} else {
console.log("[server] HF Dataset persistence disabled (no SPACE_ID or HF_DATASET_ID)");
console.log("[server] falling back to local file persistence");
}
const oauthEnabled = isOAuthEnabled();
if (oauthEnabled) {
console.log("[server] HF OAuth enabled (SPACE_ID set)");
} else {
console.log("[server] HF OAuth disabled (no SPACE_ID), all users can edit");
}
const { httpServer } = createApp();
const server = httpServer.listen(PORT, async () => {
console.log(`[server] running on http://localhost:${PORT}`);
console.log(`[server] collab websocket at ws://localhost:${PORT}/collab`);
console.log(`[server] SPACE_ID=${process.env.SPACE_ID || "(not set)"} HF_DATASET_ID=${getDatasetId() || "(empty)"} HF_TOKEN=${process.env.HF_TOKEN ? "set" : "not set"}`);
});
async function gracefulShutdown(signal: string) {
console.log(`[server] ${signal} received, flushing to HF...`);
await flushAll();
await new Promise<void>((resolve) => {
server.close(() => resolve());
setTimeout(() => {
console.warn("[server] force-closing after timeout");
resolve();
}, 5000);
});
process.exit(0);
}
process.on("SIGTERM", () => gracefulShutdown("SIGTERM"));
process.on("SIGINT", () => gracefulShutdown("SIGINT"));