File size: 1,131 Bytes
a5a755a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
48
49
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);
});