feat(配置): 启动时用 zod 一次解析环境变量,有错拒绝启动

原来是 `Number(env ?? 默认值)` 散写:JUDGE_CONCURRENCY=two 得到 NaN 照样起;
DATABASE_URL 漏配就静默连上开发库的默认账号;AI_KEY 缺了要等第一次 AI 调用才报错。

- config.ts 一张 envSchema 覆盖全部变量,失败时列出所有问题后 exit 1
- 编译形态(线上)下 DATABASE_URL / REDIS_URL / JUDGE_SERVER_TOKEN 必须显式给,
  dev 默认值指向本机 5433 / 6380,放进容器里只会连不上或连错
- 空字符串按未设置处理(compose 的 ${X:-} 展开就是空串)
- AI_KEY 为空启动时告警(AI 是可选功能,不退出)
- db/index.ts 改读 config.databaseUrl;flowchart/run.ts 的 provider / model 改读 config

实跑核过:非法值被列出并拒绝启动;编译产物在仓库外无 env 时报三项必填,
给齐后 /health 正常;dev 的 serve / worker 照常起。

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
xuyueandClaude Opus 5.5 committed 2026-09-27 14:53:51 +08:00
1 parent ca0dbbf0c5
commit b38ad8ccee
3 files changed
+113 -40

No files matched your search

+2 -5
View File
@@ -1,15 +1,12 @@
import { drizzle } from "drizzle-orm/postgres-js"
import postgres from "postgres"
import { config } from "../config"
import * as schema from "./schema"
const url =
process.env.DATABASE_URL ??
"postgres://onlinejudge:onlinejudge@localhost:5433/onlinejudge"
// 不设会话时区:日历语义的 SQL 一律显式 `at time zone`(`../time` 的 localTime),
// 不靠会话默认值兜底 —— 兜底会把漏写的地方在线上掩盖掉,dev 上又是另一个答案。
const client = postgres(url)
const client = postgres(config.databaseUrl)
export const db = drizzle(client, { schema })