"""Runtime configuration loaded from environment variables.""" from __future__ import annotations import os from dataclasses import dataclass from pathlib import Path @dataclass(frozen=True) class Settings: database_url: str session_ttl_seconds: int idempotency_ttl_seconds: int instance_id: str def _positive_int(name: str, default: int) -> int: raw = os.environ.get(name, str(default)) try: value = int(raw) except ValueError as exc: raise RuntimeError(f"{name} must be an integer") from exc if value < 1: raise RuntimeError(f"{name} must be positive") return value def load_settings() -> Settings: database_url = os.environ.get("DATABASE_URL", "").strip() if not database_url: secret_path = os.environ.get("DATABASE_URL_FILE", "").strip() if secret_path and Path(secret_path).is_file(): database_url = Path(secret_path).read_text(encoding="utf-8").strip() if not database_url: raise RuntimeError("DATABASE_URL or DATABASE_URL_FILE must be set") return Settings( database_url=database_url, session_ttl_seconds=_positive_int("SESSION_TTL_SECONDS", 3600), idempotency_ttl_seconds=_positive_int("IDEMPOTENCY_TTL_SECONDS", 86400), instance_id=os.environ.get("INSTANCE_ID") or os.environ.get("HOSTNAME") or "api", )