Files
lauadminandClaude Sonnet 5 2664cb8213 Initial commit: backend scaffold, auth, frontend login
- FastAPI + SQLAlchemy async + Alembic + Postgres backend
- Auth: JWT access + rotating refresh tokens, argon2, roles, audit log
- React 19 + Vite frontend: login page, protected route, auth context
- Docker Compose: postgres, redis, migrate, api, worker (placeholder), frontend/nginx

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-22 15:07:15 +03:00

48 lines
1.5 KiB
Python

"""Фабрика асинхронных сессий SQLAlchemy."""
from __future__ import annotations
from collections.abc import AsyncGenerator
from contextlib import asynccontextmanager
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
from app.core.config import settings
engine = create_async_engine(
settings.database_url,
echo=False,
pool_pre_ping=True, # VPS-соединения умеют отваливаться молча
pool_size=10,
max_overflow=20,
)
SessionFactory = async_sessionmaker(
bind=engine,
class_=AsyncSession,
expire_on_commit=False, # объекты остаются пригодными после commit
autoflush=False,
)
async def get_session() -> AsyncGenerator[AsyncSession, None]:
"""Зависимость FastAPI. Откатывает транзакцию при любом исключении."""
async with SessionFactory() as session:
try:
yield session
except Exception:
await session.rollback()
raise
@asynccontextmanager
async def session_scope() -> AsyncGenerator[AsyncSession, None]:
"""Сессия для воркеров и CLI, где механизма зависимостей FastAPI нет."""
async with SessionFactory() as session:
try:
yield session
await session.commit()
except Exception:
await session.rollback()
raise