Реализовать лабораторную 2 на оценку 4
PostgreSQL как источник истины, непрозрачные сессии в Redis с TTL и AOF, миграции под advisory lock, пул и таймауты, проверки гонки/отката/отказов и отчёт с evidence.
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
"""Runtime configuration.
|
||||
|
||||
The application never embeds credentials. Local Compose passes environment
|
||||
variables from an ignored .env file; *_FILE variants are accepted as well so a
|
||||
container platform can mount secrets without copying them into the image.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def _value(name: str, fallback_name: str | None = None, default: str | None = None) -> str:
|
||||
raw = os.environ.get(name)
|
||||
if raw:
|
||||
return raw
|
||||
if fallback_name:
|
||||
filename = os.environ.get(fallback_name)
|
||||
if filename:
|
||||
path = Path(filename)
|
||||
text = path.read_text(encoding="utf-8").strip()
|
||||
if text:
|
||||
return text
|
||||
if default is not None:
|
||||
return default
|
||||
raise RuntimeError(f"missing required configuration: {name}")
|
||||
|
||||
|
||||
def _positive_int(name: str, default: int) -> int:
|
||||
raw = os.environ.get(name, str(default))
|
||||
try:
|
||||
value = int(raw)
|
||||
except ValueError as exc:
|
||||
raise RuntimeError(f"{name} must be an integer") from exc
|
||||
if value < 1:
|
||||
raise RuntimeError(f"{name} must be positive")
|
||||
return value
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class Settings:
|
||||
database_url: str
|
||||
cache_url: str
|
||||
session_ttl_seconds: int
|
||||
db_pool_min: int
|
||||
db_pool_max: int
|
||||
db_pool_acquire_timeout: float
|
||||
db_connect_timeout: int
|
||||
db_statement_timeout_ms: int
|
||||
redis_connect_timeout: float
|
||||
redis_socket_timeout: float
|
||||
max_body_bytes: int
|
||||
|
||||
|
||||
def load_settings() -> Settings:
|
||||
return Settings(
|
||||
database_url=_value("DATABASE_URL", "DATABASE_URL_FILE"),
|
||||
cache_url=_value("CACHE_URL", "CACHE_URL_FILE"),
|
||||
session_ttl_seconds=_positive_int("SESSION_TTL_SECONDS", 3600),
|
||||
db_pool_min=_positive_int("DB_POOL_MIN", 1),
|
||||
db_pool_max=_positive_int("DB_POOL_MAX", 10),
|
||||
db_pool_acquire_timeout=float(os.environ.get("DB_POOL_ACQUIRE_TIMEOUT_SECONDS", "3")),
|
||||
db_connect_timeout=_positive_int("DB_CONNECT_TIMEOUT_SECONDS", 3),
|
||||
db_statement_timeout_ms=_positive_int("DB_STATEMENT_TIMEOUT_MS", 5000),
|
||||
redis_connect_timeout=float(os.environ.get("REDIS_CONNECT_TIMEOUT_SECONDS", "2")),
|
||||
redis_socket_timeout=float(os.environ.get("REDIS_SOCKET_TIMEOUT_SECONDS", "2")),
|
||||
max_body_bytes=_positive_int("MAX_BODY_BYTES", 1024 * 1024),
|
||||
)
|
||||
|
||||
|
||||
settings = load_settings()
|
||||
Reference in New Issue
Block a user