forked from mtusi2026/mtusi2026lab2
Lab2: PostgreSQL, Bearer sessions and Redis for grade 4
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
"""Конфигурация из окружения и secret-файлов."""
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def _read_secret(file_env: str, direct_env: str) -> str:
|
||||
path = os.environ.get(file_env, "").strip()
|
||||
if path:
|
||||
return Path(path).read_text(encoding="utf-8").strip()
|
||||
value = os.environ.get(direct_env, "").strip()
|
||||
if value:
|
||||
return value
|
||||
raise RuntimeError(f"Задайте {file_env} или {direct_env}")
|
||||
|
||||
|
||||
class Settings:
|
||||
def __init__(self) -> None:
|
||||
self.host = os.environ.get("APP_HOST", "0.0.0.0")
|
||||
self.port = int(os.environ.get("APP_PORT", "8080"))
|
||||
self.env = os.environ.get("APP_ENV", "lab")
|
||||
self.session_ttl_seconds = int(os.environ.get("SESSION_TTL_SECONDS", "3600"))
|
||||
if self.session_ttl_seconds < 1:
|
||||
raise RuntimeError("SESSION_TTL_SECONDS должен быть >= 1")
|
||||
self.session_store = os.environ.get("SESSION_STORE", "redis").strip().lower()
|
||||
self.database_url = _read_secret("DATABASE_URL_FILE", "DATABASE_URL")
|
||||
self.cache_url = _read_secret("CACHE_URL_FILE", "CACHE_URL")
|
||||
self.db_connect_timeout = float(os.environ.get("DB_CONNECT_TIMEOUT", "5"))
|
||||
self.db_pool_min = int(os.environ.get("DB_POOL_MIN", "1"))
|
||||
self.db_pool_max = int(os.environ.get("DB_POOL_MAX", "10"))
|
||||
self.redis_socket_timeout = float(os.environ.get("REDIS_SOCKET_TIMEOUT", "3"))
|
||||
|
||||
|
||||
settings = Settings()
|
||||
Reference in New Issue
Block a user