service_man/api/di.py

59 lines
1.5 KiB
Python

import os
import yaml # type: ignore
from dependency_injector import containers, providers
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
from api.service.user import UserService
from api.uow.uow_base import UnitOfWork
class Container(containers.DeclarativeContainer):
wiring_config = containers.WiringConfiguration(modules=["api.router.user"])
if not os.getenv("CONFIG_PATH"):
raise ValueError('Please set "CONFIG_PATH" variable in your environment')
with open(os.getenv("CONFIG_PATH", "")) as f:
config_data = yaml.safe_load(f)
config = providers.Configuration()
if os.getenv("INDOCKER"):
config_data["db"]["host"] = "db"
config_data["db"]["port"] = 5432
async_engine = providers.Factory(
create_async_engine,
"postgresql+asyncpg://{}:{}@{}:{}/{}".format(
config_data["db"]["user"],
config_data["db"]["password"],
config_data["db"]["host"],
config_data["db"]["port"],
config_data["db"]["database"],
),
echo=True,
)
async_session_factory = providers.Factory(
async_sessionmaker,
async_engine,
class_=AsyncSession,
expire_on_commit=False,
)
uow = providers.Factory(
UnitOfWork,
session_factory=async_session_factory,
)
#
# user_repository = providers.Factory(
# UserRepository,
# uow=uow,
# )
#
user_service = providers.Factory(
UserService,
uow=uow,
)