import json import os import yaml from typing import Dict, List, Any class Config: def __init__(self, config_path: str = None): if config_path is None: config_path = os.environ.get('EMERGENCY_CONFIG', 'config.json') self.config_path = config_path self.config = self._load_config() def _load_config(self) -> Dict[str, Any]: """Load configuration from file""" try: with open(self.config_path, 'r') as f: if self.config_path.endswith('.yaml') or self.config_path.endswith('.yml'): return yaml.safe_load(f) else: return json.load(f) except FileNotFoundError: raise Exception(f"Configuration file {self.config_path} not found") except Exception as e: raise Exception(f"Failed to load configuration: {str(e)}") @property def server_host(self) -> str: return self.config.get('server', {}).get('host', '127.0.0.1') @property def server_port(self) -> int: return self.config.get('server', {}).get('port', 1127) @property def key_route(self) -> str: return self.config.get('routes', {}).get('key_route', '/emergency-key-xyz123') @property def health_route(self) -> str: return self.config.get('routes', {}).get('health_route', '/health-check') @property def key_file_path(self) -> str: key_file = self.config.get('files', {}).get('key_file') if not key_file: raise Exception("key_file not configured") return key_file @property def dummy_file_path(self) -> str: dummy_file = self.config.get('files', {}).get('dummy_file') if not dummy_file: raise Exception("dummy_file not configured") return dummy_file @property def ntfy_backends_key(self) -> List[str]: backends = self.config.get('notifications', {}).get('key_backends', []) if not backends: raise Exception("No notification backends configured for key access") return backends @property def ntfy_backends_health(self) -> List[str]: backends = self.config.get('notifications', {}).get('health_backends', []) if not backends: raise Exception("No notification backends configured for health check") return backends @property def log_level(self) -> str: return self.config.get('notifications', {}).get('log_level', 'WARNING') @property def send_all_logs(self) -> bool: return self.config.get('notifications', {}).get('send_all_logs', True) @property def ntfy_key_message(self) -> str: return self.config.get('notifications', {}).get('key_message', 'EMERGENCY: Decryption key accessed') @property def ntfy_health_message(self) -> str: return self.config.get('notifications', {}).get('health_message', 'Health check performed')