From d9d471d771f659f7ccfc2ba2a7573fcb97b5494a Mon Sep 17 00:00:00 2001 From: Emmanuel Evbuomwan Date: Fri, 22 Nov 2024 13:37:43 +0100 Subject: [PATCH] feat: added basic DI - leaving an example by killing the usage of ConfigDependencyManager --- container/start.sh | 1 - pyproject.toml | 6 ++++ src/karapace/auth/dependencies.py | 2 +- src/schema_registry/__main__.py | 30 +++++++++++++------ src/schema_registry/base_config.yaml | 3 ++ src/schema_registry/container.py | 14 +++++++++ .../dependencies/config_dependency.py | 0 .../dependencies/controller_dependency.py | 6 ++-- .../dependencies/forward_client_dependency.py | 0 .../schema_registry_dependency.py | 2 +- .../dependencies/stats_dependeny.py | 2 +- .../routers/compatibility_router.py | 2 +- src/schema_registry/routers/config_router.py | 6 ++-- src/schema_registry/routers/health_router.py | 2 +- src/schema_registry/routers/mode_router.py | 2 +- src/schema_registry/routers/schemas_router.py | 2 +- .../routers/subjects_router.py | 6 ++-- 17 files changed, 60 insertions(+), 26 deletions(-) create mode 100644 src/schema_registry/base_config.yaml rename src/{karapace => schema_registry}/dependencies/config_dependency.py (100%) rename src/{karapace => schema_registry}/dependencies/controller_dependency.py (71%) rename src/{karapace => schema_registry}/dependencies/forward_client_dependency.py (100%) rename src/{karapace => schema_registry}/dependencies/schema_registry_dependency.py (90%) rename src/{karapace => schema_registry}/dependencies/stats_dependeny.py (87%) diff --git a/container/start.sh b/container/start.sh index f65dcfb31..a00f045e0 100755 --- a/container/start.sh +++ b/container/start.sh @@ -28,7 +28,6 @@ rest) exec python3 -m karapace.karapace_all /opt/karapace/rest.config.json ;; registry) - echo "Starting Karapace Schema Registry" exec python3 -m schema_registry ;; *) diff --git a/pyproject.toml b/pyproject.toml index c5bad6d99..8cd2b27cc 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -109,6 +109,12 @@ typing = [ [tool.setuptools] include-package-data = true +[tool.setuptools.packages.find] +where = ["src"] + +[tool.setuptools.package-data] +schema_registry = ["*.yaml"] + [tool.setuptools_scm] version_file = "src/karapace/version.py" diff --git a/src/karapace/auth/dependencies.py b/src/karapace/auth/dependencies.py index a3001fc8f..671769714 100644 --- a/src/karapace/auth/dependencies.py +++ b/src/karapace/auth/dependencies.py @@ -7,7 +7,7 @@ from fastapi.security import HTTPBasic, HTTPBasicCredentials from fastapi.security.base import SecurityBase from karapace.auth.auth import AuthenticationError, AuthenticatorAndAuthorizer, HTTPAuthorizer, NoAuthAndAuthz, User -from karapace.dependencies.config_dependency import ConfigDependencyManager +from schema_registry.dependencies.config_dependency import ConfigDependencyManager from typing import Annotated, Optional import logging diff --git a/src/schema_registry/__main__.py b/src/schema_registry/__main__.py index 89408e4c6..31a972576 100644 --- a/src/schema_registry/__main__.py +++ b/src/schema_registry/__main__.py @@ -4,14 +4,13 @@ """ from collections.abc import AsyncGenerator from contextlib import asynccontextmanager -from fastapi import FastAPI +from fastapi import FastAPI, Depends from karapace import version as karapace_version from karapace.auth.auth import AuthenticatorAndAuthorizer from karapace.auth.dependencies import AuthorizationDependencyManager from karapace.config import Config -from karapace.dependencies.config_dependency import ConfigDependencyManager -from karapace.dependencies.schema_registry_dependency import SchemaRegistryDependencyManager -from karapace.dependencies.stats_dependeny import StatsDependencyManager +from schema_registry.dependencies.schema_registry_dependency import SchemaRegistryDependencyManager +from schema_registry.dependencies.stats_dependeny import StatsDependencyManager from karapace.logging import configure_logging, log_config_without_secrets from karapace.schema_registry import KarapaceSchemaRegistry from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor @@ -19,9 +18,14 @@ from schema_registry.middlewares import setup_middlewares from schema_registry.routers import setup_routers from typing import Final +from schema_registry.container import SchemaRegistryContainer +from dependency_injector.wiring import Provide, inject import logging import uvicorn +from pathlib import Path + +SCHEMA_REGISTRY_ROOT: Final[Path] = Path(__file__).parent @asynccontextmanager @@ -43,7 +47,8 @@ async def lifespan(_: FastAPI) -> AsyncGenerator[None, None]: await authorizer.close() -def create_karapace_application(*, config: Config) -> FastAPI: +@inject +def create_karapace_application(*, config: Config = Depends(Provide[SchemaRegistryContainer.config])) -> FastAPI: configure_logging(config=config) log_config_without_secrets(config=config) logging.info("Starting Karapace Schema Registry (%s)", karapace_version.__version__) @@ -58,8 +63,15 @@ def create_karapace_application(*, config: Config) -> FastAPI: return app -CONFIG: Final = ConfigDependencyManager.get_config() - if __name__ == "__main__": - app = create_karapace_application(config=CONFIG) - uvicorn.run(app, host=CONFIG.host, port=CONFIG.port, log_level=CONFIG.log_level.lower()) + container = SchemaRegistryContainer() + container.base_config.from_yaml(f"{SCHEMA_REGISTRY_ROOT / 'base_config.yaml'}", envs_required=True, required=True) + container.wire(modules=[__name__,]) + + app = create_karapace_application() + uvicorn.run( + app, + host=container.config().host, + port=container.config().port, + log_level=container.config().log_level.lower() + ) diff --git a/src/schema_registry/base_config.yaml b/src/schema_registry/base_config.yaml new file mode 100644 index 000000000..5aa4d42d2 --- /dev/null +++ b/src/schema_registry/base_config.yaml @@ -0,0 +1,3 @@ +karapace: + env_file: ${KARAPACE_DOTENV} + env_file_encoding: utf-8 diff --git a/src/schema_registry/container.py b/src/schema_registry/container.py index f53be7121..ad66de53e 100644 --- a/src/schema_registry/container.py +++ b/src/schema_registry/container.py @@ -2,3 +2,17 @@ Copyright (c) 2024 Aiven Ltd See LICENSE for details """ + +from dependency_injector import containers, providers +from dependency_injector.wiring import Provide, inject + +from karapace.config import Config + + +class SchemaRegistryContainer(containers.DeclarativeContainer): + base_config = providers.Configuration() + config = providers.Singleton( + Config, + _env_file=base_config.karapace.env_file, + _env_file_encoding=base_config.karapace.env_file_encoding, + ) diff --git a/src/karapace/dependencies/config_dependency.py b/src/schema_registry/dependencies/config_dependency.py similarity index 100% rename from src/karapace/dependencies/config_dependency.py rename to src/schema_registry/dependencies/config_dependency.py diff --git a/src/karapace/dependencies/controller_dependency.py b/src/schema_registry/dependencies/controller_dependency.py similarity index 71% rename from src/karapace/dependencies/controller_dependency.py rename to src/schema_registry/dependencies/controller_dependency.py index 5e0f21e6b..60da9bf29 100644 --- a/src/karapace/dependencies/controller_dependency.py +++ b/src/schema_registry/dependencies/controller_dependency.py @@ -5,9 +5,9 @@ from fastapi import Depends -from karapace.dependencies.config_dependency import ConfigDep -from karapace.dependencies.schema_registry_dependency import SchemaRegistryDep -from karapace.dependencies.stats_dependeny import StatsDep +from schema_registry.dependencies.config_dependency import ConfigDep +from schema_registry.dependencies.schema_registry_dependency import SchemaRegistryDep +from schema_registry.dependencies.stats_dependeny import StatsDep from schema_registry.schema_registry_apis import KarapaceSchemaRegistryController from typing import Annotated diff --git a/src/karapace/dependencies/forward_client_dependency.py b/src/schema_registry/dependencies/forward_client_dependency.py similarity index 100% rename from src/karapace/dependencies/forward_client_dependency.py rename to src/schema_registry/dependencies/forward_client_dependency.py diff --git a/src/karapace/dependencies/schema_registry_dependency.py b/src/schema_registry/dependencies/schema_registry_dependency.py similarity index 90% rename from src/karapace/dependencies/schema_registry_dependency.py rename to src/schema_registry/dependencies/schema_registry_dependency.py index 68d9b0700..4823a4cc1 100644 --- a/src/karapace/dependencies/schema_registry_dependency.py +++ b/src/schema_registry/dependencies/schema_registry_dependency.py @@ -4,7 +4,7 @@ """ from fastapi import Depends -from karapace.dependencies.config_dependency import ConfigDependencyManager +from schema_registry.dependencies.config_dependency import ConfigDependencyManager from karapace.schema_registry import KarapaceSchemaRegistry from typing import Annotated diff --git a/src/karapace/dependencies/stats_dependeny.py b/src/schema_registry/dependencies/stats_dependeny.py similarity index 87% rename from src/karapace/dependencies/stats_dependeny.py rename to src/schema_registry/dependencies/stats_dependeny.py index 98c116dac..b619443c5 100644 --- a/src/karapace/dependencies/stats_dependeny.py +++ b/src/schema_registry/dependencies/stats_dependeny.py @@ -5,7 +5,7 @@ from fastapi import Depends -from karapace.dependencies.config_dependency import ConfigDependencyManager +from schema_registry.dependencies.config_dependency import ConfigDependencyManager from karapace.statsd import StatsClient from typing import Annotated diff --git a/src/schema_registry/routers/compatibility_router.py b/src/schema_registry/routers/compatibility_router.py index 25c759553..108773981 100644 --- a/src/schema_registry/routers/compatibility_router.py +++ b/src/schema_registry/routers/compatibility_router.py @@ -6,7 +6,7 @@ from fastapi import APIRouter from karapace.auth.auth import Operation from karapace.auth.dependencies import AuthenticatorAndAuthorizerDep, CurrentUserDep -from karapace.dependencies.controller_dependency import KarapaceSchemaRegistryControllerDep +from schema_registry.dependencies.controller_dependency import KarapaceSchemaRegistryControllerDep from karapace.typing import Subject from schema_registry.routers.errors import unauthorized from schema_registry.routers.requests import CompatibilityCheckResponse, SchemaRequest diff --git a/src/schema_registry/routers/config_router.py b/src/schema_registry/routers/config_router.py index b5b77b4bb..e7d6c22b4 100644 --- a/src/schema_registry/routers/config_router.py +++ b/src/schema_registry/routers/config_router.py @@ -6,9 +6,9 @@ from fastapi import APIRouter, Request from karapace.auth.auth import Operation from karapace.auth.dependencies import AuthenticatorAndAuthorizerDep, CurrentUserDep -from karapace.dependencies.controller_dependency import KarapaceSchemaRegistryControllerDep -from karapace.dependencies.forward_client_dependency import ForwardClientDep -from karapace.dependencies.schema_registry_dependency import SchemaRegistryDep +from schema_registry.dependencies.controller_dependency import KarapaceSchemaRegistryControllerDep +from schema_registry.dependencies.forward_client_dependency import ForwardClientDep +from schema_registry.dependencies.schema_registry_dependency import SchemaRegistryDep from karapace.typing import Subject from schema_registry.routers.errors import no_primary_url_error, unauthorized from schema_registry.routers.requests import CompatibilityLevelResponse, CompatibilityRequest, CompatibilityResponse diff --git a/src/schema_registry/routers/health_router.py b/src/schema_registry/routers/health_router.py index 9ef323957..36a3c6975 100644 --- a/src/schema_registry/routers/health_router.py +++ b/src/schema_registry/routers/health_router.py @@ -4,7 +4,7 @@ """ from fastapi import APIRouter, HTTPException, status -from karapace.dependencies.schema_registry_dependency import SchemaRegistryDep +from schema_registry.dependencies.schema_registry_dependency import SchemaRegistryDep from pydantic import BaseModel diff --git a/src/schema_registry/routers/mode_router.py b/src/schema_registry/routers/mode_router.py index 0789d2baf..5b6fca0c9 100644 --- a/src/schema_registry/routers/mode_router.py +++ b/src/schema_registry/routers/mode_router.py @@ -6,7 +6,7 @@ from fastapi import APIRouter from karapace.auth.auth import Operation from karapace.auth.dependencies import AuthenticatorAndAuthorizerDep, CurrentUserDep -from karapace.dependencies.controller_dependency import KarapaceSchemaRegistryControllerDep +from schema_registry.dependencies.controller_dependency import KarapaceSchemaRegistryControllerDep from karapace.typing import Subject from schema_registry.routers.errors import unauthorized diff --git a/src/schema_registry/routers/schemas_router.py b/src/schema_registry/routers/schemas_router.py index 58f5bd7da..048d52e15 100644 --- a/src/schema_registry/routers/schemas_router.py +++ b/src/schema_registry/routers/schemas_router.py @@ -5,7 +5,7 @@ from fastapi import APIRouter from karapace.auth.dependencies import AuthenticatorAndAuthorizerDep, CurrentUserDep -from karapace.dependencies.controller_dependency import KarapaceSchemaRegistryControllerDep +from schema_registry.dependencies.controller_dependency import KarapaceSchemaRegistryControllerDep from schema_registry.routers.requests import SchemaListingItem, SchemasResponse, SubjectVersion schemas_router = APIRouter( diff --git a/src/schema_registry/routers/subjects_router.py b/src/schema_registry/routers/subjects_router.py index 0189b398d..29e58e840 100644 --- a/src/schema_registry/routers/subjects_router.py +++ b/src/schema_registry/routers/subjects_router.py @@ -6,9 +6,9 @@ from fastapi import APIRouter, Request from karapace.auth.auth import Operation from karapace.auth.dependencies import AuthenticatorAndAuthorizerDep, CurrentUserDep -from karapace.dependencies.controller_dependency import KarapaceSchemaRegistryControllerDep -from karapace.dependencies.forward_client_dependency import ForwardClientDep -from karapace.dependencies.schema_registry_dependency import SchemaRegistryDep +from schema_registry.dependencies.controller_dependency import KarapaceSchemaRegistryControllerDep +from schema_registry.dependencies.forward_client_dependency import ForwardClientDep +from schema_registry.dependencies.schema_registry_dependency import SchemaRegistryDep from karapace.typing import Subject from schema_registry.routers.errors import no_primary_url_error, unauthorized from schema_registry.routers.requests import SchemaIdResponse, SchemaRequest, SchemaResponse, SubjectSchemaVersionResponse