Project started 🍾

This commit is contained in:
2026-06-01 18:47:59 +03:00
commit 4bca90a50e
39 changed files with 2340 additions and 0 deletions
+6
View File
@@ -0,0 +1,6 @@
"""Driven adapters — concrete implementations of domain ports.
ORM models, repositories, the async DB engine, the Redis client, source
backends, and external HTTP clients live here. Everything framework- and
vendor-specific is confined to this package.
"""
+5
View File
@@ -0,0 +1,5 @@
"""Redis adapter: shared connection pool for cache and arq broker."""
from app.infrastructure.cache.redis import close_redis, get_redis
__all__ = ["close_redis", "get_redis"]
+26
View File
@@ -0,0 +1,26 @@
"""Redis client provider — a single shared async connection pool."""
from redis.asyncio import Redis
from app.core.config import get_settings
_client: Redis | None = None
def get_redis() -> Redis:
"""Return the process-wide Redis client (created on first use)."""
global _client
if _client is None:
_client = Redis.from_url(
str(get_settings().redis_url),
encoding="utf-8",
decode_responses=True,
)
return _client
async def close_redis() -> None:
global _client
if _client is not None:
await _client.aclose()
_client = None
+17
View File
@@ -0,0 +1,17 @@
"""Database adapter: declarative base, async engine, session factory."""
from app.infrastructure.db.base import Base
from app.infrastructure.db.engine import (
dispose_engine,
get_engine,
get_sessionmaker,
session_scope,
)
__all__ = [
"Base",
"dispose_engine",
"get_engine",
"get_sessionmaker",
"session_scope",
]
+22
View File
@@ -0,0 +1,22 @@
"""Declarative base with a fixed naming convention.
The naming convention makes Alembic autogenerate deterministic, named
constraints — essential for clean, reversible migrations.
"""
from sqlalchemy import MetaData
from sqlalchemy.orm import DeclarativeBase
NAMING_CONVENTION = {
"ix": "ix_%(column_0_label)s",
"uq": "uq_%(table_name)s_%(column_0_name)s",
"ck": "ck_%(table_name)s_%(constraint_name)s",
"fk": "fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s",
"pk": "pk_%(table_name)s",
}
class Base(DeclarativeBase):
"""Base for all ORM models. Import models so Alembic sees their metadata."""
metadata = MetaData(naming_convention=NAMING_CONVENTION)
+61
View File
@@ -0,0 +1,61 @@
"""Async engine + session factory, created lazily from settings.
The engine is process-global and cached. The API binds a session per request
(see ``app.api.deps``); workers and scripts use :func:`session_scope`.
"""
from collections.abc import AsyncIterator
from contextlib import asynccontextmanager
from functools import lru_cache
from sqlalchemy.ext.asyncio import (
AsyncEngine,
AsyncSession,
async_sessionmaker,
create_async_engine,
)
from app.core.config import get_settings
@lru_cache
def get_engine() -> AsyncEngine:
settings = get_settings()
return create_async_engine(
settings.database_url,
echo=settings.db_echo,
pool_size=settings.db_pool_size,
max_overflow=settings.db_max_overflow,
pool_pre_ping=True, # survive Postgres restarts / idle drops
)
@lru_cache
def get_sessionmaker() -> async_sessionmaker[AsyncSession]:
return async_sessionmaker(
bind=get_engine(),
expire_on_commit=False,
autoflush=False,
)
@asynccontextmanager
async def session_scope() -> AsyncIterator[AsyncSession]:
"""Transactional session for workers/scripts: commit on success, rollback on error."""
session = get_sessionmaker()()
try:
yield session
await session.commit()
except Exception:
await session.rollback()
raise
finally:
await session.close()
async def dispose_engine() -> None:
"""Dispose the pooled engine on shutdown. Safe to call if never initialized."""
if get_engine.cache_info().currsize:
await get_engine().dispose()
get_engine.cache_clear()
get_sessionmaker.cache_clear()