|
@@ -2,7 +2,9 @@ import os, yaml, datetime
|
|
|
from typing import Any, List, Callable
|
|
|
from watchdog.observers import Observer
|
|
|
from watchdog.events import FileSystemEventHandler
|
|
|
+from dotenv import load_dotenv
|
|
|
from .config import Config
|
|
|
+from .config_env_mapping import ENV_MAPPING
|
|
|
|
|
|
|
|
|
class ConfigReloadHandler(FileSystemEventHandler):
|
|
@@ -21,18 +23,6 @@ class ConfigLoader:
|
|
|
_instance = None
|
|
|
_observers = []
|
|
|
|
|
|
- # 环境变量映射配置
|
|
|
- ENV_MAPPING = {
|
|
|
- 'APP_NAME': 'app.name',
|
|
|
- 'APP_VERSION': 'app.version',
|
|
|
- 'APP_DEBUG': 'app.debug',
|
|
|
- 'DB_HOST': 'database.host',
|
|
|
- 'DB_PORT': 'database.port',
|
|
|
- 'DB_USER': 'database.user',
|
|
|
- 'DB_PASSWORD': 'database.password',
|
|
|
- 'DB_NAME': 'database.name',
|
|
|
- }
|
|
|
-
|
|
|
def __new__(cls):
|
|
|
if cls._instance is None:
|
|
|
cls._instance = super().__new__(cls)
|
|
@@ -41,7 +31,7 @@ class ConfigLoader:
|
|
|
|
|
|
def _load_env_vars(self, config: Config) -> Config:
|
|
|
"""加载环境变量配置"""
|
|
|
- for env_key, config_path in self.ENV_MAPPING.items():
|
|
|
+ for env_key, config_path in ENV_MAPPING.items():
|
|
|
value = os.getenv(env_key)
|
|
|
if value is None:
|
|
|
continue
|
|
@@ -152,16 +142,26 @@ class ConfigLoader:
|
|
|
self._config = Config()
|
|
|
self._callbacks: List[Callable] = []
|
|
|
|
|
|
+ # 获取当前文件所在目录并保存到类变量
|
|
|
+ self._current_dir = os.path.dirname(
|
|
|
+ os.path.dirname(os.path.abspath(__file__)))
|
|
|
+ load_dotenv(os.path.join(self._current_dir, '.env'))
|
|
|
+ # print(f"配置当前目录为:{self._current_dir}")
|
|
|
+
|
|
|
# 1. 加载默认值(通过Config类初始化)
|
|
|
|
|
|
# 2. 加载通用配置
|
|
|
self._config.update_from_dict(
|
|
|
- self._load_yaml_config('SERVER/app/application.yml'))
|
|
|
+ self._load_yaml_config(
|
|
|
+ os.path.join(self._current_dir, 'application.yml')))
|
|
|
|
|
|
# 3. 加载指定环境配置
|
|
|
- env = os.getenv('ENV', 'dev')
|
|
|
- self._config.update_from_dict(
|
|
|
- self._load_yaml_config(f'SERVER/app/application-{env}.yml'))
|
|
|
+ env = os.getenv('ENV')
|
|
|
+ print(f"当前环境:{env}")
|
|
|
+ if env:
|
|
|
+ self._config.update_from_dict(
|
|
|
+ self._load_yaml_config(
|
|
|
+ os.path.join(self._current_dir, f'application-{env}.yml')))
|
|
|
|
|
|
# 4. 最后加载环境变量(最高优先级)
|
|
|
self._config = self._load_env_vars(self._config)
|
|
@@ -173,13 +173,32 @@ class ConfigLoader:
|
|
|
"""启动配置文件监控"""
|
|
|
event_handler = ConfigReloadHandler(self.reload_config)
|
|
|
observer = Observer()
|
|
|
- observer.schedule(event_handler, path='SERVER/app/', recursive=False)
|
|
|
+
|
|
|
+ # 只监控application.yml和application-{env}.yml文件
|
|
|
+ env = os.getenv('ENV', 'dev')
|
|
|
+ config_files = [
|
|
|
+ os.path.join(self._current_dir, 'application.yml'),
|
|
|
+ os.path.join(self._current_dir, f'application-{env}.yml')
|
|
|
+ ]
|
|
|
+
|
|
|
+ for config_file in config_files:
|
|
|
+ if os.path.exists(config_file):
|
|
|
+ observer.schedule(event_handler,
|
|
|
+ path=os.path.dirname(config_file),
|
|
|
+ recursive=False)
|
|
|
+
|
|
|
observer.start()
|
|
|
self._observers.append(observer)
|
|
|
|
|
|
def reload_config(self):
|
|
|
"""重新加载配置"""
|
|
|
+ # 1. 重新加载配置文件
|
|
|
self._load_config()
|
|
|
+
|
|
|
+ # 2. 重新加载环境变量(确保覆盖配置文件)
|
|
|
+ self._config = self._load_env_vars(self._config)
|
|
|
+
|
|
|
+ # 3. 触发回调
|
|
|
for callback in self._callbacks:
|
|
|
callback()
|
|
|
|