Refactor: Modularize configuration and implement robust DB bootstrap

This commit is contained in:
Scott Idem
2026-01-15 16:59:18 -05:00
parent 16c79aca39
commit 3f276a42e1
3 changed files with 136 additions and 33 deletions

View File

@@ -21,7 +21,8 @@ from app.middleware import add_process_time_header as process_time_middleware
# Centralized router registry
from app.routers.registry import setup_routers
from app.db_sql import sql_select, reset_redis # , sql_connect
from app.db_sql import sql_select, reset_redis, reconnect_db
from app.lib_config_v3 import bootstrap_db_config, validate_critical_config
print('### **** *** ** * The Aether API v4 using FastAPI is loading... * ** *** **** ###')
@@ -47,41 +48,26 @@ app = FastAPI(
log.setLevel(logging.INFO)
# log.debug(config.settings)
print('### **** *** ** * Aether API v4 using FastAPI - About to try first SQL SELECT (sql_select()) while loading... * ** *** **** ###')
if aether_cfg_sql_result := sql_select(
table_name = 'cfg',
record_id = config.settings.AETHER_CFG['id'],
as_list = False,
max_count = 1,
):
aether_cfg_sql = aether_cfg_sql_result
print('### **** *** ** * Aether API v4 using FastAPI - Bootstrapping Configuration... * ** *** **** ###')
config.settings.DB['server'] = aether_cfg_sql.get('db_server')
config.settings.DB['port'] = aether_cfg_sql.get('db_port')
config.settings.DB['name'] = aether_cfg_sql.get('db_name')
config.settings.DB['username'] = aether_cfg_sql.get('db_username')
config.settings.DB['password'] = aether_cfg_sql.get('db_password')
# Sync settings from DB with robust error handling
try:
if bootstrap_db_config(config.settings):
log.info("Successfully bootstrapped configuration from database.")
# Re-initialize the database engine with new credentials/URI
if reconnect_db():
log.info("Database connection re-established with production configuration.")
else:
log.warning("FAILED to re-establish database connection after bootstrap. Falling back to .env settings.")
else:
log.warning("System bootstrap from DB returned no results. Using environment defaults.")
except Exception as e:
log.error(f"Unexpected error during configuration bootstrap: {e}. Falling back to .env settings.")
DB = config.settings.DB
config.settings.SQLALCHEMY_DB_URI = 'mysql://'+DB['username']+':'+DB['password']+'@'+DB['server']+'/'+DB['name']
# db_result = sql_connect(config.settings.SQLALCHEMY_DB_URI)
log.debug(config.settings.DB)
# Perform final validation of critical infrastructure
validate_critical_config(config.settings)
config.settings.SMTP['server'] = aether_cfg_sql.get('smtp_server')
config.settings.SMTP['port'] = aether_cfg_sql.get('smtp_port')
config.settings.SMTP['username'] = aether_cfg_sql.get('smtp_username')
config.settings.SMTP['password'] = aether_cfg_sql.get('smtp_password')
# config.settings.FILES_PATH['hosted_files_root'] = aether_cfg_sql.get('PATH_HOSTED_FILES_ROOT')
# config.settings.FILES_PATH['hosted_tmp_root'] = aether_cfg_sql.get('PATH_HOSTED_TMP_ROOT')
config.settings.FILES_PATH['hosted_files_root'] = aether_cfg_sql.get('path_hosted_files_root')
config.settings.FILES_PATH['hosted_tmp_root'] = aether_cfg_sql.get('path_hosted_tmp_root')
else:
# aether_cfg_sql_result
pass
print('### **** *** ** * Aether API v4 using FastAPI - Finished first SQL SELECT (sql_select()) while loading... * ** *** **** ###' )
log.debug(aether_cfg_sql_result)
print('### **** *** ** * Aether API v4 using FastAPI - Finished Configuration Phase * ** *** **** ###' )
log.debug(config.settings)
# @lru_cache()