/** * The server's settings, from the environment (`.env`, built from `env.template.json`). Read once; * tests pass their own with `loadConfig({...})`. */ export type Config = { /** The public address, e.g. https://accounts.hopefamily.co.za (no trailing slash). */ publicUrl: string; port: number; /** Encrypts secrets at rest and signs cookies. Generated on the server, never sent anywhere. */ secret: string; production: boolean; /** Behind nginx/frp: trust X-Forwarded-* (oidc-provider's `proxy`). */ trustProxy: boolean; /** Express's "trust proxy" (TRUST_PROXY, read as Events reads it), or false when off. */ trustProxySetting: TrustProxy; hubUrl: string; hubLicenceKey: string | null; hubPublicKey: string | null; hubAdminName: string | null; hubAdminEmail: string | null; timezone: string; smtp: { host: string; port: number; user: string; pass: string; from: string; secure: boolean } | null; /** 1.2.0: WAWP from the .env when the database has none (as Cross Events). */ wawp: { accessToken: string; instanceId: string } | null; /** Local testing only: linked products on http or private addresses. */ allowPrivateLinks: boolean; jobsEnabled: boolean; /** 1.3.0: for the backups' pg_dump (DATABASE_URL, as Prisma uses). */ databaseUrl: string | null; /** 1.3.0: where backups go; default backend/backups (tests use their own folder). */ backupDir: string | null; }; const bool = (v: string | undefined, def: boolean) => (v === undefined || v === '' ? def : String(v).toLowerCase() === 'true'); export type TrustProxy = false | number | string | string[]; /** * TRUST_PROXY as Express's "trust proxy": `loopback` (behind frp, the default the Hub suggests), * a hop count, IP addresses/CIDRs separated by commas, `true` (one hop: never "trust everyone") or * `false`. Unset: one hop in production, off otherwise. Before 1.0.1 anything but `true` meant * off, so `loopback` turned it off. */ export function trustProxyFrom(v: string | undefined, production: boolean): TrustProxy { const raw = (v ?? '').trim(); if (raw === '') return production ? 1 : false; const lower = raw.toLowerCase(); if (lower === 'true') return 1; if (lower === 'false') return false; if (/^\d+$/.test(raw)) return Number(raw) || false; return raw.includes(',') ? raw.split(',').map((s) => s.trim()).filter(Boolean) : raw; } export function loadConfig(env: Record = process.env): Config { const publicUrl = String(env.ACCOUNTS_URL || env.PUBLIC_URL || 'http://localhost:3200').replace(/\/+$/, ''); const production = env.NODE_ENV === 'production'; const secret = env.ACCOUNTS_SECRET || ''; const trustProxySetting = trustProxyFrom(env.TRUST_PROXY, production); if (production && secret.length < 32) throw new Error('ACCOUNTS_SECRET must be set (at least 32 characters) in production.'); return { publicUrl, port: Number(env.PORT || 5200), secret: secret || 'development-only-secret-change-me-0123456789', production, trustProxy: trustProxySetting !== false, trustProxySetting, hubUrl: String(env.HUB_URL || 'https://hub.crosscode.co.za').replace(/\/+$/, ''), hubLicenceKey: env.HUB_LICENCE_KEY?.trim() || null, hubPublicKey: env.HUB_PUBLIC_KEY?.trim() || null, hubAdminName: env.HUB_ADMIN_NAME?.trim() || null, hubAdminEmail: env.HUB_ADMIN_EMAIL?.trim() || null, timezone: env.APP_TIMEZONE || 'Africa/Johannesburg', smtp: env.SMTP_HOST ? { host: env.SMTP_HOST, port: Number(env.SMTP_PORT || 587), user: env.SMTP_USER || '', pass: env.SMTP_PASS || '', from: env.SMTP_FROM || env.SMTP_USER || '', secure: bool(env.SMTP_SECURE, Number(env.SMTP_PORT) === 465) } : null, wawp: env.WAWP_ACCESS_TOKEN && env.WAWP_INSTANCE_ID ? { accessToken: env.WAWP_ACCESS_TOKEN, instanceId: env.WAWP_INSTANCE_ID } : null, allowPrivateLinks: bool(env.LINKS_ALLOW_PRIVATE, false), jobsEnabled: bool(env.JOBS_ENABLED, true), databaseUrl: env.DATABASE_URL?.trim() || null, backupDir: null, }; }