86 lines
2.8 KiB
JavaScript
86 lines
2.8 KiB
JavaScript
#!/usr/bin/env node
|
|
// dev-backend: 读取 .env,补齐开发默认值,确保 postgres/docker-gateway 容器在跑,然后用 air 热加载运行 control-plane。
|
|
import { spawn, spawnSync } from "node:child_process";
|
|
import { readFileSync, existsSync } from "node:fs";
|
|
import { fileURLToPath } from "node:url";
|
|
import path from "node:path";
|
|
|
|
const root = path.dirname(path.dirname(fileURLToPath(import.meta.url)));
|
|
const envPath = path.join(root, ".env");
|
|
|
|
if (!existsSync(envPath)) {
|
|
console.error("缺少 .env:请参照 .env.example 创建(开发值任意填)。");
|
|
process.exit(1);
|
|
}
|
|
|
|
// 简易 KEY=value 解析(与 compose 的 env_file 语法一致,无需处理引号)。
|
|
const overrides = {};
|
|
for (const line of readFileSync(envPath, "utf8").split("\n")) {
|
|
const m = /^([A-Za-z_][A-Za-z0-9_]*)=(.*)$/.exec(line.trim());
|
|
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.CREATORHUB_CREDENTIAL_MASTER_KEY) {
|
|
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.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);
|
|
|
|
const env = { ...process.env, ...overrides };
|
|
|
|
const compose = spawnSync(
|
|
"docker",
|
|
[
|
|
"compose",
|
|
"-f",
|
|
"compose.yaml",
|
|
"-f",
|
|
"compose.dev.yaml",
|
|
"up",
|
|
"-d",
|
|
"--build",
|
|
"postgres",
|
|
"docker-gateway",
|
|
],
|
|
{ cwd: root, env, stdio: "inherit" },
|
|
);
|
|
if (compose.status !== 0) {
|
|
console.error("启动 postgres/docker-gateway 容器失败,请确认 docker 可用。");
|
|
process.exit(compose.status ?? 1);
|
|
}
|
|
|
|
const air = spawn("air", ["-c", ".air.toml", ...process.argv.slice(2)], {
|
|
cwd: root,
|
|
env,
|
|
stdio: "inherit",
|
|
});
|
|
air.on("exit", (code) => process.exit(code ?? 0));
|