fix: reuse local gateway port during development

This commit is contained in:
2026-09-20 14:54:52 +08:00
parent 26b5804092
commit 2cb22ac5e5
5 changed files with 18 additions and 5 deletions
+14 -1
View File
@@ -23,9 +23,22 @@ const gatewayEnvPath = process.env.CREATORHUB_BROWSER_GATEWAY_ENV
?? path.join(os.homedir(), ".config", "creatorhub", "browser-gateway.env");
const gatewayEnv = readEnvFile(gatewayEnvPath);
const env = { ...process.env, ...rootEnv, ...gatewayEnv };
if (!env.LISTEN_ADDR) env.LISTEN_ADDR = "0.0.0.0:8081";
if (!env.LISTEN_ADDR) env.LISTEN_ADDR = "127.0.0.1:28187";
if (!env.GATEWAY_TOKEN) env.GATEWAY_TOKEN = "dev-creatorhub-gateway-token";
const port = env.LISTEN_ADDR.match(/:(\d+)$/)?.[1] ?? "28187";
try {
const response = await fetch(`http://127.0.0.1:${port}/healthz`, {
signal: AbortSignal.timeout(1000),
});
if (response.status === 204 || response.ok) {
console.log(`[dev-gateway] 已有 gateway 运行于 127.0.0.1:${port},直接复用。`);
process.exit(0);
}
} catch {
// The port is available or belongs to a different service; let the gateway report that clearly.
}
const gateway = spawn("python3", ["-m", "browser_gateway.server.http"], {
cwd: root,
env,