38 lines
1.1 KiB
TypeScript
38 lines
1.1 KiB
TypeScript
import { z } from "zod";
|
|
|
|
const schema = z.object({
|
|
NODE_ENV: z
|
|
.enum(["development", "test", "production"])
|
|
.default("development"),
|
|
APP_URL: z.string().url().default("http://127.0.0.1:3000"),
|
|
DATABASE_PATH: z.string().default("./data/hy2-panel.db"),
|
|
SESSION_SECRET: z
|
|
.string()
|
|
.min(16)
|
|
.default("change-this-session-secret-before-production"),
|
|
SESSION_COOKIE_SECURE: z
|
|
.enum(["auto", "true", "false"])
|
|
.default("auto"),
|
|
SESSION_TTL_HOURS: z.coerce.number().int().min(1).max(24 * 365).default(720),
|
|
ADMIN_USERNAME: z.string().min(1).max(64).default("admin"),
|
|
ADMIN_PASSWORD: z.string().min(8).max(128).default("change-me-now"),
|
|
POLLER_ENABLED: z
|
|
.union([z.literal("true"), z.literal("false")])
|
|
.default("true")
|
|
.transform((value) => value === "true"),
|
|
POLLER_STARTUP_DELAY_MS: z.coerce.number().int().min(0).max(60_000).default(2500),
|
|
});
|
|
|
|
export type AppEnv = z.infer<typeof schema>;
|
|
|
|
let cachedEnv: AppEnv | null = null;
|
|
|
|
export function getEnv(): AppEnv {
|
|
if (cachedEnv) {
|
|
return cachedEnv;
|
|
}
|
|
|
|
cachedEnv = schema.parse(process.env);
|
|
return cachedEnv;
|
|
}
|