import os from dataclasses import dataclass from functools import lru_cache def _get_bool_env(name: str, default: bool) -> bool: raw = os.getenv(name) if raw is None: return default return raw.strip().lower() in {"1", "true", "yes", "on"} @dataclass(frozen=True) class Settings: app_name: str = "Iron Gateway" debug: bool = False api_prefix: str = "/api" database_url: str = "sqlite+aiosqlite:///./iron.db" upload_temp_dir: str = "./.iron-temp/uploads" local_storage_dir: str = "./.iron-storage/local" cache_dir: str = "./.iron-cache" bootstrap_username: str = "admin" bootstrap_password: str = "changeme-iron" auth_session_ttl_hours: int = 24 * 7 job_poll_interval_seconds: float = 2.0 job_batch_size: int = 10 @classmethod def from_env(cls) -> "Settings": return cls( app_name=os.getenv("IRON_APP_NAME", cls.app_name), debug=_get_bool_env("IRON_DEBUG", cls.debug), api_prefix=os.getenv("IRON_API_PREFIX", cls.api_prefix), database_url=os.getenv("IRON_DATABASE_URL", cls.database_url), upload_temp_dir=os.getenv("IRON_UPLOAD_TEMP_DIR", cls.upload_temp_dir), local_storage_dir=os.getenv("IRON_LOCAL_STORAGE_DIR", cls.local_storage_dir), cache_dir=os.getenv("IRON_CACHE_DIR", cls.cache_dir), bootstrap_username=os.getenv("IRON_BOOTSTRAP_USERNAME", cls.bootstrap_username), bootstrap_password=os.getenv("IRON_BOOTSTRAP_PASSWORD", cls.bootstrap_password), auth_session_ttl_hours=int( os.getenv("IRON_AUTH_SESSION_TTL_HOURS", str(cls.auth_session_ttl_hours)) ), job_poll_interval_seconds=float( os.getenv("IRON_JOB_POLL_INTERVAL_SECONDS", str(cls.job_poll_interval_seconds)) ), job_batch_size=int(os.getenv("IRON_JOB_BATCH_SIZE", str(cls.job_batch_size))), ) @lru_cache(maxsize=1) def get_settings() -> Settings: return Settings.from_env()