Files
Vishesh 'ironeagle' Bangotra 0cb35d10a6 Phase 7: serve the vet sample from the template tools and docs
- build.py: _TEMPLATE_COMPONENTS rewritten around the vet sample (53 kinds —
  per-resource slices, jobs, worked appointment_reminder family, tests);
  seed tokens dropped from the manifest ('sample: vet' instead)
- registry.py: get_app_component(kind) no longer renames item/example seeds;
  prompts and tool docs updated to 'copy a slice and adapt'
- templates/README.md, 01_build_recipe, 40_ui_admin, wiki/03 updated off the
  old seed scheme; repo inventory/config now exclude vet (docs move under
  standards; react/vet itself is deleted in a later phase)
- regenerated MCP bundle, vendored templates, and site output
2026-10-20 14:58:17 +05:30

83 lines
3.0 KiB
Python

"""# Summary
Composition root — connects MongoDB (when selected), wires all repos and
services, and returns the resources dict. Shared bootstrap for the FastAPI
app and the seed/drop scripts, guarded against repeated initialization via a
module-level flag.
---
The ``memory`` backend skips the Mongo connection entirely — repos are
process-local dictionaries, so a plain ``uvicorn main:app`` runs with zero
external services.
"""
from typing import Any
from config import settings
from domain.appointments.ports.db import create_appointment_repo
from domain.appointments.ports.service import get_appointment_service
from domain.jobs.ports.db import create_job_repo
from domain.jobs.ports.service import get_job_service
from domain.parents.ports.db import create_parent_repo
from domain.parents.ports.service import get_parent_service
from domain.pets.ports.db import create_pet_repo
from domain.pets.ports.service import get_pet_service
from domain.treatments.ports.db import create_treatment_repo
from domain.treatments.ports.service import get_treatment_service
from domain.vets.ports.db import create_vet_repo
from domain.vets.ports.service import get_vet_service
from infra.db.setup import setup_database, shutdown_database
_resources: dict[str, Any] = {}
_initialized: bool = False
async def init() -> dict[str, Any]:
"""Wire all repos and services, and return the resources dict.
Idempotent: repeated calls short-circuit and return the cached resources.
Only the ``mongo`` backend performs a network connection here.
Returns:
Dict[str, Any]: Named services keyed by resource name
(``<resource>_svc`` and ``<resource>_repo``).
"""
global _resources, _initialized
if _initialized:
return _resources
if settings.repo_backend == "mongo":
await setup_database(uri=settings.mongo_uri, db_name=settings.mongo_db)
parent_repo = create_parent_repo()
vet_repo = create_vet_repo()
treatment_repo = create_treatment_repo()
pet_repo = create_pet_repo()
appointment_repo = create_appointment_repo()
job_repo = create_job_repo()
_resources["parent_repo"] = parent_repo
_resources["vet_repo"] = vet_repo
_resources["treatment_repo"] = treatment_repo
_resources["pet_repo"] = pet_repo
_resources["appointment_repo"] = appointment_repo
_resources["job_repo"] = job_repo
_resources["parent_svc"] = get_parent_service(parent_repo)
_resources["vet_svc"] = get_vet_service(vet_repo)
_resources["treatment_svc"] = get_treatment_service(treatment_repo)
_resources["pet_svc"] = get_pet_service(pet_repo)
_resources["appointment_svc"] = get_appointment_service(appointment_repo)
_resources["job_svc"] = get_job_service(job_repo)
_initialized = True
return _resources
async def shutdown() -> None:
"""Disconnect MongoDB (if connected) and reset the resources cache."""
global _initialized
if settings.repo_backend == "mongo":
await shutdown_database()
_resources.clear()
_initialized = False