Skip to content

Commit

Permalink
feat: added basic DI
Browse files Browse the repository at this point in the history
- leaving an example by killing the usage of ConfigDependencyManager
  • Loading branch information
nosahama committed Nov 22, 2024
1 parent 609eb45 commit d9d471d
Show file tree
Hide file tree
Showing 17 changed files with 60 additions and 26 deletions.
1 change: 0 additions & 1 deletion container/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
;;
*)
Expand Down
6 changes: 6 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
2 changes: 1 addition & 1 deletion src/karapace/auth/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 21 additions & 9 deletions src/schema_registry/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,28 @@
"""
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
from schema_registry.http_handlers import setup_exception_handlers
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
Expand All @@ -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__)
Expand All @@ -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()
)
3 changes: 3 additions & 0 deletions src/schema_registry/base_config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
karapace:
env_file: ${KARAPACE_DOTENV}
env_file_encoding: utf-8
14 changes: 14 additions & 0 deletions src/schema_registry/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/schema_registry/routers/compatibility_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/schema_registry/routers/config_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/schema_registry/routers/health_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
2 changes: 1 addition & 1 deletion src/schema_registry/routers/mode_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/schema_registry/routers/schemas_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
6 changes: 3 additions & 3 deletions src/schema_registry/routers/subjects_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d9d471d

Please sign in to comment.