from dataclasses import dataclass import os from pathlib import Path def _secret(file_variable: str, value_variable: str) -> str: file_path = os.environ.get(file_variable) if file_path: value = Path(file_path).read_text(encoding="utf-8").strip() else: value = os.environ.get(value_variable, "").strip() if not value: raise RuntimeError(f"Required setting {file_variable} is missing") return value def load_database_url() -> str: return _secret("DATABASE_URL_FILE", "DATABASE_URL") @dataclass(frozen=True) class Settings: cache_url: str session_ttl_seconds: int def load_settings() -> Settings: try: ttl = int(os.environ.get("SESSION_TTL_SECONDS", "3600")) except ValueError as exc: raise RuntimeError("SESSION_TTL_SECONDS must be an integer") from exc if ttl < 1 or ttl > 31_536_000: raise RuntimeError("SESSION_TTL_SECONDS must be between 1 and 31536000") return Settings( cache_url=_secret("CACHE_URL_FILE", "CACHE_URL"), session_ttl_seconds=ttl, )