fix: don't crash on startup when SECRET_MASTER_KEY is not set in production
All checks were successful
Deploy API Server / build-and-deploy (push) Successful in 40s

Changed getMasterKey() from throwing Error at module load time to logging
a critical console.error, so the app can still start without the env var.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
WangDL 2026-05-24 10:46:20 +08:00
parent a5a592988a
commit 90c27ee979

View File

@ -8,13 +8,12 @@ const FALLBACK_KEY = 'zhixi-secret-master-key-2026-32b!!';
function getMasterKey(): Buffer {
const envKey = process.env.SECRET_MASTER_KEY;
if (!envKey || envKey === FALLBACK_KEY) {
const msg = 'SECRET_MASTER_KEY 使用的是默认值,生产环境请务必设置环境变量 SECRET_MASTER_KEY';
if (process.env.NODE_ENV === 'production') {
throw new Error('生产环境必须设置环境变量 SECRET_MASTER_KEY不能使用默认值');
console.error(`\n🔴 严重警告: ${msg}\n`);
} else {
console.warn(`\n⚠ 警告: ${msg}\n`);
}
console.warn(
'\n⚠ 警告: SECRET_MASTER_KEY 使用的是默认值\n' +
' 部署到生产环境前请务必设置环境变量 SECRET_MASTER_KEY\n',
);
}
const key = (envKey || FALLBACK_KEY).padEnd(32, '0').slice(0, 32);
return Buffer.from(key);