Added feature-flags #48

This commit is contained in:
Sven Heidemann 2022-10-02 02:54:08 +02:00
parent ac238b360a
commit 890cb6f6b5
9 changed files with 49 additions and 24 deletions

View File

@ -17,7 +17,7 @@
"de" "de"
] ]
}, },
"FeatureFlag": { "FeatureFlags": {
"AdminModule": true, "AdminModule": true,
"BaseModule": true, "BaseModule": true,
"BootLogModule": true, "BootLogModule": true,

View File

@ -19,7 +19,7 @@
"Token": "OTk4MTYwNDI3Njg5MTgxMjM3.GI7h67.BqD6Lu1Tz0MuG8iktYrcLnHi1pNozyMiWFGTKI", "Token": "OTk4MTYwNDI3Njg5MTgxMjM3.GI7h67.BqD6Lu1Tz0MuG8iktYrcLnHi1pNozyMiWFGTKI",
"Prefix": "!ke " "Prefix": "!ke "
}, },
"FeatureFlag": { "FeatureFlags": {
"AdminModule": true, "AdminModule": true,
"BaseModule": true, "BaseModule": true,
"BootLogModule": true, "BootLogModule": true,

View File

@ -28,7 +28,7 @@
"Buffered": "true", "Buffered": "true",
"AuthPlugin": "mysql_native_password" "AuthPlugin": "mysql_native_password"
}, },
"FeatureFlag": { "FeatureFlags": {
"AdminModule": true, "AdminModule": true,
"BaseModule": true, "BaseModule": true,
"BootLogModule": true, "BootLogModule": true,

View File

@ -17,7 +17,7 @@
"de" "de"
] ]
}, },
"FeatureFlag": { "FeatureFlags": {
"AdminModule": true, "AdminModule": true,
"BaseModule": true, "BaseModule": true,
"BootLogModule": true, "BootLogModule": true,

View File

@ -8,6 +8,7 @@ from bot.application import Application
from bot.startup import Startup from bot.startup import Startup
from bot.startup_discord_extension import StartupDiscordExtension from bot.startup_discord_extension import StartupDiscordExtension
from bot.startup_migration_extension import StartupMigrationExtension from bot.startup_migration_extension import StartupMigrationExtension
from bot.startup_settings_extension import StartupSettingsExtension
from modules.boot_log.boot_log_extension import BootLogExtension from modules.boot_log.boot_log_extension import BootLogExtension
from modules.database.database_extension import DatabaseExtension from modules.database.database_extension import DatabaseExtension
@ -18,12 +19,13 @@ class Program:
self.app: Optional[Application] = None self.app: Optional[Application] = None
async def start(self): async def start(self):
app_builder = ApplicationBuilder(Application) app_builder = ApplicationBuilder(Application) \
app_builder.use_extension(StartupDiscordExtension) .use_extension(StartupSettingsExtension) \
app_builder.use_extension(StartupMigrationExtension) .use_extension(StartupDiscordExtension) \
app_builder.use_extension(BootLogExtension) .use_extension(StartupMigrationExtension) \
app_builder.use_extension(DatabaseExtension) .use_extension(BootLogExtension) \
app_builder.use_startup(Startup) .use_extension(DatabaseExtension) \
.use_startup(Startup)
self.app: Application = await app_builder.build_async() self.app: Application = await app_builder.build_async()
await self.app.run_async() await self.app.run_async()

View File

@ -1,5 +1,3 @@
import os
from datetime import datetime
from typing import Optional from typing import Optional
from cpl_core.application import StartupABC from cpl_core.application import StartupABC
@ -37,22 +35,10 @@ class Startup(StartupABC):
def __init__(self): def __init__(self):
StartupABC.__init__(self) StartupABC.__init__(self)
self._start_time = datetime.now()
self._config: Optional[ConfigurationABC] = None self._config: Optional[ConfigurationABC] = None
self._feature_flags: Optional[FeatureFlagsSettings] = None self._feature_flags: Optional[FeatureFlagsSettings] = None
def configure_configuration(self, configuration: ConfigurationABC, environment: ApplicationEnvironment) -> ConfigurationABC: def configure_configuration(self, configuration: ConfigurationABC, environment: ApplicationEnvironment) -> ConfigurationABC:
environment.set_working_directory(os.path.dirname(os.path.realpath(__file__)))
configuration.add_environment_variables('KDB_')
configuration.add_environment_variables('DISCORD_')
configuration.add_json_file(f'config/appsettings.json', optional=False)
configuration.add_json_file(f'config/appsettings.{environment.environment_name}.json', optional=True)
configuration.add_json_file(f'config/appsettings.{environment.host_name}.json', optional=True)
configuration.add_configuration('Startup_StartTime', str(self._start_time))
self._config = configuration self._config = configuration
self._feature_flags = configuration.get_configuration(FeatureFlagsSettings) self._feature_flags = configuration.get_configuration(FeatureFlagsSettings)
return configuration return configuration

View File

@ -0,0 +1,28 @@
import os
from datetime import datetime
from cpl_core.application import StartupExtensionABC
from cpl_core.configuration import ConfigurationABC
from cpl_core.dependency_injection import ServiceCollectionABC
from cpl_core.environment import ApplicationEnvironmentABC
class StartupSettingsExtension(StartupExtensionABC):
def __init__(self):
self._start_time = datetime.now()
def configure_configuration(self, configuration: ConfigurationABC, environment: ApplicationEnvironmentABC):
# this shit has to be done here because we need settings in subsequent startup extensions
environment.set_working_directory(os.path.dirname(os.path.realpath(__file__)))
configuration.add_environment_variables('KDB_')
configuration.add_environment_variables('DISCORD_')
configuration.add_json_file(f'config/appsettings.json', optional=False)
configuration.add_json_file(f'config/appsettings.{environment.environment_name}.json', optional=True)
configuration.add_json_file(f'config/appsettings.{environment.host_name}.json', optional=True)
configuration.add_configuration('Startup_StartTime', str(self._start_time))
def configure_services(self, services: ServiceCollectionABC, env: ApplicationEnvironmentABC):
pass

View File

@ -5,6 +5,8 @@ from cpl_core.configuration import ConfigurationABC
from cpl_core.dependency_injection import ServiceProviderABC from cpl_core.dependency_injection import ServiceProviderABC
from cpl_core.logging import LoggerABC from cpl_core.logging import LoggerABC
from bot_core.configuration.feature_flags_settings import FeatureFlagsSettings
class BootLogExtension(ApplicationExtensionABC): class BootLogExtension(ApplicationExtensionABC):
@ -12,6 +14,9 @@ class BootLogExtension(ApplicationExtensionABC):
pass pass
async def run(self, config: ConfigurationABC, services: ServiceProviderABC): async def run(self, config: ConfigurationABC, services: ServiceProviderABC):
feature_flags: FeatureFlagsSettings = config.get_configuration(FeatureFlagsSettings)
if not feature_flags.boot_log_module:
return
logger: LoggerABC = services.get_service(LoggerABC) logger: LoggerABC = services.get_service(LoggerABC)
logger.debug(__name__, 'BootLog extension started') logger.debug(__name__, 'BootLog extension started')
config.add_configuration('Bot_StartTime', str(datetime.now())) config.add_configuration('Bot_StartTime', str(datetime.now()))

View File

@ -5,6 +5,7 @@ from cpl_core.configuration import ConfigurationABC
from cpl_core.dependency_injection import ServiceProviderABC from cpl_core.dependency_injection import ServiceProviderABC
from cpl_core.logging import LoggerABC from cpl_core.logging import LoggerABC
from bot_core.configuration.feature_flags_settings import FeatureFlagsSettings
from bot_data.service.migration_service import MigrationService from bot_data.service.migration_service import MigrationService
@ -14,6 +15,9 @@ class DatabaseExtension(ApplicationExtensionABC):
pass pass
async def run(self, config: ConfigurationABC, services: ServiceProviderABC): async def run(self, config: ConfigurationABC, services: ServiceProviderABC):
feature_flags: FeatureFlagsSettings = config.get_configuration(FeatureFlagsSettings)
if not feature_flags.database_module:
return
logger: LoggerABC = services.get_service(LoggerABC) logger: LoggerABC = services.get_service(LoggerABC)
logger.debug(__name__, 'Database extension started') logger.debug(__name__, 'Database extension started')
config.add_configuration('Database_StartTime', str(datetime.now())) config.add_configuration('Database_StartTime', str(datetime.now()))