-
Notifications
You must be signed in to change notification settings - Fork 785
Expand file tree
/
Copy pathstart-backend.js
More file actions
47 lines (40 loc) · 1.25 KB
/
start-backend.js
File metadata and controls
47 lines (40 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
const { spawn } = require("child_process");
const path = require("path");
const fs = require("fs");
const backendDir = path.resolve(__dirname, "backend");
const venvBin = process.platform === "win32"
? path.join(backendDir, "venv", "Scripts", "python.exe")
: path.join(backendDir, "venv", "bin", "python3");
if (!fs.existsSync(venvBin)) {
console.error(`[!] Python venv not found at: ${venvBin}`);
console.error("[!] Run start.sh (Mac/Linux) or start.bat (Windows) first to create the venv.");
process.exit(1);
}
const backendArgs = ["-m", "uvicorn", "main:app", "--timeout-keep-alive", "120"];
if (["1", "true", "yes"].includes(String(process.env.BACKEND_RELOAD || "").toLowerCase())) {
backendArgs.push("--reload");
}
console.log(`[*] Starting backend with: ${venvBin} ${backendArgs.join(" ")}`);
const backendProc = spawn(venvBin, backendArgs, {
cwd: backendDir,
stdio: "inherit",
env: process.env,
});
const cleanupAll = () => {
if (backendProc && !backendProc.killed) {
backendProc.kill();
}
};
process.on("exit", cleanupAll);
process.on("SIGINT", () => {
cleanupAll();
process.exit(0);
});
process.on("SIGTERM", () => {
cleanupAll();
process.exit(0);
});
backendProc.on("exit", (code) => {
cleanupAll();
process.exit(code ?? 0);
});