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 from cpl_core.dependency_injection import ServiceProviderABC from cpl_core.environment import ApplicationEnvironment from cpl_core.logging import LoggerABC from bot_api.logging.api_logger import ApiLogger from bot_core.abc.custom_file_logger_abc import CustomFileLoggerABC from bot_core.configuration.feature_flags_enum import FeatureFlagsEnum from bot_core.configuration.feature_flags_settings import FeatureFlagsSettings from bot_core.logging.command_logger import CommandLogger from bot_core.logging.database_logger import DatabaseLogger from bot_core.logging.message_logger import MessageLogger from bot_data.db_context import DBContext class Startup(StartupABC): def __init__(self): StartupABC.__init__(self) self._start_time = datetime.now() self._config: Optional[ConfigurationABC] = None self._feature_flags: Optional[FeatureFlagsSettings] = None def configure_configuration( self, configuration: ConfigurationABC, environment: ApplicationEnvironment ) -> ConfigurationABC: self._config = configuration self._feature_flags = configuration.get_configuration(FeatureFlagsSettings) return configuration def configure_services( self, services: ServiceCollectionABC, environment: ApplicationEnvironment ) -> ServiceProviderABC: services.add_logging() if self._feature_flags.get_flag(FeatureFlagsEnum.core_module): # custom logging services.add_singleton(CustomFileLoggerABC, CommandLogger) services.add_singleton(CustomFileLoggerABC, DatabaseLogger) services.add_singleton(CustomFileLoggerABC, MessageLogger) if self._feature_flags.get_flag(FeatureFlagsEnum.api_module): services.add_singleton(CustomFileLoggerABC, ApiLogger) services.add_translation() services.add_db_context( DBContext, self._config.get_configuration(DatabaseSettings) ) provider = services.build_service_provider() # instantiate custom logger for c in CustomFileLoggerABC.__subclasses__(): i: LoggerABC = provider.get_service(c) logger: LoggerABC = provider.get_service(LoggerABC) for flag in [f for f in FeatureFlagsEnum]: logger.debug( __name__, f"Loaded feature-flag: {flag} = {self._feature_flags.get_flag(flag)}", ) return provider