feat: create backend skeleton
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
from .integrations.asyncio import FromDishka, Scope, inject
|
||||
|
||||
__all__ = ["FromDishka", "Scope", "inject"]
|
||||
@@ -0,0 +1,5 @@
|
||||
from dishka import make_async_container
|
||||
|
||||
from dependencies.providers.postgres import DbProvider
|
||||
|
||||
container = make_async_container(DbProvider())
|
||||
@@ -0,0 +1,3 @@
|
||||
from . import asyncio
|
||||
|
||||
__all__ = ["asyncio"]
|
||||
@@ -0,0 +1,25 @@
|
||||
from collections.abc import Callable
|
||||
|
||||
from dishka import FromDishka, Scope
|
||||
from dishka.integrations.base import wrap_injection
|
||||
|
||||
from dependencies.container import container
|
||||
|
||||
|
||||
def inject[**P, T](
|
||||
_func: Callable[P, T] | None = None, *, scope: Scope | None = None
|
||||
) -> Callable[P, T] | Callable[[Callable[P, T]], Callable[P, T]]:
|
||||
def decorator(func: Callable[P, T]) -> Callable[P, T]:
|
||||
return wrap_injection(
|
||||
func=func,
|
||||
is_async=True,
|
||||
container_getter=lambda _args, _kwargs: container,
|
||||
scope=scope,
|
||||
)
|
||||
|
||||
if _func is None:
|
||||
return decorator
|
||||
return decorator(_func)
|
||||
|
||||
|
||||
__all__ = ["FromDishka", "Scope", "container", "inject"]
|
||||
@@ -0,0 +1,27 @@
|
||||
from collections.abc import AsyncGenerator
|
||||
|
||||
import asyncpg
|
||||
from dishka import Provider, Scope, provide
|
||||
|
||||
from utils.env import env
|
||||
|
||||
|
||||
async def _init_connection(conn: asyncpg.Connection) -> None:
|
||||
await conn.execute("SET timezone TO 'UTC'")
|
||||
|
||||
|
||||
class DbProvider(Provider):
|
||||
@provide(scope=Scope.APP)
|
||||
async def get_pool(self) -> AsyncGenerator[asyncpg.Pool]:
|
||||
# noinspection PyUnresolvedReferences
|
||||
pool = await asyncpg.create_pool(
|
||||
dsn=env.db.connection_url,
|
||||
min_size=env.db.min_pool_size,
|
||||
max_size=env.db.max_pool_size,
|
||||
command_timeout=60,
|
||||
init=_init_connection,
|
||||
)
|
||||
try:
|
||||
yield pool
|
||||
finally:
|
||||
await pool.close()
|
||||
Reference in New Issue
Block a user