refactor: migrate browser gateway to native xvfb
This commit is contained in:
Regular → Executable
+20
-41
@@ -1,5 +1,5 @@
|
||||
#!/usr/bin/env node
|
||||
// dev-backend: 读取 .env,补齐开发默认值,确保 postgres/docker-gateway 容器在跑,然后用 air 热加载运行 control-plane。
|
||||
// dev-backend: 启动 PostgreSQL 依赖,并连接宿主机 native browser gateway 后运行 control-plane。
|
||||
import { spawn, spawnSync } from "node:child_process";
|
||||
import { readFileSync, existsSync } from "node:fs";
|
||||
import { fileURLToPath } from "node:url";
|
||||
@@ -20,63 +20,42 @@ for (const line of readFileSync(envPath, "utf8").split("\n")) {
|
||||
if (m && !(m[1] in overrides)) overrides[m[1]] = m[2];
|
||||
}
|
||||
|
||||
if (!overrides.CONTROL_PLANE_USERNAME)
|
||||
overrides.CONTROL_PLANE_USERNAME = "admin";
|
||||
if (!overrides.CONTROL_PLANE_PASSWORD)
|
||||
overrides.CONTROL_PLANE_PASSWORD = "admin123";
|
||||
// 主密钥必须是 32 字节的 base64;无效或缺失时换成本地开发专用密钥(与 .env 里的 dev 凭据同级机密性)。
|
||||
const devMasterKey = Buffer.from(
|
||||
"creatorhub dev local master key",
|
||||
"utf8",
|
||||
).toString("base64"); // 恰 32 字节
|
||||
if (
|
||||
Buffer.from(overrides.CREATORHUB_CREDENTIAL_MASTER_KEY ?? "", "base64")
|
||||
.length !== 32
|
||||
) {
|
||||
if (!overrides.CONTROL_PLANE_USERNAME) overrides.CONTROL_PLANE_USERNAME = "admin";
|
||||
if (!overrides.CONTROL_PLANE_PASSWORD) overrides.CONTROL_PLANE_PASSWORD = "admin123";
|
||||
const devMasterKey = Buffer.from("creatorhub dev local master key", "utf8").toString("base64");
|
||||
if (Buffer.from(overrides.CREATORHUB_CREDENTIAL_MASTER_KEY ?? "", "base64").length !== 32) {
|
||||
if (overrides.CREATORHUB_CREDENTIAL_MASTER_KEY) {
|
||||
console.error(
|
||||
"[dev-backend] .env 的 CREATORHUB_CREDENTIAL_MASTER_KEY 不是 32 字节 base64,本地开发改用固定开发密钥。",
|
||||
);
|
||||
console.error("[dev-backend] .env 的 CREATORHUB_CREDENTIAL_MASTER_KEY 不是 32 字节 base64,本地开发改用固定开发密钥。");
|
||||
}
|
||||
overrides.CREATORHUB_CREDENTIAL_MASTER_KEY = devMasterKey;
|
||||
}
|
||||
if (!overrides.LISTEN_ADDR) overrides.LISTEN_ADDR = ":8082";
|
||||
if (!overrides.DATABASE_URL)
|
||||
overrides.DATABASE_URL =
|
||||
"postgres://creatorhub@127.0.0.1:5432/creatorhub?sslmode=disable";
|
||||
if (!overrides.CREATORHUB_CREDENTIAL_STORE_DIR)
|
||||
overrides.CREATORHUB_CREDENTIAL_STORE_DIR = path.join(
|
||||
root,
|
||||
".dev-credentials",
|
||||
);
|
||||
if (!overrides.DATABASE_URL) overrides.DATABASE_URL = "postgres://creatorhub@127.0.0.1:5432/creatorhub?sslmode=disable";
|
||||
if (!overrides.CREATORHUB_CREDENTIAL_STORE_DIR) overrides.CREATORHUB_CREDENTIAL_STORE_DIR = path.join(root, ".dev-credentials");
|
||||
if (!overrides.WEB_DIR) overrides.WEB_DIR = path.join(root, "web", "dist");
|
||||
if (!overrides.LOG_LEVEL) overrides.LOG_LEVEL = "debug";
|
||||
if (!overrides.DOCKER_GID)
|
||||
overrides.DOCKER_GID = String(process.getgid?.() ?? 1000);
|
||||
if (!overrides.NATIVE_GATEWAY_ENDPOINT) overrides.NATIVE_GATEWAY_ENDPOINT = "http://127.0.0.1:8081";
|
||||
|
||||
const env = { ...process.env, ...overrides };
|
||||
|
||||
const compose = spawnSync(
|
||||
"docker",
|
||||
[
|
||||
"compose",
|
||||
"-f",
|
||||
"compose.yaml",
|
||||
"-f",
|
||||
"compose.dev.yaml",
|
||||
"up",
|
||||
"-d",
|
||||
"--build",
|
||||
"postgres",
|
||||
"docker-gateway",
|
||||
],
|
||||
["compose", "-f", "compose.yaml", "-f", "compose.dev.yaml", "up", "-d", "postgres"],
|
||||
{ cwd: root, env, stdio: "inherit" },
|
||||
);
|
||||
if (compose.status !== 0) {
|
||||
console.error("启动 postgres/docker-gateway 容器失败,请确认 docker 可用。");
|
||||
console.error("启动 PostgreSQL 容器失败,请确认 Docker 只用于数据库依赖。");
|
||||
process.exit(compose.status ?? 1);
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await fetch(`${overrides.NATIVE_GATEWAY_ENDPOINT}/healthz`, { signal: AbortSignal.timeout(2000) });
|
||||
if (!response.ok) throw new Error(`HTTP ${response.status}`);
|
||||
} catch (error) {
|
||||
console.error(`[dev-backend] native browser gateway 不可达:${overrides.NATIVE_GATEWAY_ENDPOINT} (${error})`);
|
||||
console.error("请先以非 root 用户启动 cmd/browser_gateway.gateway 或对应的 systemd user service。");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const air = spawn("air", ["-c", ".air.toml", ...process.argv.slice(2)], {
|
||||
cwd: root,
|
||||
env,
|
||||
|
||||
Reference in New Issue
Block a user