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 broker_url: str broker_stream: str session_ttl_seconds: int s3_endpoint: str s3_region: str s3_bucket: str s3_access_key_id: str s3_secret_access_key: str max_upload_bytes: int max_image_pixels: int worker_concurrency: int job_max_attempts: int job_timeout_seconds: int job_lease_seconds: int staging_retention_seconds: int def load_settings(*, require_cache: bool = True, require_broker: bool = True, require_s3: bool = True) -> 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") try: max_upload_bytes = int(os.environ.get("MAX_UPLOAD_BYTES", "10485760")) max_image_pixels = int(os.environ.get("MAX_IMAGE_PIXELS", "40000000")) worker_concurrency = int(os.environ.get("WORKER_CONCURRENCY", "1")) job_max_attempts = int(os.environ.get("JOB_MAX_ATTEMPTS", "5")) job_timeout_seconds = int(os.environ.get("JOB_TIMEOUT_SECONDS", "60")) job_lease_seconds = int(os.environ.get("JOB_LEASE_SECONDS", "180")) staging_retention_seconds = int(os.environ.get("STAGING_RETENTION_SECONDS", "86400")) except ValueError as exc: raise RuntimeError("upload, worker and job settings must be integers") from exc if not 1 <= max_upload_bytes <= 10_485_760: raise RuntimeError("MAX_UPLOAD_BYTES must be between 1 and 10485760") if not 1 <= max_image_pixels <= 40_000_000: raise RuntimeError("MAX_IMAGE_PIXELS must be between 1 and 40000000") endpoint = os.environ.get("S3_ENDPOINT", "http://s3:9000").strip() region = os.environ.get("S3_REGION", "us-east-1").strip() bucket = os.environ.get("S3_BUCKET", "messenger-attachments").strip() if not endpoint.startswith("http://") and not endpoint.startswith("https://"): raise RuntimeError("S3_ENDPOINT must be an HTTP(S) URL") if not region or not bucket: raise RuntimeError("S3_REGION and S3_BUCKET are required") if worker_concurrency != 1: raise RuntimeError("WORKER_CONCURRENCY is fixed at 1 per worker; scale worker replicas for more throughput") if not 1 <= job_max_attempts <= 20: raise RuntimeError("JOB_MAX_ATTEMPTS must be between 1 and 20") if not 5 <= job_timeout_seconds <= 600 or job_lease_seconds <= job_timeout_seconds: raise RuntimeError("JOB_LEASE_SECONDS must exceed JOB_TIMEOUT_SECONDS (5..600)") if staging_retention_seconds < 3600: raise RuntimeError("STAGING_RETENTION_SECONDS must be at least 3600") cache_file = os.environ.get("CACHE_URL_FILE") cache_value = os.environ.get("CACHE_URL", "").strip() cache_url = _secret("CACHE_URL_FILE", "CACHE_URL") if require_cache or cache_file or cache_value else "" # API processes do not publish directly; falling back keeps them independent # from broker credentials while the dispatcher and worker use the explicit URL. broker_file = os.environ.get("BROKER_URL_FILE") broker_url = Path(broker_file).read_text(encoding="utf-8").strip() if broker_file else os.environ.get("BROKER_URL", cache_url) if require_broker and not broker_url: raise RuntimeError("BROKER_URL_FILE is empty") s3_access_key_id = _secret("S3_ACCESS_KEY_ID_FILE", "S3_ACCESS_KEY_ID") if require_s3 else "" s3_secret_access_key = _secret("S3_SECRET_ACCESS_KEY_FILE", "S3_SECRET_ACCESS_KEY") if require_s3 else "" return Settings( cache_url=cache_url, broker_url=broker_url, broker_stream=os.environ.get("BROKER_STREAM", "messenger:attachment-jobs:v1"), session_ttl_seconds=ttl, s3_endpoint=endpoint.rstrip("/"), s3_region=region, s3_bucket=bucket, s3_access_key_id=s3_access_key_id, s3_secret_access_key=s3_secret_access_key, max_upload_bytes=max_upload_bytes, max_image_pixels=max_image_pixels, worker_concurrency=worker_concurrency, job_max_attempts=job_max_attempts, job_timeout_seconds=job_timeout_seconds, job_lease_seconds=job_lease_seconds, staging_retention_seconds=staging_retention_seconds, )