This repository has been archived on 2022-07-14. You can view files and clone it, but cannot push or open issues or pull requests.
sh_gismo/src/gismo/startup.py
2021-12-21 15:39:46 +01:00

105 lines
5.0 KiB
Python

import os
from datetime import datetime
from typing import Optional
from cpl_core.application import StartupABC
from cpl_core.configuration import ConfigurationABC
from cpl_core.database import DatabaseSettings
from cpl_core.dependency_injection import (ServiceCollectionABC,
ServiceProviderABC)
from cpl_core.environment import ApplicationEnvironment
from cpl_core.logging import LoggerABC
from gismo_core.abc.bot_service_abc import BotServiceABC
from gismo_core.abc.message_service_abc import MessageServiceABC
from gismo_core.service.bot_service import BotService
from gismo_core.service.message_service import MessageService
from gismo_data.abc.known_user_repository_abc import KnownUserRepositoryABC
from gismo_data.abc.migration_abc import MigrationABC
from gismo_data.abc.server_repository_abc import ServerRepositoryABC
from gismo_data.abc.user_joined_server_repository_abc import UserJoinedServerRepositoryABC
from gismo_data.abc.user_joined_voice_channel_abc import UserJoinedVoiceChannelRepositoryABC
from gismo_data.abc.user_repository_abc import UserRepositoryABC
from gismo_data.db_context import DBContext
from gismo_data.migration.initial_migration import InitialMigration
from gismo_data.migration.migration_0_3 import Migration_0_3
from gismo_data.migration.migration_0_3_1 import Migration_0_3_1
from gismo_data.service.client_repository_service import (
ClientRepositoryABC, ClientRepositoryService)
from gismo_data.service.known_user_repository_service import \
KnownUserRepositoryService
from gismo_data.service.migration_service import MigrationService
from gismo_data.service.server_repository_service import \
ServerRepositoryService
from gismo_data.service.user_joined_server_repository_service import UserJoinedServerRepositoryService
from gismo_data.service.user_joined_voice_channel_service import UserJoinedVoiceChannelRepositoryService
from gismo_data.service.user_repository_service import UserRepositoryService
from modules.base.base import Base
from modules.boot_log.boot_log import BootLog
from modules.database.database import Database
from modules_core.abc.module_abc import ModuleABC
from modules_core.abc.module_service_abc import ModuleServiceABC
from modules_core.service.module_service import ModuleService
class Startup(StartupABC):
def __init__(self):
StartupABC.__init__(self)
self._start_time = datetime.now()
self._config: Optional[ConfigurationABC] = None
async def configure_configuration(self, configuration: ConfigurationABC, environment: ApplicationEnvironment) -> ConfigurationABC:
environment.set_working_directory(os.path.dirname(os.path.realpath(__file__)))
configuration.add_environment_variables('GISMO_')
configuration.add_json_file(f'appsettings.json', optional=False)
configuration.add_json_file(f'appsettings.{environment.environment_name}.json', optional=True)
configuration.add_json_file(f'appsettings.{environment.host_name}.json', optional=True)
configuration.add_configuration('Startup_StartTime', self._start_time)
self._config = configuration
return configuration
async def configure_services(self, services: ServiceCollectionABC, environment: ApplicationEnvironment) -> ServiceProviderABC:
services.add_logging()
services.add_db_context(DBContext, self._config.get_configuration(DatabaseSettings))
# modules
services.add_singleton(ModuleServiceABC, ModuleService)
services.add_singleton(BotServiceABC, BotService)
services.add_transient(MessageServiceABC, MessageService)
# services
services.add_transient(MigrationService)
# data services
services.add_transient(ServerRepositoryABC, ServerRepositoryService)
services.add_transient(UserRepositoryABC, UserRepositoryService)
services.add_transient(ClientRepositoryABC, ClientRepositoryService)
services.add_transient(KnownUserRepositoryABC, KnownUserRepositoryService)
services.add_transient(UserJoinedServerRepositoryABC, UserJoinedServerRepositoryService)
services.add_transient(UserJoinedVoiceChannelRepositoryABC, UserJoinedVoiceChannelRepositoryService)
# modules
services.add_transient(ModuleABC, Database)
services.add_transient(ModuleABC, Base)
services.add_transient(ModuleABC, BootLog)
# migrations
services.add_transient(MigrationABC, InitialMigration)
services.add_transient(MigrationABC, Migration_0_3)
services.add_transient(MigrationABC, Migration_0_3_1)
provider: ServiceProviderABC = services.build_service_provider()
startup_init_time = round((datetime.now() - self._config.get_configuration('Startup_StartTime')).total_seconds(), 2)
self._config.add_configuration('Startup_InitTime', startup_init_time)
logger: LoggerABC = provider.get_service(LoggerABC)
logger.debug(__name__, f'Startup Init time: {startup_init_time}s')
return provider