51 lines
1.5 KiB
Python
51 lines
1.5 KiB
Python
"""# Summary
|
|
|
|
Infrastructure initialization — connects MongoDB and wires all repos and services.
|
|
Shared bootstrap for both the FastAPI app and the worker, guarded against
|
|
repeated initialization via a module-level flag.
|
|
"""
|
|
from typing import Any, Dict
|
|
|
|
from config import settings
|
|
from domain.item.ports.db import create_item_repo
|
|
from domain.item.ports.service import get_item_service
|
|
from domain.job.ports.db import create_job_repo
|
|
from domain.job.ports.service import get_job_service
|
|
from infra.db.setup import setup_database, shutdown_database
|
|
|
|
_resources: Dict[str, Any] = {}
|
|
_initialized: bool = False
|
|
|
|
|
|
async def init() -> Dict[str, Any]:
|
|
"""Connect MongoDB, wire all repos and services, and return the resources dict.
|
|
|
|
Idempotent: repeated calls short-circuit and return the cached resources.
|
|
|
|
Returns:
|
|
Dict[str, Any]: Named services keyed by resource name.
|
|
"""
|
|
global _resources, _initialized
|
|
if _initialized:
|
|
return _resources
|
|
|
|
await setup_database(uri=settings.mongo_uri, db_name=settings.mongo_db)
|
|
|
|
item_repo = create_item_repo()
|
|
job_repo = create_job_repo()
|
|
|
|
_resources["item_repo"] = item_repo
|
|
_resources["job_repo"] = job_repo
|
|
_resources["item_svc"] = get_item_service(item_repo)
|
|
_resources["job_svc"] = get_job_service(job_repo)
|
|
|
|
_initialized = True
|
|
return _resources
|
|
|
|
|
|
async def shutdown() -> None:
|
|
"""Disconnect MongoDB and reset the resources cache."""
|
|
global _initialized
|
|
await shutdown_database()
|
|
_resources.clear()
|
|
_initialized = False |