service_man/api/di.py

59 lines
1.5 KiB
Python
Raw Normal View History

2024-03-04 07:12:29 +03:00
import os
2024-03-06 02:28:59 +03:00
import yaml # type: ignore
2024-03-04 07:12:29 +03:00
from dependency_injector import containers, providers
2024-03-06 02:28:59 +03:00
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
2024-03-04 13:15:28 +03:00
from api.service.user import UserService
2024-03-06 02:28:59 +03:00
from api.uow.uow_base import UnitOfWork
2024-03-04 07:12:29 +03:00
class Container(containers.DeclarativeContainer):
2024-03-04 13:15:28 +03:00
wiring_config = containers.WiringConfiguration(modules=["api.router.user"])
2024-03-04 07:12:29 +03:00
2024-03-06 02:28:59 +03:00
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()
2024-03-04 07:12:29 +03:00
2024-03-04 13:15:28 +03:00
if os.getenv("INDOCKER"):
2024-03-06 02:28:59 +03:00
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"],
2024-03-04 07:12:29 +03:00
),
2024-03-06 02:28:59 +03:00
echo=True,
2024-03-04 07:12:29 +03:00
)
2024-03-06 02:28:59 +03:00
async_session_factory = providers.Factory(
async_sessionmaker,
async_engine,
class_=AsyncSession,
expire_on_commit=False,
2024-03-04 13:15:28 +03:00
)
2024-03-06 02:28:59 +03:00
uow = providers.Factory(
UnitOfWork,
session_factory=async_session_factory,
2024-03-04 07:12:29 +03:00
)
2024-03-06 02:28:59 +03:00
#
# user_repository = providers.Factory(
# UserRepository,
# uow=uow,
# )
#
2024-03-04 07:12:29 +03:00
user_service = providers.Factory(
UserService,
2024-03-06 02:28:59 +03:00
uow=uow,
2024-03-04 07:12:29 +03:00
)