Files
NianAIGC/scripts/health-check.mjs
2026-05-29 12:32:02 +08:00

27 lines
1020 B
JavaScript

const port = process.env.PORT || process.env.APP_PORT || '3000';
const hostname = process.env.HOSTNAME || '127.0.0.1';
const baseUrl = process.env.NEXT_PUBLIC_APP_URL || process.env.ZHINIAN_PUBLIC_BASE_URL || `http://${hostname}:${port}`;
const healthUrl = new URL('/api/health', baseUrl).toString();
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), 3000);
try {
const response = await fetch(healthUrl, { signal: controller.signal });
const text = await response.text();
if (!response.ok) {
console.error(`[health] ${response.status} ${response.statusText}: ${text}`);
process.exit(1);
}
const payload = JSON.parse(text);
console.log(JSON.stringify(payload, null, 2));
if (payload?.appId !== 'zhinian-web-studio' || !payload?.ok) {
process.exit(1);
}
} catch (error) {
console.error(`[health] Failed to reach ${healthUrl}: ${error instanceof Error ? error.message : String(error)}`);
process.exit(1);
} finally {
clearTimeout(timeout);
}