// src/config.js - central runtime config (env vars, defaults, startup validation). // // Notes: // - WEBAPI_BASE/MANAGEMENT_BASE are default OSCAR endpoints; login can also accept // user-supplied host/ports (validated/SSRF-checked in auth) for per-session targets. // - Bad config should fail fast at startup with a clear error. // - Never log secrets (SESSION_SECRET, passwords, tokens). function port(name, fallback) { const raw = process.env[name] ?? String(fallback); const n = Number(raw); if (!Number.isInteger(n) || n < 1 || n > 65535) { throw new Error(`${name} must be a port from 1 to 65535, got: ${raw}`); } return n; } function int(name, fallback, { min = 0, max = Number.MAX_SAFE_INTEGER } = {}) { const raw = process.env[name] ?? String(fallback); const n = Number(raw); if (!Number.isInteger(n) || n < min || n > max) { throw new Error(`${name} must be an integer ${min}-${max}, got: ${raw}`); } return n; } const host = (name, fallback) => process.env[name] || fallback; const required = (name) => { const value = process.env[name]; if (!value) throw new Error(`${name} is required (set it in .env)`); return value; }; export const WEBAPI_BASE = `http://${host('WEBAPI_HOST', '127.0.0.1')}:${port('WEBAPI_PORT', 8081)}`; export const MANAGEMENT_BASE = `http://${host('MANAGEMENT_HOST', '127.0.0.1')}:${port('MANAGEMENT_PORT', 8080)}`; export const PORT = port('PORT', 3000); export const SESSION_SECRET = () => required('SESSION_SECRET'); // Bind address for this app. Default localhost; set 0.0.0.0 only behind nginx. export const LISTEN_HOST = host('LISTEN_HOST', '127.0.0.1'); export const TRUST_PROXY = int('TRUST_PROXY', 1, { min: 0, max: 10 }); export const COOKIE_SECURE = (() => { const raw = (process.env.COOKIE_SECURE ?? 'auto').toLowerCase(); if (['auto', 'true', 'false'].includes(raw)) return raw; throw new Error(`COOKIE_SECURE must be auto|true|false, got: ${raw}`); })(); export const SESSION_MAX_AGE_MS = int('SESSION_MAX_AGE_HOURS', 24, { min: 1, max: 24 * 30 }) * 60 * 60 * 1000; export const DB_PATH = process.env.DB_PATH || './data/oscar-web.sqlite'; export const NODE_ENV = process.env.NODE_ENV || 'development'; export function logStartupConfig() { // Never log secrets. console.log(`[config] Web API: ${WEBAPI_BASE}`); console.log(`[config] Management API: ${MANAGEMENT_BASE}`); console.log(`[config] App listen: http://${LISTEN_HOST}:${PORT}`); console.log(`[config] trust proxy: ${TRUST_PROXY}`); console.log(`[config] cookie secure: ${COOKIE_SECURE}`); console.log(`[config] db path: ${DB_PATH}`); }