import dotenv from 'dotenv'; import * as fs from 'fs'; import * as path from 'path'; // Load environment variables from .env file dotenv.config(); export interface AppConfig { port: number; apiKey: string; databasePath: string; relayUrls: string[]; autoRestart: boolean; maxRestarts: number; logLevel: string; } function parseConfigFile(): Partial { try { // Try to read the TOML config file const configPath = '/usr/local/bin/sdk_signer.conf'; if (fs.existsSync(configPath)) { const configContent = fs.readFileSync(configPath, 'utf8'); // Simple TOML parsing for our needs const relayUrlsMatch = configContent.match(/relay_urls\s*=\s*\[(.*?)\]/); const wsPortMatch = configContent.match(/ws_port\s*=\s*(\d+)/); const httpPortMatch = configContent.match(/http_port\s*=\s*(\d+)/); const config: Partial = {}; if (relayUrlsMatch) { // Parse relay URLs from the array format const urlsStr = relayUrlsMatch[1]; const urls = urlsStr.split(',').map(url => url.trim().replace(/"/g, '').replace(/http:\/\//, 'ws://') ); config.relayUrls = urls; } if (wsPortMatch) { config.port = parseInt(wsPortMatch[1]); } return config; } } catch (error) { console.warn('⚠️ Warning: Could not read config file, using defaults:', error); } return {}; } export function loadConfig(): AppConfig { const fileConfig = parseConfigFile(); return { port: fileConfig.port || parseInt(process.env.PORT || '9090'), apiKey: process.env.API_KEY || 'your-api-key-change-this', databasePath: process.env.DATABASE_PATH || './data/server.db', relayUrls: fileConfig.relayUrls || process.env.RELAY_URLS?.split(',') || ['ws://localhost:8090'], autoRestart: process.env.AUTO_RESTART === 'true', maxRestarts: parseInt(process.env.MAX_RESTARTS || '10'), logLevel: process.env.LOG_LEVEL || 'info' }; } export const config: AppConfig = loadConfig(); // Validate required environment variables if (!config.apiKey || config.apiKey === 'your-api-key-change-this') { console.warn('⚠️ Warning: Using default API key. Set API_KEY environment variable for production.'); }