service_man/api/di.py

45 lines
1.1 KiB
Python
Raw Normal View History

2024-03-04 07:12:29 +03:00
import os
from dependency_injector import containers, providers
2024-03-04 13:15:28 +03:00
from api.repository.user import UserRepository
from api.service.user import UserService
from api.uow.database import Database
from api.uow.uow_base import UowBase
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
config = providers.Configuration(yaml_files=[f"{os.getenv('CONFIG_PATH')}"])
2024-03-04 13:15:28 +03:00
if os.getenv("INDOCKER"):
config.db.host.update("db")
2024-03-04 07:12:29 +03:00
db = providers.Singleton(
Database,
db_url="postgresql+asyncpg://{}:{}@{}:{}/{}".format(
config.db.user,
config.db.password,
config.db.host,
# config.db.port,
"5432",
config.db.database,
),
)
2024-03-04 13:15:28 +03:00
uow = providers.Factory(
UowBase,
session_factory=db.provided.session,
)
2024-03-04 07:12:29 +03:00
user_repository = providers.Factory(
UserRepository,
2024-03-04 13:15:28 +03:00
uow=uow,
2024-03-04 07:12:29 +03:00
)
user_service = providers.Factory(
UserService,
user_repository=user_repository,
)