Commit 0c60fbfc by Oleksandr Barabash

database init added on startup. Small fixes, app_factory added

parent b369a553
......@@ -20,7 +20,7 @@ from marshmallow import EXCLUDE, ValidationError
from bots import TeamsMessagingExtensionsActionPreviewBot
from bots.exceptions import ConversationNotFound, DataParsingError
from config import AppConfig, COSMOS_CLIENT, TeamsAppConfig, TOKEN_HELPER, \
LOG_FILE
CosmosDBConfig
from entities.json.admin_user import AdminUser
from entities.json.notification import Notification
from utils.cosmos_client import ItemNotFound
......@@ -212,24 +212,55 @@ async def v1_auth(request: Request) -> Response:
return Response(status=HTTPStatus.FORBIDDEN)
APP = web.Application(middlewares=[error_middleware])
APP.router.add_post("/api/v1/messages", v1_messages)
APP.router.add_post("/api/v1/notification", v1_notification)
APP.router.add_get("/api/v1/notification/{notification_id}",
v1_get_notification)
APP.router.add_get("/api/v1/initiations/{notification_id}", v1_get_initiations)
APP.router.add_get("/api/v1/health-check", v1_health_check)
APP.router.add_get("/{}".format(TeamsAppConfig.zip_name), get_app_zip)
APP.router.add_post("/api/v1/auth", v1_auth)
async def init_db_containers():
""" To speed up the process we have to create containers first """
COSMOS_CLIENT.create_container(
CosmosDBConfig.Conversations.DATABASE,
CosmosDBConfig.Conversations.CONTAINER,
CosmosDBConfig.Conversations.PARTITION_KEY
)
COSMOS_CLIENT.create_container(
CosmosDBConfig.Notifications.DATABASE,
CosmosDBConfig.Notifications.CONTAINER,
CosmosDBConfig.Notifications.PARTITION_KEY
)
COSMOS_CLIENT.create_container(
CosmosDBConfig.Acknowledges.DATABASE,
CosmosDBConfig.Acknowledges.CONTAINER,
CosmosDBConfig.Acknowledges.PARTITION_KEY
)
COSMOS_CLIENT.create_container(
CosmosDBConfig.Initiations.DATABASE,
CosmosDBConfig.Initiations.CONTAINER,
CosmosDBConfig.Initiations.PARTITION_KEY
)
async def app_factory(bot):
""" Create the app """
await init_db_containers()
app = web.Application(middlewares=[error_middleware])
app.router.add_post("/api/v1/messages", v1_messages)
app.router.add_post("/api/v1/notification", v1_notification)
app.router.add_get("/api/v1/notification/{notification_id}",
v1_get_notification)
app.router.add_get("/api/v1/initiations/{notification_id}",
v1_get_initiations)
app.router.add_get("/api/v1/health-check", v1_health_check)
app.router.add_get("/{}".format(TeamsAppConfig.zip_name), get_app_zip)
app.router.add_post("/api/v1/auth", v1_auth)
bot.add_web_app(app)
bot.add_cosmos_client(COSMOS_CLIENT)
BOT.add_web_app(APP)
BOT.add_cosmos_client(COSMOS_CLIENT)
return app
if __name__ == "__main__":
init_logging()
try:
web.run_app(APP, host="0.0.0.0", port=app_config.PORT)
web.run_app(app_factory(BOT), host="0.0.0.0", port=app_config.PORT)
except Exception as error:
raise error
......@@ -10,7 +10,6 @@ from utils.token_helper import TokenHelper
PROJECT_ROOT_PATH = os.path.dirname(os.path.abspath("__file__"))
ASSETS_PATH = os.path.join(PROJECT_ROOT_PATH, "assets")
CARDS_PATH = os.path.join(ASSETS_PATH, "cards")
LOG_FILE = os.path.join(PROJECT_ROOT_PATH, "log.txt")
class Auth:
......
......@@ -78,18 +78,29 @@ class CosmosClient:
return await self.execute_blocking(bl)
async def get_container(self, database_id: str, container_id: str,
partition_key: Any, **kwargs) -> ContainerProxy:
""" Get or create container """
async def create_container(self, database_id: str, container_id: str,
partition_key: Any, **kwargs) -> ContainerProxy:
""" Create container """
db = await self.get_db(database_id)
def bl() -> ContainerProxy:
""" Get Notifications container blocking """
try:
return db.get_container_client(container_id)
except exceptions.CosmosResourceNotFoundError:
return db.create_container(container_id, partition_key,
**kwargs)
except exceptions.CosmosResourceNotFoundError:
pass
return await self.execute_blocking(bl)
async def get_container(self, database_id: str, container_id: str,
partition_key: Any, **kwargs) -> ContainerProxy:
""" Get or create container """
db = await self.get_db(database_id)
def bl() -> ContainerProxy:
""" Get Notifications container blocking """
return db.get_container_client(container_id)
return await self.execute_blocking(bl)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment