Files
mtusi2026lab4/app/config.py
T

59 lines
1.9 KiB
Python

"""Process configuration. Secrets are read from the environment, never logged."""
import os
from dataclasses import dataclass
def _required(name: str) -> str:
value = os.environ.get(name, "").strip()
if not value:
raise SystemExit(f"missing required environment variable {name}")
return value
def _int(name: str, default: int) -> int:
raw = os.environ.get(name)
if raw is None or raw.strip() == "":
return default
try:
return int(raw)
except ValueError as exc:
raise SystemExit(f"environment variable {name} must be an integer") from exc
@dataclass(frozen=True)
class Settings:
instance_id: str
database_url: str
s3_endpoint: str
s3_region: str
s3_bucket: str
s3_access_key: str
s3_secret_key: str
session_ttl_seconds: int
idempotency_ttl_seconds: int
max_upload_bytes: int
max_image_pixels: int
orphan_grace_seconds: int
app_env: str
# Full bitmap decode is bounded so one upload cannot expand to 40e6 pixels in RAM.
full_decode_pixels: int = 1_000_000
def load_settings() -> Settings:
return Settings(
instance_id=_required("INSTANCE_ID"),
database_url=_required("DATABASE_URL"),
s3_endpoint=_required("S3_ENDPOINT"),
s3_region=os.environ.get("S3_REGION", "us-east-1").strip() or "us-east-1",
s3_bucket=_required("S3_BUCKET"),
s3_access_key=_required("S3_ACCESS_KEY_ID"),
s3_secret_key=_required("S3_SECRET_ACCESS_KEY"),
session_ttl_seconds=_int("SESSION_TTL_SECONDS", 3600),
idempotency_ttl_seconds=_int("IDEMPOTENCY_TTL_SECONDS", 86400),
max_upload_bytes=_int("MAX_UPLOAD_BYTES", 10_485_760),
max_image_pixels=_int("MAX_IMAGE_PIXELS", 40_000_000),
orphan_grace_seconds=_int("ORPHAN_GRACE_SECONDS", 120),
app_env=os.environ.get("APP_ENV", "lab").strip() or "lab",
)