Skip to content

Commit

Permalink
Merge pull request #56 from minos-framework/0.2.0
Browse files Browse the repository at this point in the history
v0.2.0
  • Loading branch information
andrea-mucci authored Apr 1, 2022
2 parents 62c8395 + 2193e62 commit 1552a85
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 100 deletions.
6 changes: 3 additions & 3 deletions microservice/language/python/init/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}
Expand Down
119 changes: 63 additions & 56 deletions microservice/language/python/init/config.yml.jinja
Original file line number Diff line number Diff line change
@@ -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
27 changes: 19 additions & 8 deletions microservice/language/python/init/src/queries/repository.py.jinja
Original file line number Diff line number Diff line change
@@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ from src.queries.repository import (
from minos.aggregate import (
Event,
)
from minos.common import (
Inject,
)
from minos.cqrs import (
QueryService,
)
Expand All @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
43 changes: 24 additions & 19 deletions microservice/language/python/init/tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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):
Expand All @@ -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"
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down

0 comments on commit 1552a85

Please sign in to comment.