export interface Config { databaseUrl: string; dataDir: string; secret: string; publicUrl: string | undefined; port: number; pluginDirs: string[]; } export const MISSING_SECRET = "Set STTRACKER_SECRET to a long random string. It encrypts source credentials."; /** Read the server config from the environment. Throws when `STTRACKER_SECRET` is missing. */ export function loadConfig(env: Record = process.env): Config { const secret = env.STTRACKER_SECRET; if (!secret) throw new Error(MISSING_SECRET); const dataDir = env.DATA_DIR || "./data"; const port = Number(env.PORT || 3000); if (!Number.isInteger(port) || port < 0 || port > 65535) throw new Error(`PORT must be a port number, got "${env.PORT}"`); return { databaseUrl: env.DATABASE_URL || `sqlite://${dataDir}/sttracker.db`, dataDir, secret, publicUrl: env.PUBLIC_URL || undefined, port, pluginDirs: (env.PLUGIN_DIRS || `plugins,${dataDir}/plugins`) .split(",") .map((d) => d.trim()) .filter((d) => d !== ""), }; }