Реализовать лабораторную 4 на оценку 5
Добавить приватное S3-хранилище, вложения, presigned upload, reconciler и доказательства.
This commit is contained in:
+134
@@ -0,0 +1,134 @@
|
||||
"""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.
|
||||
|
||||
Laboratory 4 adds S3 settings. The API uses a restricted application identity;
|
||||
only the one-shot bootstrap service receives the administrative identity.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import socket
|
||||
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
|
||||
|
||||
|
||||
def _positive_float(name: str, default: float) -> float:
|
||||
raw = os.environ.get(name, str(default))
|
||||
try:
|
||||
value = float(raw)
|
||||
except ValueError as exc:
|
||||
raise RuntimeError(f"{name} must be a number") from exc
|
||||
if value <= 0:
|
||||
raise RuntimeError(f"{name} must be positive")
|
||||
return value
|
||||
|
||||
|
||||
def _instance_id() -> str:
|
||||
"""Stable public alias of one API process/container.
|
||||
|
||||
Containers use their hostname (the short container id) by default. An
|
||||
explicit INSTANCE_ID is useful for non-container deployments or for a
|
||||
randomly generated lab7 alias. The value never contains host IP data.
|
||||
"""
|
||||
value = (os.environ.get("INSTANCE_ID") or socket.gethostname()).strip()
|
||||
if not value or len(value) > 128 or any(not (c.isalnum() or c in "-._:") for c in value):
|
||||
raise RuntimeError("INSTANCE_ID must be 1..128 safe characters")
|
||||
return value
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class Settings:
|
||||
database_url: str
|
||||
cache_url: str
|
||||
instance_id: str
|
||||
session_ttl_seconds: int
|
||||
idempotency_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
|
||||
max_upload_bytes: int
|
||||
max_image_pixels: int
|
||||
max_decode_concurrency: int
|
||||
upload_temp_dir: str
|
||||
s3_endpoint: str
|
||||
s3_public_endpoint: str
|
||||
s3_region: str
|
||||
s3_bucket: str
|
||||
s3_access_key_id: str
|
||||
s3_secret_access_key: str
|
||||
s3_connect_timeout: float
|
||||
s3_read_timeout: float
|
||||
presign_ttl_seconds: int
|
||||
presign_grace_seconds: int
|
||||
orphan_grace_seconds: int
|
||||
|
||||
|
||||
def load_settings() -> Settings:
|
||||
return Settings(
|
||||
database_url=_value("DATABASE_URL", "DATABASE_URL_FILE"),
|
||||
cache_url=_value("CACHE_URL", "CACHE_URL_FILE"),
|
||||
instance_id=_instance_id(),
|
||||
session_ttl_seconds=_positive_int("SESSION_TTL_SECONDS", 3600),
|
||||
idempotency_ttl_seconds=_positive_int("IDEMPOTENCY_TTL_SECONDS", 86400),
|
||||
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),
|
||||
max_upload_bytes=_positive_int("MAX_UPLOAD_BYTES", 10 * 1024 * 1024),
|
||||
max_image_pixels=_positive_int("MAX_IMAGE_PIXELS", 40_000_000),
|
||||
max_decode_concurrency=_positive_int("MAX_DECODE_CONCURRENCY", 1),
|
||||
upload_temp_dir=os.environ.get("UPLOAD_TEMP_DIR", "/tmp"),
|
||||
s3_endpoint=_value("S3_ENDPOINT", "S3_ENDPOINT_FILE", "http://seaweedfs:8333"),
|
||||
s3_public_endpoint=_value("S3_PUBLIC_ENDPOINT", "S3_PUBLIC_ENDPOINT_FILE", "http://localhost:9000"),
|
||||
s3_region=_value("S3_REGION", default="us-east-1"),
|
||||
s3_bucket=_value("S3_BUCKET", default="messenger-attachments"),
|
||||
s3_access_key_id=_value("S3_ACCESS_KEY_ID", "S3_ACCESS_KEY_ID_FILE"),
|
||||
s3_secret_access_key=_value("S3_SECRET_ACCESS_KEY", "S3_SECRET_ACCESS_KEY_FILE"),
|
||||
s3_connect_timeout=_positive_float("S3_CONNECT_TIMEOUT_SECONDS", 3.0),
|
||||
s3_read_timeout=_positive_float("S3_READ_TIMEOUT_SECONDS", 30.0),
|
||||
presign_ttl_seconds=_positive_int("PRESIGN_TTL_SECONDS", 300),
|
||||
presign_grace_seconds=_positive_int("PRESIGN_GRACE_SECONDS", 3600),
|
||||
orphan_grace_seconds=_positive_int("ORPHAN_GRACE_SECONDS", 86400),
|
||||
)
|
||||
|
||||
|
||||
settings = load_settings()
|
||||
Reference in New Issue
Block a user