NestJS 11 + Fastify 统一错误体与 trace_id,/health 真实探活 DB/Redis,OpenAPI 3.1 契约含 20 个错误码;shared 包提供错误码、权限点、ULID、分页与通用 schema;worker 完成队列进程骨架。
41 lines
1.3 KiB
TypeScript
41 lines
1.3 KiB
TypeScript
import { z } from 'zod';
|
|
|
|
const envSchema = z.object({
|
|
NODE_ENV: z.enum(['development', 'test', 'production']).default('development'),
|
|
LOG_LEVEL: z.enum(['fatal', 'error', 'warn', 'info', 'debug', 'trace']).default('info'),
|
|
REDIS_URL: z
|
|
.string()
|
|
.min(1)
|
|
.refine(
|
|
(value) => value.startsWith('redis://') || value.startsWith('rediss://'),
|
|
'REDIS_URL 必须是 redis:// 连接串',
|
|
),
|
|
QUEUE: z.string().min(1).default('default'),
|
|
WORKER_CONCURRENCY: z.coerce.number().int().min(1).max(64).default(2),
|
|
});
|
|
|
|
export interface WorkerConfig {
|
|
nodeEnv: 'development' | 'test' | 'production';
|
|
logLevel: 'fatal' | 'error' | 'warn' | 'info' | 'debug' | 'trace';
|
|
redisUrl: string;
|
|
queue: string;
|
|
concurrency: number;
|
|
}
|
|
|
|
export function loadWorkerConfig(source: NodeJS.ProcessEnv = process.env): WorkerConfig {
|
|
const result = envSchema.safeParse(source);
|
|
if (!result.success) {
|
|
const details = result.error.issues
|
|
.map((issue) => `${issue.path.join('.') || '(root)'}: ${issue.message}`)
|
|
.join('; ');
|
|
throw new Error(`Worker 环境变量校验失败:${details}`);
|
|
}
|
|
return {
|
|
nodeEnv: result.data.NODE_ENV,
|
|
logLevel: result.data.LOG_LEVEL,
|
|
redisUrl: result.data.REDIS_URL,
|
|
queue: result.data.QUEUE,
|
|
concurrency: result.data.WORKER_CONCURRENCY,
|
|
};
|
|
}
|