"""Runtime configuration from environment; secrets are read from *_FILE paths.""" from __future__ import annotations import os from typing import Optional def _secret(name: str) -> Optional[str]: """NAME wins over NAME_FILE; NAME_FILE keeps the value out of `docker inspect`.""" value = os.environ.get(name) if value: return value.strip() path = os.environ.get(name + "_FILE") if path and os.path.exists(path): with open(path, encoding="utf-8") as fh: return fh.read().strip() or None return None def _int(name: str, default: int) -> int: return int(os.environ.get(name, default)) def _float(name: str, default: float) -> float: return float(os.environ.get(name, default)) DATABASE_URL = _secret("DATABASE_URL") CACHE_URL = _secret("CACHE_URL") SESSION_TTL_SECONDS = _int("SESSION_TTL_SECONDS", 3600) # Upper bound for one cached session entry; PostgreSQL is re-checked after it. SESSION_CACHE_MAX_SECONDS = _int("SESSION_CACHE_MAX_SECONDS", 300) DB_POOL_MAX = _int("DB_POOL_MAX", 10) DB_CONNECT_TIMEOUT = _float("DB_CONNECT_TIMEOUT", 3) DB_ACQUIRE_TIMEOUT = _float("DB_ACQUIRE_TIMEOUT", 3) DB_COMMAND_TIMEOUT = _float("DB_COMMAND_TIMEOUT", 5) CACHE_TIMEOUT = _float("CACHE_TIMEOUT", 0.3) CACHE_POOL_MAX = _int("CACHE_POOL_MAX", 20) # After a cache error, skip the cache for this long instead of paying the timeout per request. CACHE_RETRY_AFTER = _float("CACHE_RETRY_AFTER", 5) # Argon2id hashing is CPU/RAM heavy: bound how many run at once. PASSWORD_HASH_CONCURRENCY = _int("PASSWORD_HASH_CONCURRENCY", 4)