Files
Kaname/src/lib/env.ts
T
Pascal LinxweilerandClaude Opus 5.5 891e496157 Milestone (a): project skeleton and OIDC login
- Next.js 16 + Tailwind + shadcn/ui skeleton
- Auth.js v5 generic OIDC (Pocket ID, PKCE) with optional group gate
- proxy.ts gates all pages and API routes
- Drizzle schema (models, files, jobs, logs, worker state) on SQLite/libsql
- Worker skeleton: migrations, interrupted-job recovery, heartbeat

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-05 15:32:52 +02:00

57 lines
1.9 KiB
TypeScript

import { z } from "zod";
// Shared by the web app and the worker. Never import this from a client component:
// it contains HF_TOKEN. Web code should go through "@/lib/server-env", which is guarded
// by "server-only".
const emptyToUndefined = (v: unknown) => (v === "" ? undefined : v);
const coreSchema = z.object({
HF_TOKEN: z.preprocess(emptyToUndefined, z.string().optional()),
MODELS_DIR: z.preprocess(emptyToUndefined, z.string().default("/models")),
DATABASE_URL: z.preprocess(emptyToUndefined, z.string().default("file:/data/app.db")),
MAX_CONCURRENT_DOWNLOADS: z.preprocess(emptyToUndefined, z.coerce.number().int().min(1).default(1)),
UPDATE_CHECK_INTERVAL_HOURS: z.preprocess(emptyToUndefined, z.coerce.number().min(0).default(12)),
});
const authSchema = z.object({
OIDC_ISSUER: z.url(),
OIDC_CLIENT_ID: z.string().min(1),
OIDC_CLIENT_SECRET: z.string().min(1),
OIDC_ALLOWED_GROUPS: z.preprocess(
(v) =>
typeof v === "string"
? v
.split(",")
.map((g) => g.trim())
.filter(Boolean)
: [],
z.array(z.string()),
),
});
export type CoreEnv = z.infer<typeof coreSchema>;
export type AuthEnv = z.infer<typeof authSchema>;
let core: CoreEnv | undefined;
let auth: AuthEnv | undefined;
function parse<T extends z.ZodType>(schema: T, name: string): z.infer<T> {
const result = schema.safeParse(process.env);
if (!result.success) {
const issues = result.error.issues.map((i) => ` ${i.path.join(".")}: ${i.message}`).join("\n");
throw new Error(`Invalid ${name} environment:\n${issues}`);
}
return result.data;
}
/** Config needed by both web and worker. Parsed lazily so `next build` works without env. */
export function coreEnv(): CoreEnv {
return (core ??= parse(coreSchema, "core"));
}
/** OIDC config, only needed by the web app. */
export function authEnv(): AuthEnv {
return (auth ??= parse(authSchema, "auth"));
}