Skip to content

Commit

Permalink
Add different booters.
Browse files Browse the repository at this point in the history
  • Loading branch information
n1nj4t4nuk1 committed Dec 9, 2021
1 parent 9fc9d68 commit bc1a01c
Show file tree
Hide file tree
Showing 13 changed files with 188 additions and 5 deletions.
26 changes: 23 additions & 3 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
from src.apps.backoffice.boot import boot
import argparse

from src.apps.backoffice.boot import boot as boot_backoffice
from src.apps.photostore.boot import boot as boot_photostore


service_mapping = {
'backoffice': boot_backoffice,
'photostore': boot_photostore,
}

if __name__ == "__main__":
print("Booting backoffice server!")
boot()
parser = argparse.ArgumentParser()
parser.add_argument(
'--service',
type=str,
nargs='?',
help='Service to run must be one of ["backoffice", "photostore"]',
)
params = vars(parser.parse_args())
service_name = params['service']
service_booter = service_mapping[service_name]

print(f'Booting {service_name} server')
service_booter()
print(f'{service_name} server start success')
2 changes: 1 addition & 1 deletion src/apps/backoffice/BackofficeApp.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def __init__(self):
self.__app: FastAPI = FastAPI()
router: APIRouter = APIRouter()
register_routes(router)
self.__app.include_router(router, prefix='/api/backoffice')
self.__app.include_router(router, prefix='/api')

def get_runnable(self):
return self.__app
6 changes: 5 additions & 1 deletion src/apps/backoffice/BackofficeServer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import uvicorn

from src.apps.backoffice.BackofficeApp import BackofficeApp
from src.contexts.shared.Infrastructure.environment.EnvManager import EnvManager
from src.contexts.shared.Infrastructure.environment.EnvVar import EnvVar


class BackofficeServer:
Expand All @@ -9,4 +11,6 @@ def __init__(self):
self.app = BackofficeApp()

def run(self):
uvicorn.run(self.app.get_runnable(), host="0.0.0.0", port=8000)
host = EnvManager.get(EnvVar.BACKOFFICE_SERVER_HOST)
port = EnvManager.get(EnvVar.BACKOFFICE_SERVER_PORT, parser=int)
uvicorn.run(self.app.get_runnable(), host=host, port=port)
16 changes: 16 additions & 0 deletions src/apps/photostore/PhotoStoreServer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import uvicorn

from src.apps.backoffice.BackofficeApp import BackofficeApp
from src.contexts.shared.Infrastructure.environment.EnvManager import EnvManager
from src.contexts.shared.Infrastructure.environment.EnvVar import EnvVar


class PhotoStoreServer:

def __init__(self):
self.app = BackofficeApp()

def run(self):
host = EnvManager.get(EnvVar.PHOTOSTORE_SERVER_HOST)
port = EnvManager.get(EnvVar.PHOTOSTORE_SERVER_PORT, parser=int)
uvicorn.run(self.app.get_runnable(), host=host, port=port)
15 changes: 15 additions & 0 deletions src/apps/photostore/PtohoStoreApp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from fastapi import FastAPI, APIRouter

from src.apps.photostore.routes import register_routes


class PhotoStoreApp:

def __init__(self):
self.__app: FastAPI = FastAPI()
router: APIRouter = APIRouter()
register_routes(router)
self.__app.include_router(router, prefix='/api')

def get_runnable(self):
return self.__app
6 changes: 6 additions & 0 deletions src/apps/photostore/boot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from src.apps.photostore.PhotoStoreServer import PhotoStoreServer


def boot():
server = PhotoStoreServer()
server.run()
11 changes: 11 additions & 0 deletions src/apps/photostore/controllers/PhotoStoreController.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from abc import ABC, abstractmethod

from fastapi import Request
from fastapi.responses import JSONResponse


class PhotoStoreController(ABC):

@abstractmethod
async def run(self, req: Request) -> JSONResponse:
raise NotImplementedError()
14 changes: 14 additions & 0 deletions src/apps/photostore/controllers/StatusGetController.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from starlette.requests import Request
from starlette.responses import JSONResponse
from http import HTTPStatus

from src.apps.photostore.controllers.PhotoStoreController import PhotoStoreController


class StatusGetController(PhotoStoreController):

def __init__(self):
pass

async def run(self, req: Request) -> JSONResponse:
return JSONResponse(status_code=HTTPStatus.OK)
35 changes: 35 additions & 0 deletions src/apps/photostore/dependencies/PhotoStoreContainer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from dependency_injector import containers, providers

from src.apps.backoffice.controllers.StatusGetController import StatusGetController
from src.contexts.backoffice.users.application.createone.CreateUserCommandHandler import CreateUserCommandHandler
from src.contexts.backoffice.users.application.createone.UserCreator import UserCreator
from src.contexts.photostore.photo.infrastructure.persistence.MinioPhotoStorePhotoRepository import MinioPhotoRepository
from src.contexts.photostore.photo.infrastructure.persistence.config.MinioPhotoConfigFactory import \
MinioPhotoConfigFactory
from src.contexts.shared.Infrastructure.eventbus.InMemoryEventBus import InMemoryEventBus
from src.contexts.shared.Infrastructure.persistence.minio.MinioClientFactory import MinioClientFactory


class PhotoStoreContainer(containers.DeclarativeContainer):

event_bus = providers.Singleton(
InMemoryEventBus,
)

db_config = providers.Singleton(MinioPhotoConfigFactory.create)
db_client = providers.Singleton(MinioClientFactory.create_instance, 'photostore', db_config)

user_repository = providers.Singleton(MinioPhotoRepository, db_client)

user_creator = providers.Singleton(UserCreator, user_repository, event_bus)
create_user_command_handler = providers.Singleton(
CreateUserCommandHandler,
user_creator,
)

status_get_controller = providers.Singleton(StatusGetController)


backoffice_container: PhotoStoreContainer = PhotoStoreContainer()


7 changes: 7 additions & 0 deletions src/apps/photostore/routes/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from fastapi import APIRouter

from src.apps.photostore.routes.status_routes import register as register_status_routes


def register_routes(router: APIRouter):
register_status_routes(router)
18 changes: 18 additions & 0 deletions src/apps/photostore/routes/status_routes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import sys

from dependency_injector.wiring import inject, Provide
from fastapi import APIRouter

from src.apps.photostore.controllers.StatusGetController import StatusGetController
from src.apps.backoffice.dependencies.BackofficeContainer import BackofficeContainer, backoffice_container


@inject
def register(
router: APIRouter,
status_get_controller: StatusGetController = Provide[BackofficeContainer.status_get_controller]
):
router.add_route('/status', status_get_controller.run)


backoffice_container.wire(modules=[sys.modules[__name__]])
6 changes: 6 additions & 0 deletions src/contexts/shared/Infrastructure/environment/EnvVar.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,19 @@ class EnvVar(Enum):
# -------------------- BACKOFFICE -----------------------
# -------------------------------------------------------

BACKOFFICE_SERVER_HOST = 'backoffice.server.host'
BACKOFFICE_SERVER_PORT = 'backoffice.server.port'

BACKOFFICE_USER_MONGO_HOST = 'backoffice.user.mongo_host'
BACKOFFICE_USER_MONGO_PORT = 'backoffice.user.mongo_port'

# -------------------------------------------------------
# -------------------- PHOTO STORE ----------------------
# -------------------------------------------------------

PHOTOSTORE_SERVER_HOST = 'photostore.server.host'
PHOTOSTORE_SERVER_PORT = 'photostore.server.port'

PHOTOSTORE_PHOTO_MINIO_HOST = 'photostore.photo.minio_host'
PHOTOSTORE_PHOTO_MINIO_PORT = 'photostore.photo.minio_port'
PHOTOSTORE_PHOTO_MINIO_ACCESS_KEY = 'photostore.photo.minio_access_key'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from typing import Dict, Optional

from minio import Minio

from src.contexts.shared.Infrastructure.persistence.minio.MinioConfiguration import MinioConfiguration
from src.contexts.shared.Infrastructure.persistence.mongo.PyMongoConfiguration import PyMongoConfiguration


class MinioClientFactory:

__clients: Dict[str, Minio] = {}

@staticmethod
def __get_client(context_name: str):
return MinioClientFactory.__clients.get(context_name)

@staticmethod
def __add_client(context_name: str, client: Minio):
MinioClientFactory.__clients[context_name] = client

@staticmethod
def create_instance(context_name: str, config: Optional[PyMongoConfiguration] = None):
client = MinioClientFactory.__get_client(context_name)
if client is not None:
return client

if config is None:
config = MinioConfiguration()
client = config.create_client_from_config()
MinioClientFactory.__add_client(context_name, client)
return client

0 comments on commit bc1a01c

Please sign in to comment.