sh_discord_bot/kdb-bot/src/bot/startup.py

66 lines
2.6 KiB
Python
Raw Normal View History

2022-10-02 12:14:44 +02:00
from datetime import datetime
2022-07-16 19:47:04 +02:00
from typing import Optional
2022-07-14 16:32:46 +02:00
from cpl_core.application import StartupABC
from cpl_core.configuration import ConfigurationABC
from cpl_core.database import DatabaseSettings
2022-10-03 13:39:09 +02:00
from cpl_core.dependency_injection import ServiceCollectionABC
from cpl_core.dependency_injection import ServiceProviderABC
2022-07-14 16:32:46 +02:00
from cpl_core.environment import ApplicationEnvironment
2022-10-03 13:39:09 +02:00
from cpl_core.logging import LoggerABC
2022-10-13 19:57:56 +02:00
from bot_api.logging.api_logger import ApiLogger
2022-10-03 13:39:09 +02:00
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
2022-07-14 16:32:46 +02:00
class Startup(StartupABC):
def __init__(self):
StartupABC.__init__(self)
2022-10-02 12:14:44 +02:00
self._start_time = datetime.now()
2022-07-16 19:47:04 +02:00
self._config: Optional[ConfigurationABC] = None
2022-10-03 13:39:09 +02:00
self._feature_flags: Optional[FeatureFlagsSettings] = None
2022-07-14 16:32:46 +02:00
2023-01-13 23:35:42 +01:00
def configure_configuration(
self, configuration: ConfigurationABC, environment: ApplicationEnvironment
) -> ConfigurationABC:
2022-07-16 19:47:04 +02:00
self._config = configuration
2022-10-03 13:39:09 +02:00
self._feature_flags = configuration.get_configuration(FeatureFlagsSettings)
2022-07-14 16:32:46 +02:00
return configuration
2023-01-13 23:35:42 +01:00
def configure_services(
self, services: ServiceCollectionABC, environment: ApplicationEnvironment
) -> ServiceProviderABC:
2022-07-16 19:47:04 +02:00
services.add_logging()
2022-10-03 13:39:09 +02:00
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)
2022-10-02 12:14:44 +02:00
2022-10-13 19:57:56 +02:00
if self._feature_flags.get_flag(FeatureFlagsEnum.api_module):
services.add_singleton(CustomFileLoggerABC, ApiLogger)
2022-07-16 19:47:04 +02:00
services.add_translation()
2023-01-13 23:54:06 +01:00
services.add_db_context(DBContext, self._config.get_configuration(DatabaseSettings))
2022-10-03 13:39:09 +02:00
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]:
2023-01-13 23:35:42 +01:00
logger.debug(
__name__,
f"Loaded feature-flag: {flag} = {self._feature_flags.get_flag(flag)}",
)
2022-10-03 13:39:09 +02:00
return provider