Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | INFRASTRUCTURE & AUTOMATION4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : MacLustr Vision8 * File : server/src/config.ts9 * Module : server10 * Repository : https://git.spboucher.ai/maclustr-vision11 * Project Website : https://camera.spboucher.ai12 * Created : 2026-11-3013 * Last Modified : 2026-11-3014 *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 */4142// Chargement de la configuration (sans secrets) et des chemins dérivés.43import fs from 'node:fs';44import os from 'node:os';45import path from 'node:path';46import { fileURLToPath } from 'node:url';4748export type Config = {49 domain: string;50 port: number;51 listen: string[];52 trustedProxies: string[];53 extraOrigins: string[];54 timezone: string;55 dataDir: string;56 archiveDir: string;57 configDir: string;58 binDir: string;59 ffmpegBin: string;60 ffprobeBin: string;61 go2rtc: { apiPort: number; rtspPort: number; controlPort: number };62 recording: { segmentSeconds: number; stallSeconds: number; noSegmentAlertSeconds: number };63 retention: {64 days: number;65 maxArchiveGB: number;66 minFreeGB: number;67 criticalFreeGB: number;68 spoolMaxGB: number;69 exportTtlHours: number;70 exportMaxMinutes: number;71 playbackCacheGB: number;72 };73 session: { trustedDays: number; defaultHours: number };74 // chemins dérivés75 rootDir: string;76 spoolDir: string;77 dbPath: string;78 exportsDir: string;79 cacheDir: string;80 quarantineDir: string;81 eventsDir: string;82 webDir: string;83 migrationsDir: string;84 version: string;85};8687export function expandHome(p: string): string {88 if (p === '~') return os.homedir();89 if (p.startsWith('~/')) return path.join(os.homedir(), p.slice(2));90 return p;91}9293export const ROOT_DIR = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..', '..');9495export function loadConfig(file?: string): Config {96 const cfgPath = file || process.env.MLV_CONFIG || path.join(ROOT_DIR, 'config', 'vision.json');97 const raw = JSON.parse(fs.readFileSync(cfgPath, 'utf8'));98 const dataDir = path.resolve(expandHome(raw.dataDir));99 const pkg = JSON.parse(fs.readFileSync(path.join(ROOT_DIR, 'package.json'), 'utf8'));100 const cfg: Config = {101 ...raw,102 dataDir,103 archiveDir: path.resolve(raw.archiveDir ? expandHome(raw.archiveDir) : path.join(dataDir, 'recordings')),104 configDir: path.resolve(expandHome(raw.configDir)),105 binDir: path.resolve(expandHome(raw.binDir)),106 extraOrigins: raw.extraOrigins || [],107 rootDir: ROOT_DIR,108 spoolDir: path.join(dataDir, 'spool'),109 dbPath: path.join(dataDir, 'database', 'vision.db'),110 exportsDir: path.join(dataDir, 'exports'),111 cacheDir: path.join(dataDir, 'cache'),112 quarantineDir: path.join(dataDir, 'quarantine'),113 eventsDir: path.join(dataDir, 'events'),114 webDir: path.join(ROOT_DIR, 'dist', 'web'),115 migrationsDir: path.join(ROOT_DIR, 'migrations'),116 version: pkg.version,117 };118 return cfg;119}120121export function ensureDirs(cfg: Config): void {122 for (const d of [cfg.dataDir, cfg.spoolDir, path.dirname(cfg.dbPath), cfg.exportsDir, cfg.cacheDir, cfg.quarantineDir, cfg.eventsDir]) {123 fs.mkdirSync(d, { recursive: true, mode: 0o700 });124 }125}126127/** Adresses d'écoute : « wg » est remplacé par l'adresse 10.67.0.x du tunnel si elle existe. */128export function resolveListen(cfg: Config): string[] {129 const out: string[] = [];130 for (const a of cfg.listen) {131 if (a === 'wg') {132 for (const list of Object.values(os.networkInterfaces())) {133 for (const i of list || []) if (i.family === 'IPv4' && i.address.startsWith('10.67.0.')) out.push(i.address);134 }135 } else out.push(a);136 }137 return [...new Set(out)];138}139140export function gitCommit(): string | null {141 try {142 const head = fs.readFileSync(path.join(ROOT_DIR, '.git', 'HEAD'), 'utf8').trim();143 if (!head.startsWith('ref:')) return head.slice(0, 7);144 const ref = head.slice(5).trim();145 return fs.readFileSync(path.join(ROOT_DIR, '.git', ref), 'utf8').trim().slice(0, 7);146 } catch {147 return null;148 }149}150