Spaces:
Sleeping
Sleeping
| import { readFile, writeFile } from "node:fs/promises"; | |
| import { spawn } from "node:child_process"; | |
| const CONFIG_PATH = "dist/server/wrangler.json"; | |
| const PASSTHROUGH_ENV = ["ENV_BASE_URL", "API_BASE_URL", "MODEL_NAME", "HF_TOKEN", "API_KEY"]; | |
| const rawConfig = await readFile(CONFIG_PATH, "utf8"); | |
| const config = JSON.parse(rawConfig); | |
| config.vars = { ...(config.vars || {}) }; | |
| for (const key of PASSTHROUGH_ENV) { | |
| if (process.env[key]) { | |
| config.vars[key] = process.env[key]; | |
| } | |
| } | |
| config.compatibility_flags = Array.from( | |
| new Set([...(config.compatibility_flags || []), "nodejs_compat_populate_process_env"]), | |
| ); | |
| await writeFile(CONFIG_PATH, `${JSON.stringify(config)}\n`); | |
| const port = process.env.PORT || "7860"; | |
| const child = spawn( | |
| "npx", | |
| [ | |
| "wrangler", | |
| "dev", | |
| "--config", | |
| CONFIG_PATH, | |
| "--ip", | |
| "0.0.0.0", | |
| "--port", | |
| port, | |
| "--inspector-ip", | |
| "0.0.0.0", | |
| "--inspector-port", | |
| "9230", | |
| "--log-level", | |
| "error", | |
| ], | |
| { stdio: "inherit" }, | |
| ); | |
| child.on("exit", (code, signal) => { | |
| if (signal) process.kill(process.pid, signal); | |
| process.exit(code ?? 0); | |
| }); | |