import { existsSync, readFileSync } from 'node:fs'; import { dirname, resolve } from 'node:path'; import { fileURLToPath } from 'node:url'; const serverDir = dirname(fileURLToPath(import.meta.url)); // The server now lives at `/demos/dating/server/`, so the repo // root is three levels up — not two as it was when the server lived // at `/servers/dating/`. Loading `.env`/`.env.local` from both // the repo root and the server directory keeps either location valid. const rootDir = resolve(serverDir, '../../..'); loadEnvFile(resolve(rootDir, '.env')); loadEnvFile(resolve(rootDir, '.env.local')); loadEnvFile(resolve(serverDir, '.env')); loadEnvFile(resolve(serverDir, '.env.local')); export const config = { host: process.env.DATING_SERVER_HOST || '127.0.0.1', port: readPort(process.env.DATING_SERVER_PORT, 8787), allowedOrigins: readList( process.env.DATING_ALLOWED_ORIGINS, ['http://localhost:5173', 'http://127.0.0.1:5173'] ), cookieName: process.env.DATING_SESSION_COOKIE || 'dating_session', secureCookies: process.env.DATING_SECURE_COOKIES === 'true', pocketBaseUrl: stripTrailingSlash( process.env.POCKETBASE_URL || process.env.PUBLIC_POCKETBASE_URL || 'http://127.0.0.1:8090' ), pocketBaseSuperuserEmail: requireEnv('POCKETBASE_SUPERUSER_EMAIL'), pocketBaseSuperuserPassword: requireEnv('POCKETBASE_SUPERUSER_PASSWORD') }; function loadEnvFile(file) { if (!existsSync(file)) return; const text = readFileSync(file, 'utf8'); for (const rawLine of text.split(/\r?\n/)) { const line = rawLine.trim(); if (!line || line.startsWith('#')) continue; const eq = line.indexOf('='); if (eq === -1) continue; const key = line.slice(0, eq).trim(); const value = unquote(line.slice(eq + 1).trim()); if (key && process.env[key] == null) { process.env[key] = value; } } } function unquote(value) { if ( (value.startsWith('"') && value.endsWith('"')) || (value.startsWith("'") && value.endsWith("'")) ) { return value.slice(1, -1); } return value; } function readPort(value, fallback) { const port = Number(value); return Number.isInteger(port) && port > 0 && port < 65536 ? port : fallback; } function readList(value, fallback) { if (!value) return fallback; return value .split(',') .map((item) => item.trim()) .filter(Boolean); } function stripTrailingSlash(value) { return value.replace(/\/+$/, ''); } function requireEnv(name) { const value = process.env[name]; if (!value) { throw new Error(`Missing required env var ${name}`); } return value; }