database.ts 导出db常量替代getDb()函数 env.ts JWT_SECRET/ES_URL改optional加DEV_MODE/PUSH_GATEWAY_URL elasticsearch.ts ES降级: ES_URL未设置时esClient=null notifications.service.ts 加createBatch+分页查询+Push Gateway推送调用 新建msg-init.sql创建2张表
37 lines
1.1 KiB
TypeScript
37 lines
1.1 KiB
TypeScript
import { z } from "zod";
|
|
|
|
const envSchema = z.object({
|
|
PORT: z.string().default("3007"),
|
|
DATABASE_URL: z.string().url(),
|
|
REDIS_URL: z.string().url().optional(),
|
|
JWT_SECRET: z.string().optional(),
|
|
JWT_ISSUER: z.string().default("next-edu-cloud"),
|
|
KAFKA_BROKERS: z.string().default("localhost:9092"),
|
|
ES_URL: z.string().url().optional(),
|
|
PUSH_GATEWAY_URL: z.string().url().optional(),
|
|
OTEL_EXPORTER_OTLP_ENDPOINT: z.string().url().optional(),
|
|
LOG_LEVEL: z
|
|
.enum(["fatal", "error", "warn", "info", "debug", "trace"])
|
|
.default("info"),
|
|
NODE_ENV: z
|
|
.enum(["development", "production", "test"])
|
|
.default("development"),
|
|
DEV_MODE: z.string().optional().default("false"),
|
|
});
|
|
|
|
export type Env = z.infer<typeof envSchema>;
|
|
|
|
export function loadEnv(): Env {
|
|
const result = envSchema.safeParse(process.env);
|
|
if (!result.success) {
|
|
console.error(
|
|
"❌ Invalid environment variables:",
|
|
result.error.flatten().fieldErrors,
|
|
);
|
|
throw new Error("Invalid environment configuration");
|
|
}
|
|
return result.data;
|
|
}
|
|
|
|
export const env = loadEnv();
|