diff --git a/docs/pages/getting_started.md b/docs/pages/getting_started.md index d5fec85..66a0b41 100644 --- a/docs/pages/getting_started.md +++ b/docs/pages/getting_started.md @@ -27,7 +27,7 @@ In this example, we will store the Redis URL and an API key for the weather serv === "@ Annotations" - ```python title="main.py" hl_lines="9 10 13 17" + ```python title="main.py" hl_lines="9-18" import os from wireup import container, initialize_container from myapp import services @@ -35,23 +35,25 @@ In this example, we will store the Redis URL and an API key for the weather serv def create_app(): app = ... - # Expose configuration by populating container.params. - container.params.put("redis_url", os.environ["APP_REDIS_URL"]) - container.params.put("weather_api_key", os.environ["APP_WEATHER_API_KEY"]) - - # Bulk update is also possible. - container.params.update(Settings().model_dump()) - - # Start the container: This registers + initializes services. - # `service_modules` contains top-level modules containing registrations. - initialize_container(container, service_modules=[services]) + # ⬇️ Start the container: This registers and initializes services. + initialize_container( + container, + # Parameters serve as application/service configuration. + parameters={ + "redis_url": os.environ["APP_REDIS_URL"], + "weather_api_key": os.environ["APP_WEATHER_API_KEY"] + }, + # Top-level modules containing service registrations. + # This is where your services live. + service_modules=[services] + ) return app ``` === "🏭 Programmatic" ```python title="main.py" hl_lines="15 19" - from pydantic import Field, PostgresDsn + from pydantic import Field from pydantic_settings import BaseSettings from wireup import container, initialize_container diff --git a/readme.md b/readme.md index 5d04f19..a3e080f 100644 --- a/readme.md +++ b/readme.md @@ -32,20 +32,22 @@ Example showcasing a Redis wrapper and a weather service that calls an external ```python from wireup import container, initialize_container + def create_app(): app = ... - - # ⬇️ Expose configuration by populating container.params. - container.params.put("redis_url", os.environ["APP_REDIS_URL"]) - container.params.put("weather_api_key", os.environ["APP_WEATHER_API_KEY"]) - - # Bulk update is possible via the "update" method. - container.params.update(Settings().model_dump()) - - # Start the container: This registers and initializes services. - # `service_modules` contains top-level modules containing registrations. - # ⬇️ - initialize_container(container, service_modules=[services]) + + # ⬇️ Start the container: This registers and initializes services. + initialize_container( + container, + # Parameters serve as application/service configuration. + parameters={ + "redis_url": os.environ["APP_REDIS_URL"], + "weather_api_key": os.environ["APP_WEATHER_API_KEY"] + }, + # Top-level modules containing service registrations. + # This is where your services live. + service_modules=[services] + ) return app ``` diff --git a/wireup/import_util.py b/wireup/import_util.py index 24f30bb..26e12af 100644 --- a/wireup/import_util.py +++ b/wireup/import_util.py @@ -18,12 +18,25 @@ from wireup import DependencyContainer -def initialize_container(dependency_container: DependencyContainer, *, service_modules: list[ModuleType]) -> None: +def initialize_container( + dependency_container: DependencyContainer, + *, + service_modules: list[ModuleType], + parameters: dict[str, Any] | None = None, +) -> None: """Trigger service registrations in `service_modules` and initialize registered singleton services. !!! note For long-lived processes this should be executed once at startup. + + :param dependency_container: The container to initialize. + :param service_modules: Top-level modules containing service registrations. + :param parameters: Parameters to be added to the container. + """ + if parameters: + dependency_container.params.update(parameters) + _register_services(dependency_container, service_modules) dependency_container.warmup() diff --git a/wireup/integration/django/apps.py b/wireup/integration/django/apps.py index cfa08b7..e0188ef 100644 --- a/wireup/integration/django/apps.py +++ b/wireup/integration/django/apps.py @@ -20,12 +20,13 @@ class WireupConfig(AppConfig): def ready(self) -> None: # noqa: D102 integration_settings: WireupSettings = settings.WIREUP - for entry in dir(settings): - if not entry.startswith("__") and hasattr(settings, entry): - container.params.put(entry, getattr(settings, entry)) - initialize_container( container, + parameters={ + entry: getattr(settings, entry) + for entry in dir(settings) + if not entry.startswith("__") and hasattr(settings, entry) + }, service_modules=[ importlib.import_module(m) if isinstance(m, str) else m for m in integration_settings.service_modules ],