diff --git a/core/routes/civitai_search.py b/core/routes/civitai_search.py index 6316eae..90a23cc 100644 --- a/core/routes/civitai_search.py +++ b/core/routes/civitai_search.py @@ -5,8 +5,10 @@ from .context import RouteContext from .helpers import register_service_route -def register_civitai_search_routes(context: RouteContext): - model_service = ModelService(context) +def register_civitai_search_routes( + context: RouteContext, + model_service: ModelService, +): register_service_route( context, path="/model_resolver/civitai-search", diff --git a/core/routes/custom_url.py b/core/routes/custom_url.py index adbb1db..bfe8648 100644 --- a/core/routes/custom_url.py +++ b/core/routes/custom_url.py @@ -5,8 +5,10 @@ from .context import RouteContext from .helpers import register_service_route -def register_custom_url_routes(context: RouteContext): - model_service = ModelService(context) +def register_custom_url_routes( + context: RouteContext, + model_service: ModelService, +): register_service_route( context, path="/model_resolver/custom-url", diff --git a/core/routes/model_details.py b/core/routes/model_details.py index b4fef3f..a590662 100644 --- a/core/routes/model_details.py +++ b/core/routes/model_details.py @@ -5,8 +5,10 @@ from .context import RouteContext from .helpers import register_service_route -def register_model_details_routes(context: RouteContext): - model_service = ModelService(context) +def register_model_details_routes( + context: RouteContext, + model_service: ModelService, +): register_service_route( context, path="/model_resolver/model-details", diff --git a/core/routes/registry.py b/core/routes/registry.py index 1da67fa..73c3ed7 100644 --- a/core/routes/registry.py +++ b/core/routes/registry.py @@ -91,6 +91,7 @@ def register_routes(self): search_local_matches_by_hash, ) from ..scanner import find_local_file_path, get_model_files, invalidate_model_files_cache + from ..services.model_service import ModelService from ..settings import ( TEMPLATE_KEY_ALIASES, get_default_root_for_category, @@ -249,9 +250,10 @@ def register_routes(self): register_hash_routes(route_context) register_metadata_routes(route_context) register_loaded_model_routes(route_context) - register_civitai_search_routes(route_context) - register_custom_url_routes(route_context) - register_model_details_routes(route_context) + model_service = ModelService(route_context) + register_civitai_search_routes(route_context, model_service) + register_custom_url_routes(route_context, model_service) + register_model_details_routes(route_context, model_service) if download_available: register_source_search_routes(route_context) diff --git a/tests/test_route_registry.py b/tests/test_route_registry.py index e41e5ce..8084224 100644 --- a/tests/test_route_registry.py +++ b/tests/test_route_registry.py @@ -85,6 +85,31 @@ def test_register_routes_registers_each_route_family(route_environment): }.issubset(routes.registered) +def test_register_routes_reuses_one_model_service_for_model_route_families( + monkeypatch, + route_environment, +): + extension, _ = route_environment + constructors = [] + service = SimpleNamespace( + civitai_search=MagicMock(), + custom_url=MagicMock(), + model_details=MagicMock(), + ) + + def build_service(context): + constructors.append(context) + return service + + service_module = importlib.import_module( + f"{PACKAGE_NAME}.core.services.model_service" + ) + monkeypatch.setattr(service_module, "ModelService", build_service) + + assert registry_module.register_routes(extension) is True + assert len(constructors) == 1 + + def test_register_routes_is_idempotent(route_environment): extension, routes = route_environment