1
This commit is contained in:
@@ -37,22 +37,93 @@ loadEnvFile(resolve(repoRoot, '.env'), fileEnv);
|
||||
loadEnvFile(resolve(repoRoot, '.env.local'), fileEnv);
|
||||
loadEnvFile(resolve(repoRoot, '.env.secrets.local'), fileEnv);
|
||||
|
||||
function buildTargetCandidates() {
|
||||
const candidates = [
|
||||
fileEnv.GENARRATIVE_RUNTIME_SERVER_TARGET,
|
||||
fileEnv.RUST_SERVER_TARGET,
|
||||
fileEnv.GENARRATIVE_API_TARGET,
|
||||
`http://127.0.0.1:${fileEnv.GENARRATIVE_API_PORT || '3100'}`,
|
||||
'http://127.0.0.1:8082',
|
||||
'http://127.0.0.1:3100',
|
||||
].filter(Boolean);
|
||||
|
||||
return Array.from(new Set(candidates));
|
||||
}
|
||||
|
||||
async function isTargetReachable(target) {
|
||||
const healthUrl = new URL('/healthz', target);
|
||||
const controller = new AbortController();
|
||||
const timeoutId = setTimeout(() => controller.abort(), 1200);
|
||||
|
||||
try {
|
||||
const response = await fetch(healthUrl, {
|
||||
method: 'GET',
|
||||
signal: controller.signal,
|
||||
});
|
||||
|
||||
return response.ok;
|
||||
} catch {
|
||||
return false;
|
||||
} finally {
|
||||
clearTimeout(timeoutId);
|
||||
}
|
||||
}
|
||||
|
||||
async function resolveRuntimeTarget() {
|
||||
const candidates = buildTargetCandidates();
|
||||
const reachableTargets = [];
|
||||
|
||||
for (const target of candidates) {
|
||||
if (await isTargetReachable(target)) {
|
||||
reachableTargets.push(target);
|
||||
if (
|
||||
target === fileEnv.GENARRATIVE_RUNTIME_SERVER_TARGET ||
|
||||
target === fileEnv.RUST_SERVER_TARGET ||
|
||||
target === fileEnv.GENARRATIVE_API_TARGET
|
||||
) {
|
||||
return {
|
||||
target,
|
||||
fallbackUsed: false,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (reachableTargets.length > 0) {
|
||||
return {
|
||||
target: reachableTargets[0],
|
||||
fallbackUsed: true,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
target:
|
||||
fileEnv.GENARRATIVE_RUNTIME_SERVER_TARGET ||
|
||||
fileEnv.RUST_SERVER_TARGET ||
|
||||
fileEnv.GENARRATIVE_API_TARGET ||
|
||||
`http://127.0.0.1:${fileEnv.GENARRATIVE_API_PORT || '3100'}`,
|
||||
fallbackUsed: false,
|
||||
};
|
||||
}
|
||||
|
||||
const runtimeTarget = await resolveRuntimeTarget();
|
||||
if (runtimeTarget.fallbackUsed) {
|
||||
console.warn(
|
||||
`[dev:web] 配置的 Rust target 不可用,已切换到 ${runtimeTarget.target}`,
|
||||
);
|
||||
}
|
||||
|
||||
const mergedEnv = {
|
||||
...fileEnv,
|
||||
RUST_SERVER_TARGET:
|
||||
fileEnv.RUST_SERVER_TARGET ||
|
||||
fileEnv.GENARRATIVE_API_TARGET ||
|
||||
`http://127.0.0.1:${fileEnv.GENARRATIVE_API_PORT || '3100'}`,
|
||||
RUST_SERVER_TARGET: runtimeTarget.target,
|
||||
GENARRATIVE_RUNTIME_SERVER_TARGET: runtimeTarget.target,
|
||||
};
|
||||
|
||||
mergedEnv.GENARRATIVE_RUNTIME_SERVER_TARGET =
|
||||
fileEnv.GENARRATIVE_RUNTIME_SERVER_TARGET || mergedEnv.RUST_SERVER_TARGET;
|
||||
|
||||
console.log(`[dev:web] backend=rust target=${mergedEnv.GENARRATIVE_RUNTIME_SERVER_TARGET}`);
|
||||
|
||||
const child = spawn(
|
||||
'node',
|
||||
['scripts/vite-cli.mjs', '--port=3000', '--host=0.0.0.0'],
|
||||
['scripts/vite-cli.mjs', '--port=3000', '--host=0.0.0.0', '--strictPort'],
|
||||
{
|
||||
cwd: process.cwd(),
|
||||
env: mergedEnv,
|
||||
|
||||
Reference in New Issue
Block a user