ЛР2: PostgreSQL, сессии (Basic + Bearer), Compose, миграции
Skeleton contracts / contracts (pull_request) Canceled after 0s
Skeleton contracts / contracts (pull_request) Canceled after 0s
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
from pathlib import Path
|
||||
from typing import Literal
|
||||
|
||||
from pydantic import BaseModel
|
||||
from pydantic_settings import BaseSettings, SettingsConfigDict
|
||||
|
||||
# Корень проекта (в контейнере код лежит в /app, и это будет "/").
|
||||
BASE_DIR = Path(__file__).resolve().parents[2]
|
||||
|
||||
|
||||
class RunConfig(BaseModel):
|
||||
host: str = "0.0.0.0"
|
||||
port: int = 8080
|
||||
graceful_timeout: int = 5
|
||||
|
||||
|
||||
class LoggingConfig(BaseModel):
|
||||
level: Literal["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"] = "INFO"
|
||||
format: str = "[%(asctime)s] [%(levelname)s] [%(name)s] %(message)s"
|
||||
datefmt: str = "%Y-%m-%d %H:%M:%S"
|
||||
|
||||
|
||||
class DatabaseConfig(BaseModel):
|
||||
# Значения по умолчанию — для запуска внутри Compose.
|
||||
host: str = "postgres"
|
||||
port: int = 5432
|
||||
name: str = "messenger"
|
||||
user: str = "messenger"
|
||||
password_file: Path = Path("/run/secrets/postgres_password")
|
||||
|
||||
pool_size: int = 5
|
||||
max_overflow: int = 5
|
||||
pool_timeout: int = 5
|
||||
connect_timeout: int = 3
|
||||
statement_timeout_ms: int = 5000
|
||||
echo: bool = False
|
||||
|
||||
def read_password(self) -> str:
|
||||
return read_secret(self.password_file)
|
||||
|
||||
|
||||
class JWTConfig(BaseModel):
|
||||
# Ключ должен сохраняться между перезапусками, иначе выданные токены станут недействительны.
|
||||
secret_file: Path = Path("/run/secrets/jwt_secret")
|
||||
algorithm: str = "HS256"
|
||||
session_ttl_seconds: int = 3600
|
||||
|
||||
def read_secret_key(self) -> str:
|
||||
return read_secret(self.secret_file)
|
||||
|
||||
|
||||
def read_secret(path: Path) -> str:
|
||||
"""Прочитать файл-секрет. Относительный путь считается от корня проекта."""
|
||||
full_path = path if path.is_absolute() else BASE_DIR / path
|
||||
return full_path.read_text(encoding="utf-8").strip()
|
||||
|
||||
|
||||
class ApiV1Prefix(BaseModel):
|
||||
prefix: str = "/v1"
|
||||
users: str = "/users"
|
||||
chats: str = "/chats"
|
||||
auth: str = "/auth"
|
||||
|
||||
|
||||
class ApiPrefix(BaseModel):
|
||||
prefix: str = "/api"
|
||||
v1: ApiV1Prefix = ApiV1Prefix()
|
||||
|
||||
|
||||
class Settings(BaseSettings):
|
||||
model_config = SettingsConfigDict(
|
||||
env_file=BASE_DIR / ".env",
|
||||
case_sensitive=False,
|
||||
env_nested_delimiter="__",
|
||||
env_prefix="APP_CONFIG__",
|
||||
extra="ignore",
|
||||
)
|
||||
|
||||
run: RunConfig = RunConfig()
|
||||
api: ApiPrefix = ApiPrefix()
|
||||
logging: LoggingConfig = LoggingConfig()
|
||||
db: DatabaseConfig = DatabaseConfig()
|
||||
jwt: JWTConfig = JWTConfig()
|
||||
|
||||
|
||||
settings = Settings()
|
||||
Reference in New Issue
Block a user