diff --git a/microservice/language/python/init/_utils.py b/microservice/language/python/init/_utils.py index 8f5fca7..72c9f29 100644 --- a/microservice/language/python/init/_utils.py +++ b/microservice/language/python/init/_utils.py @@ -44,9 +44,9 @@ def build_docker_compose(path: Path, microservice_name: str) -> str: if "x-microservice-environment" not in data: data["x-microservice-environment"] = { - "MINOS_BROKER_QUEUE_HOST": "postgres", - "MINOS_BROKER_HOST": "kafka", - "MINOS_REPOSITORY_HOST": "postgres", + "MINOS_INTERFACES_BROKER_COMMON_HOST": "kafka", + "MINOS_DATABASES_DEFAULT_HOST": "postgres", + "MINOS_DATABASES_QUERY_HOST": "postgres", "MINOS_SNAPSHOT_HOST": "postgres", "MINOS_DISCOVERY_HOST": "discovery", } diff --git a/microservice/language/python/init/config.yml.jinja b/microservice/language/python/init/config.yml.jinja index 457cfe1..0dffc26 100644 --- a/microservice/language/python/init/config.yml.jinja +++ b/microservice/language/python/init/config.yml.jinja @@ -1,66 +1,73 @@ -service: - name: {{ name }} - aggregate: src.aggregates.{{ aggregate }} - injections: - lock_pool: minos.common.PostgreSqlLockPool - postgresql_pool: minos.common.PostgreSqlPool - broker_publisher: minos.plugins.kafka.InMemoryQueuedKafkaBrokerPublisher - broker_subscriber_builder: minos.plugins.kafka.InMemoryQueuedKafkaBrokerSubscriberBuilder - broker_pool: minos.networks.BrokerClientPool - transaction_repository: minos.aggregate.PostgreSqlTransactionRepository - event_repository: minos.aggregate.PostgreSqlEventRepository - snapshot_repository: minos.aggregate.PostgreSqlSnapshotRepository - {{ name }}_repository: src.{{ aggregate }}QueryServiceRepository - saga_manager: minos.saga.SagaManager - discovery: minos.networks.DiscoveryConnector - services: - - minos.networks.BrokerHandlerService - - minos.networks.RestService - - minos.networks.PeriodicTaskSchedulerService -middleware: - - minos.saga.transactional_command -services: - - minos.aggregate.TransactionService - - minos.aggregate.SnapshotService - - minos.saga.SagaService - - src.queries.{{ aggregate }}QueryService - - src.commands.{{ aggregate }}CommandService -rest: - host: 0.0.0.0 - port: 8080 -broker: - host: localhost - port: 9092 - queue: +version: 2 +name: {{ name }} +injections: + - src.{{ aggregate }}QueryServiceRepository +databases: + default: database: {{ name }}_db user: minos password: min0s host: localhost port: 5432 - records: 1000 - retry: 2 -repository: - database: {{ name }}_db - user: minos - password: min0s - host: localhost - port: 5432 -query_repository: - database: {{ name }}_query_db - user: minos - password: min0s - host: localhost - port: 5432 -snapshot: - database: {{ name }}_db - user: minos - password: min0s - host: localhost - port: 5432 -saga: - storage: + query: + database: {{ name }}_query_db + user: minos + password: min0s + host: localhost + port: 5432 + saga: path: ./{{ name }}.lmdb +interfaces: + broker: + port: minos.networks.BrokerPort + common: + host: localhost + port: 9092 + queue: + records: 1000 + retry: 2 + publisher: + client: minos.plugins.kafka.KafkaBrokerPublisher + queue: minos.networks.PostgreSqlBrokerPublisherQueue + subscriber: + client: minos.plugins.kafka.KafkaBrokerSubscriber + queue: minos.networks.PostgreSqlBrokerSubscriberQueue + validator: minos.networks.PostgreSqlBrokerSubscriberDuplicateValidator + http: + port: minos.networks.HttpPort + connector: + client: minos.plugins.aiohttp.AioHttpConnector + host: 0.0.0.0 + port: 8023 + periodic: + port: minos.networks.PeriodicPort +pools: + lock: minos.common.PostgreSqlLockPool + database: minos.common.PostgreSqlPool + broker: minos.networks.BrokerClientPool discovery: + connector: minos.networks.DiscoveryConnector client: minos.plugins.minos_discovery.MinosDiscoveryClient host: localhost port: 5567 +saga: + manager: minos.saga.SagaManager +aggregate: + entities: + - src.aggregates.{{ aggregate }} + repositories: + transaction: minos.aggregate.PostgreSqlTransactionRepository + event: minos.aggregate.PostgreSqlEventRepository + snapshot: minos.aggregate.PostgreSqlSnapshotRepository +routers: + - minos.networks.BrokerRouter + - minos.networks.PeriodicRouter + - minos.networks.RestHttpRouter +middleware: + - minos.saga.transactional_command +services: + - minos.aggregate.TransactionService + - minos.aggregate.SnapshotService + - minos.saga.SagaService + - src.queries.{{ aggregate }}QueryService + - src.commands.{{ aggregate }}CommandService diff --git a/microservice/language/python/init/src/queries/repository.py.jinja b/microservice/language/python/init/src/queries/repository.py.jinja index e0ca30a..ed3cd57 100644 --- a/microservice/language/python/init/src/queries/repository.py.jinja +++ b/microservice/language/python/init/src/queries/repository.py.jinja @@ -1,18 +1,29 @@ -from minos.common import MinosSetup, MinosConfig -from sqlalchemy import create_engine -from sqlalchemy.orm import sessionmaker -from src.queries.models import Base +from minos.common import ( + Config, + Injectable, + SetupMixin, +) +from sqlalchemy import ( + create_engine, +) +from sqlalchemy.orm import ( + sessionmaker, +) +from src.queries.models import ( + Base, +) -class {{ aggregate }}QueryServiceRepository(MinosSetup): +@Injectable("{{ aggregate.lower() }}_repository") +class {{ aggregate }}QueryServiceRepository(SetupMixin): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) - self.engine = create_engine("postgresql+psycopg2://minos:min0s@postgres:5432/{{ name }}_query_db".format(**kwargs)) + self.engine = create_engine("postgresql+psycopg2://{user}:{password}@{host}:{port}/{{ name }}_query_db".format(**kwargs)) self.session = sessionmaker(bind=self.engine)() async def _setup(self) -> None: Base.metadata.create_all(self.engine) @classmethod - def _from_config(cls, *args, config: MinosConfig, **kwargs): - return cls(*args, **(config.query_repository._asdict()) | kwargs) + def _from_config(cls, *args, config: Config, **kwargs): + return cls(*args, **(config.get_database_by_name("query")) | kwargs) diff --git a/microservice/language/python/init/src/queries/services.py.jinja b/microservice/language/python/init/src/queries/services.py.jinja index 9a15faa..48042fc 100644 --- a/microservice/language/python/init/src/queries/services.py.jinja +++ b/microservice/language/python/init/src/queries/services.py.jinja @@ -9,6 +9,9 @@ from src.queries.repository import ( from minos.aggregate import ( Event, ) +from minos.common import ( + Inject, +) from minos.cqrs import ( QueryService, ) @@ -23,7 +26,10 @@ from minos.networks import ( class {{ aggregate }}QueryService(QueryService): """{{ aggregate }}QueryService class.""" - repository: {{ aggregate }}QueryServiceRepository = Provide["{{ name }}_repository"] + @Inject() + def __init__(self, repository: {{ aggregate }}QueryServiceRepository, **kwargs): + super().__init__(**kwargs) + self.repository = repository @enroute.rest.query("/{{ aggregate.lower() }}s", "GET") async def get_{{ aggregate.lower() }}(self, request: Request) -> Response: diff --git a/microservice/language/python/init/tests/test_aggregates.py.jinja b/microservice/language/python/init/tests/test_aggregates.py.jinja index da815ab..15d11cc 100644 --- a/microservice/language/python/init/tests/test_aggregates.py.jinja +++ b/microservice/language/python/init/tests/test_aggregates.py.jinja @@ -16,10 +16,10 @@ class Test{{aggregate}}(unittest.IsolatedAsyncioTestCase): self.injector = build_dependency_injector() async def asyncSetUp(self) -> None: - await self.injector.wire(modules=[sys.modules[__name__]]) + await self.injector.wire_and_destroy_injections() async def asyncTearDown(self) -> None: - await self.injector.unwire() + await self.injector.unwire_and_destroy_injections() def test_constructor(self): obj = {{ aggregate }}() diff --git a/microservice/language/python/init/tests/test_commands/test_services.py.jinja b/microservice/language/python/init/tests/test_commands/test_services.py.jinja index 45358a5..0a59b48 100644 --- a/microservice/language/python/init/tests/test_commands/test_services.py.jinja +++ b/microservice/language/python/init/tests/test_commands/test_services.py.jinja @@ -21,10 +21,10 @@ class Test{{aggregate}}CommandService(unittest.IsolatedAsyncioTestCase): self.injector = build_dependency_injector() async def asyncSetUp(self) -> None: - await self.injector.wire(modules=[sys.modules[__name__]]) + await self.injector.wire_and_setup_injections() async def asyncTearDown(self) -> None: - await self.injector.unwire() + await self.injector.unwire_and_setup_injections() def test_constructor(self): service = {{ aggregate }}CommandService() diff --git a/microservice/language/python/init/tests/test_queries/test_services.py.jinja b/microservice/language/python/init/tests/test_queries/test_services.py.jinja index da80c1e..f7de868 100644 --- a/microservice/language/python/init/tests/test_queries/test_services.py.jinja +++ b/microservice/language/python/init/tests/test_queries/test_services.py.jinja @@ -16,10 +16,10 @@ class Test{{aggregate}}QueryService(unittest.IsolatedAsyncioTestCase): self.injector = build_dependency_injector() async def asyncSetUp(self) -> None: - await self.injector.wire(modules=[sys.modules[__name__]]) + await self.injector.wire_and_setup_injections() async def asyncTearDown(self) -> None: - await self.injector.unwire() + await self.injector.unwire_and_destroy_injections() def test_constructor(self): service = {{ aggregate }}QueryService() diff --git a/microservice/language/python/init/tests/utils.py b/microservice/language/python/init/tests/utils.py index e173c87..6013cf9 100644 --- a/microservice/language/python/init/tests/utils.py +++ b/microservice/language/python/init/tests/utils.py @@ -15,26 +15,28 @@ InMemoryTransactionRepository, ) from minos.common import ( + Config, DependencyInjector, + Injectable, Lock, - MinosConfig, - MinosPool, - MinosSetup, + LockPool, + SetupMixin, +) +from minos.networks import ( + InMemoryBrokerPublisher, ) from minos.saga import ( SagaContext, SagaStatus, ) - -class _FakeBroker(MinosSetup): - """For testing purposes.""" - - async def send(self, *args, **kwargs) -> None: - """For testing purposes.""" +from src import ( + {{ aggregate }}QueryServiceRepository, +) -class _FakeSagaManager(MinosSetup): +@Injectable("saga_manager") +class _FakeSagaManager(SetupMixin): """For testing purposes.""" async def run(self, *args, **kwargs) -> UUID: @@ -59,7 +61,7 @@ async def __aexit__(self, exc_type, exc_val, exc_tb): return -class FakeLockPool(MinosPool): +class FakeLockPool(LockPool): """For testing purposes.""" async def _create_instance(self): @@ -74,19 +76,22 @@ def build_dependency_injector() -> DependencyInjector: return DependencyInjector( build_config(), - saga_manager=_FakeSagaManager, - broker_publisher=_FakeBroker, - lock_pool=FakeLockPool, - transaction_repository=InMemoryTransactionRepository, - event_repository=InMemoryEventRepository, - snapshot_repository=InMemorySnapshotRepository, + [ + _FakeSagaManager, + InMemoryBrokerPublisher, + FakeLockPool, + InMemoryTransactionRepository, + InMemoryEventRepository, + InMemorySnapshotRepository, + FooQueryServiceRepository, + ], ) -def build_config() -> MinosConfig: +def build_config() -> Config: """For testing purposes""" - return MinosConfig(DEFAULT_CONFIG_FILE_PATH) + return Config(DEFAULT_CONFIG_FILE_PATH) DEFAULT_CONFIG_FILE_PATH = Path(__file__).parents[1] / "config.yml" diff --git a/microservice/language/python/package-manager/poetry/pyproject.toml.jinja b/microservice/language/python/package-manager/poetry/pyproject.toml.jinja index 00eda87..a02e579 100644 --- a/microservice/language/python/package-manager/poetry/pyproject.toml.jinja +++ b/microservice/language/python/package-manager/poetry/pyproject.toml.jinja @@ -7,13 +7,14 @@ packages = [{ include = "src" }] [tool.poetry.dependencies] python = "^3.9" -minos-microservice-common = "^0.5.0" -minos-microservice-networks = "^0.5.0" -minos-microservice-aggregate = "^0.5.0" -minos-microservice-saga = "^0.5.0" -minos-microservice-cqrs = "^0.5.0" -minos-broker-kafka = "^0.5.0" -minos-discovery-minos = "^0.5.0" +minos-microservice-common = "^0.6" +minos-microservice-networks = "^0.6" +minos-microservice-aggregate = "^0.6" +minos-microservice-saga = "^0.6" +minos-microservice-cqrs = "^0.6" +minos-broker-kafka = "^0.6" +minos-discovery-minos = "^0.6" +minos-http-aiohttp = "^0.6" typer = "^0.3.2" SQLAlchemy = "^1.4.0"