"""Runtime configuration for the backend service.""" from __future__ import annotations import os from dataclasses import dataclass def _env_str(name: str, default: str) -> str: value = os.getenv(name) return value if value not in (None, "") else default def _env_int(name: str, default: int) -> int: value = os.getenv(name) if value in (None, ""): return default return int(value) def _env_float(name: str, default: float) -> float: value = os.getenv(name) if value in (None, ""): return default return float(value) def _env_bool(name: str, default: bool) -> bool: value = os.getenv(name) if value in (None, ""): return default return value.strip().lower() in {"1", "true", "yes", "on"} @dataclass(frozen=True) class BackendSettings: db_dsn: str log_level: str db_pool_min_size: int db_pool_max_size: int broadcaster_queue_maxsize: int packet_tracker_finalize_delay_seconds: float packet_tracker_retention_seconds: float packet_tracker_min_flush_interval_seconds: float packet_tracker_persist_timeout_seconds: float packet_tracker_stop_join_timeout_seconds: float packet_tracker_reject_correlation_window_seconds: float sniffer_buffer_capacity: int sniffer_socket_rcvbuf_bytes: int sniffer_selector_timeout_seconds: float sniffer_recv_bytes: int sniffer_buffer_drain_interval_seconds: float sniffer_thread_join_timeout_seconds: float capture_interface: str capture_header_enabled: bool telemetry_process_stop_timeout_seconds: float telemetry_reader_join_timeout_seconds: float ndpi_max_flows: int ndpi_flow_ttl_seconds: float ndpi_cleanup_interval_packets: int def load_settings() -> BackendSettings: return BackendSettings( db_dsn=_env_str("BACKEND_DB_DSN", "postgresql://mitm_user:mitm_password@localhost:5432/mitm_db"), log_level=_env_str("BACKEND_LOG_LEVEL", "DEBUG"), db_pool_min_size=_env_int("BACKEND_DB_POOL_MIN_SIZE", 1), db_pool_max_size=_env_int("BACKEND_DB_POOL_MAX_SIZE", 5), broadcaster_queue_maxsize=_env_int("BACKEND_BROADCAST_QUEUE_MAXSIZE", 1024), packet_tracker_finalize_delay_seconds=_env_float("BACKEND_PACKET_TRACKER_FINALIZE_DELAY_SECONDS", 0.25), packet_tracker_retention_seconds=_env_float("BACKEND_PACKET_TRACKER_RETENTION_SECONDS", 10.0), packet_tracker_min_flush_interval_seconds=_env_float("BACKEND_PACKET_TRACKER_MIN_FLUSH_INTERVAL_SECONDS", 0.05), packet_tracker_persist_timeout_seconds=_env_float("BACKEND_PACKET_TRACKER_PERSIST_TIMEOUT_SECONDS", 2.0), packet_tracker_stop_join_timeout_seconds=_env_float("BACKEND_PACKET_TRACKER_STOP_JOIN_TIMEOUT_SECONDS", 2.0), packet_tracker_reject_correlation_window_seconds=_env_float( "BACKEND_PACKET_TRACKER_REJECT_CORRELATION_WINDOW_SECONDS", 1.0, ), sniffer_buffer_capacity=_env_int("BACKEND_SNIFFER_BUFFER_CAPACITY", 20_000), sniffer_socket_rcvbuf_bytes=_env_int("BACKEND_SNIFFER_SOCKET_RCVBUF_BYTES", 4 * 1024 * 1024), sniffer_selector_timeout_seconds=_env_float("BACKEND_SNIFFER_SELECTOR_TIMEOUT_SECONDS", 1.0), sniffer_recv_bytes=_env_int("BACKEND_SNIFFER_RECV_BYTES", 65_536), sniffer_buffer_drain_interval_seconds=_env_float("BACKEND_SNIFFER_BUFFER_DRAIN_INTERVAL_SECONDS", 5.0), sniffer_thread_join_timeout_seconds=_env_float("BACKEND_SNIFFER_THREAD_JOIN_TIMEOUT_SECONDS", 2.0), capture_interface=_env_str("BACKEND_CAPTURE_INTERFACE", ""), capture_header_enabled=_env_bool("BACKEND_CAPTURE_HEADER_ENABLED", True), telemetry_process_stop_timeout_seconds=_env_float("BACKEND_TELEMETRY_PROCESS_STOP_TIMEOUT_SECONDS", 3.0), telemetry_reader_join_timeout_seconds=_env_float("BACKEND_TELEMETRY_READER_JOIN_TIMEOUT_SECONDS", 2.0), ndpi_max_flows=_env_int("BACKEND_NDPI_MAX_FLOWS", 200_000), ndpi_flow_ttl_seconds=_env_float("BACKEND_NDPI_FLOW_TTL_SECONDS", 120.0), ndpi_cleanup_interval_packets=_env_int("BACKEND_NDPI_CLEANUP_INTERVAL_PACKETS", 10_000), ) settings = load_settings()