feat: validate production runtime config
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
export type ConfigStatus = { ok: boolean; errors: string[] };
|
||||
export function checkRuntimeConfig(env: NodeJS.ProcessEnv = process.env): ConfigStatus {
|
||||
if (env.HUB_BUILD === "1" || env.NODE_ENV !== "production") return { ok: true, errors: [] };
|
||||
const errors: string[] = []; const session = env.SESSION_SECRET || ""; const encryption = env.TOKEN_ENCRYPTION_KEY || ""; const publicUrl = env.NEXT_PUBLIC_APP_URL || "";
|
||||
if (session.length < 32 || session === "development-only-change-me" || session.includes("replace-with")) errors.push("SESSION_SECRET must be a non-default value of at least 32 characters");
|
||||
if (!/^[0-9a-f]{64}$/i.test(encryption)) errors.push("TOKEN_ENCRYPTION_KEY must be 64 hexadecimal characters");
|
||||
try { if (new URL(publicUrl).protocol !== "https:") throw new Error(); } catch { errors.push("NEXT_PUBLIC_APP_URL must be an HTTPS URL in production"); }
|
||||
return { ok: errors.length === 0, errors };
|
||||
}
|
||||
export function requireRuntimeConfig() { const status = checkRuntimeConfig(); if (!status.ok) throw new Error(`Invalid production configuration: ${status.errors.join("; ")}`); }
|
||||
@@ -1,7 +1,9 @@
|
||||
import Database from "better-sqlite3";
|
||||
import { mkdirSync } from "node:fs";
|
||||
import { dirname } from "node:path";
|
||||
import { requireRuntimeConfig } from "@/lib/config";
|
||||
|
||||
requireRuntimeConfig();
|
||||
const path = process.env.HUB_BUILD === "1" ? ":memory:" : (process.env.DATABASE_PATH || "./data/hub.db");
|
||||
if (path !== ":memory:") mkdirSync(dirname(path), { recursive: true });
|
||||
export const db = new Database(path);
|
||||
|
||||
Reference in New Issue
Block a user