From e9d66323254d3a12f26304cad49cd3f008ae897e Mon Sep 17 00:00:00 2001 From: panyy Date: Fri, 20 Mar 2026 11:10:21 +0800 Subject: [PATCH] =?UTF-8?q?config:=20mindma=5Furl=E7=9A=84=E9=85=8D?= =?UTF-8?q?=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/maxkb/const.py | 55 ++++++++++++++++++++++++++++++++++++++++++--- check_config.py | 6 +++-- 2 files changed, 56 insertions(+), 5 deletions(-) diff --git a/apps/maxkb/const.py b/apps/maxkb/const.py index 6129a1527..cc7c3104d 100644 --- a/apps/maxkb/const.py +++ b/apps/maxkb/const.py @@ -6,15 +6,64 @@ from dotenv import load_dotenv from .conf import ConfigManager -__all__ = ['BASE_DIR', 'PROJECT_DIR', 'VERSION', 'CONFIG'] +__all__ = ['BASE_DIR', 'PROJECT_DIR', 'VERSION', 'CONFIG', 'CONFIG_SOURCE'] BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) LOG_DIR = os.path.join('/', 'opt', 'maxkb', 'logs') PROJECT_DIR = os.path.dirname(BASE_DIR) VERSION = '2.0.0' +DEFAULT_CONFIG_ROOT = os.path.abspath('/opt/maxkb/conf') +CONFIG_FILENAMES = ('config_example.yml', 'config.yaml', 'config.yml') # load environment variables from .env file load_dotenv() -# 直接从 /opt/maxkb/conf 目录加载配置 -CONFIG = ConfigManager.load_user_config(root_path=os.path.abspath('/opt/maxkb/conf')) + +def _has_config_file(root_path): + return any(os.path.isfile(os.path.join(root_path, name)) for name in CONFIG_FILENAMES) + + +def _resolve_config_target(): + config_path = os.environ.get('MAXKB_CONFIG') + if config_path: + config_path = os.path.abspath(config_path) + if os.path.isfile(config_path): + return os.path.dirname(config_path), os.path.basename(config_path), config_path + if os.path.isdir(config_path): + if _has_config_file(config_path): + return config_path, None, config_path + raise ImportError(f'No config file found in MAXKB_CONFIG directory: {config_path}') + raise ImportError(f'MAXKB_CONFIG path does not exist: {config_path}') + + for root_path in (DEFAULT_CONFIG_ROOT, PROJECT_DIR): + if _has_config_file(root_path): + return root_path, None, root_path + + return DEFAULT_CONFIG_ROOT, None, DEFAULT_CONFIG_ROOT + + +def _load_config(): + if os.environ.get('MAXKB_CONFIG_TYPE') == 'ENV': + root_path, filename, source = _resolve_config_target() + manager = ConfigManager(root_path=root_path) + if filename: + manager.from_yaml(filename) + source = f'ENV + {source}' + elif _has_config_file(root_path): + manager.load_from_yml() + source = f'ENV + {source}' + else: + source = 'ENV' + manager.load_from_env() + return manager.config, source + + root_path, filename, source = _resolve_config_target() + if filename: + manager = ConfigManager(root_path=root_path) + manager.from_yaml(filename) + return manager.config, source + + return ConfigManager.load_user_config(root_path=root_path), source + + +CONFIG, CONFIG_SOURCE = _load_config() diff --git a/check_config.py b/check_config.py index 7f9edeadd..05509804b 100644 --- a/check_config.py +++ b/check_config.py @@ -37,8 +37,9 @@ for path in config_paths: # 加载配置并显示 print("\n3. 实际加载的配置:") try: - from apps.maxkb.const import CONFIG - + from apps.maxkb.const import CONFIG, CONFIG_SOURCE + + print(f" CONFIG_SOURCE: {CONFIG_SOURCE}") print(f" DB_HOST: {CONFIG.get('DB_HOST')}") print(f" DB_PORT: {CONFIG.get('DB_PORT')}") print(f" DB_NAME: {CONFIG.get('DB_NAME')}") @@ -48,6 +49,7 @@ try: print(f" REDIS_HOST: {CONFIG.get('REDIS_HOST')}") print(f" REDIS_PORT: {CONFIG.get('REDIS_PORT')}") print(f" DEBUG: {CONFIG.get('DEBUG')}") + print(f" MINDMAP_URL: {CONFIG.get('MINDMAP_URL')}") # 显示完整的数据库配置 print("\n4. Django 数据库配置:")