Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | INFRASTRUCTURE & AUTOMATION4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : MacLustr Social Engine & Remote Navigator8 * File : services/control-api/src/config.ts9 * Module : services10 * Repository : https://git.spboucher.ai/maclustr-social11 * Project Website : https://social.spboucher.ai12 * Created : 2026-12-0113 * Last Modified : 2026-12-0114 *15 * Author : Simon-Pierre Boucher16 * Position : Professor of Finance17 * Institution : Université du Québec en Outaouais (UQO)18 * Organization : SP Boucher19 * Website : https://spboucher.ai20 * Git : https://git.spboucher.ai21 *22 * Preferred Contact: admin@spboucher.ai23 * Technical Contact: dev@spboucher.ai24 * Security/Admin : admin@spboucher.ai25 *26 * Copyright (c) 202627 * Simon-Pierre Boucher28 * All rights reserved where applicable. This file is governed by the29 * applicable project license, copyright notices and relevant law.30 * Existing third-party rights and notices remain unaffected.31 *32 * License : None specified (no license file in this repository)33 *34 * Do not embed credentials, private keys, access tokens,35 * certificates or confidential personal information.36 *37 * =============================================================================38 * END OF SP BOUCHER SOURCE HEADER39 * =============================================================================40 */4142import { readFileSync, existsSync, mkdirSync } from 'node:fs';43import { randomBytes } from 'node:crypto';44import { homedir } from 'node:os';45import { join } from 'node:path';4647const env = (k: string, d?: string) => process.env[k] ?? d ?? '';48function secretFile(path: string, create: boolean): Buffer {49 if (existsSync(path)) return Buffer.from(readFileSync(path, 'utf8').trim(), 'hex');50 if (!create) throw new Error(`secret manquant: ${path}`);51 const b = randomBytes(32);52 mkdirSync(join(path, '..'), { recursive: true, mode: 0o700 });53 // wx: never overwrite an existing key54 require('node:fs').writeFileSync(path, b.toString('hex') + '\n', { mode: 0o600, flag: 'wx' });55 return b;56}57export type Config = ReturnType<typeof loadConfig>;58export function loadConfig(over: Partial<Record<string, string>> = {}) {59 const e = (k: string, d?: string) => over[k] ?? env(k, d);60 const prod = e('MLS_ENV', 'development') === 'production';61 const dataDir = e('MLS_DATA_DIR', join(homedir(), 'maclustr-social-data'));62 const cfgDir = e('MLS_CONFIG_DIR', join(homedir(), '.config', 'maclustr-social'));63 const socialOrigin = e('MLS_SOCIAL_ORIGIN', 'http://localhost:8880');64 const navigatorOrigin = e('MLS_NAVIGATOR_ORIGIN', 'http://localhost:8881');65 return {66 env: e('MLS_ENV', 'development'), prod,67 host: e('HOST', '127.0.0.1'), port: Number(e('PORT', '8880')),68 databaseUrl: e('DATABASE_URL', 'postgres://127.0.0.1/mls_dev'),69 dataDir, cfgDir, socialOrigin, navigatorOrigin,70 origins: [socialOrigin, navigatorOrigin],71 /** Socket peers allowed to reach the public listeners (reverse proxy over WireGuard + loopback). Empty = any (tests). */72 allowedPeers: e('MLS_ALLOWED_PEERS', '').split(',').map((s) => s.trim()).filter(Boolean),73 trustProxy: e('MLS_TRUST_PROXY', '127.0.0.1').split(',').map((s) => s.trim()).filter(Boolean),74 gatewayInternalUrl: e('MLS_GATEWAY_INTERNAL_URL', 'http://127.0.0.1:8882'),75 controlInternalUrl: e('MLS_CONTROL_INTERNAL_URL', 'http://127.0.0.1:8880'),76 webDir: e('MLS_SOCIAL_WEB_DIR', ''),77 fixtureOrigin: e('MLS_FIXTURE_ORIGIN', ''), // e.g. http://127.0.0.1:8883 — synthetic test platform, never a third party78 forgeApi: e('MLS_FORGE_API', 'https://git.spboucher.ai/api/v1'),79 anthropicKeyFile: e('MLS_ANTHROPIC_KEY_FILE', join(cfgDir, 'anthropic.key')),80 mlxBaseUrl: e('MLS_MLX_BASE_URL', 'https://llm.spboucher.ai/v1'),81 mlxKeyFile: e('MLS_MLX_KEY_FILE', join(cfgDir, 'mlx.key')),82 mlxModel: e('MLS_MLX_MODEL', 'maclustr-qwen3'),83 claudeModel: e('MLS_CLAUDE_MODEL', 'claude-opus-5-5'),84 sessionTtlHours: Number(e('MLS_SESSION_TTL_HOURS', '12')),85 secret: (name: 'media' | 'service' | 'session') => secretFile(join(cfgDir, `${name}.key`), true),86 };87}88