|
@@ -16,32 +16,34 @@ class DatabaseConfig:
|
|
|
if self._config is not None:
|
|
if self._config is not None:
|
|
|
return self._config
|
|
return self._config
|
|
|
|
|
|
|
|
- # 使用 python-dotenv 加载环境变量
|
|
|
|
|
- # find_dotenv() 会自动查找 .env 文件
|
|
|
|
|
- env_path = find_dotenv(self.env_file)
|
|
|
|
|
-
|
|
|
|
|
- if not env_path:
|
|
|
|
|
- # 手动尝试多个可能的.env文件位置
|
|
|
|
|
- possible_paths = [
|
|
|
|
|
- # 当前工作目录
|
|
|
|
|
- os.path.join(os.getcwd(), self.env_file),
|
|
|
|
|
- # 项目根目录(从当前文件位置推算)
|
|
|
|
|
- os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(__file__))), self.env_file),
|
|
|
|
|
- # mysql目录的上级目录的上级目录
|
|
|
|
|
- os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))), self.env_file)
|
|
|
|
|
- ]
|
|
|
|
|
-
|
|
|
|
|
- for path in possible_paths:
|
|
|
|
|
- if os.path.exists(path):
|
|
|
|
|
- env_path = path
|
|
|
|
|
- break
|
|
|
|
|
|
|
+ # 若运行时已经通过 docker --env-file 注入了数据库配置,直接使用,不强制依赖 .env 文件
|
|
|
|
|
+ if not self._has_runtime_env_config():
|
|
|
|
|
+ # 使用 python-dotenv 加载环境变量
|
|
|
|
|
+ # find_dotenv() 会自动查找 .env 文件
|
|
|
|
|
+ env_path = find_dotenv(self.env_file)
|
|
|
|
|
|
|
|
if not env_path:
|
|
if not env_path:
|
|
|
- searched_paths = '\n'.join(possible_paths)
|
|
|
|
|
- raise FileNotFoundError(f"配置文件 {self.env_file} 不存在,已搜索以下路径:\n{searched_paths}")
|
|
|
|
|
-
|
|
|
|
|
- # 加载环境变量
|
|
|
|
|
- load_dotenv(env_path)
|
|
|
|
|
|
|
+ # 手动尝试多个可能的.env文件位置
|
|
|
|
|
+ possible_paths = [
|
|
|
|
|
+ # 当前工作目录
|
|
|
|
|
+ os.path.join(os.getcwd(), self.env_file),
|
|
|
|
|
+ # 项目根目录(从当前文件位置推算)
|
|
|
|
|
+ os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(__file__))), self.env_file),
|
|
|
|
|
+ # mysql目录的上级目录的上级目录
|
|
|
|
|
+ os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))), self.env_file)
|
|
|
|
|
+ ]
|
|
|
|
|
+
|
|
|
|
|
+ for path in possible_paths:
|
|
|
|
|
+ if os.path.exists(path):
|
|
|
|
|
+ env_path = path
|
|
|
|
|
+ break
|
|
|
|
|
+
|
|
|
|
|
+ if not env_path:
|
|
|
|
|
+ searched_paths = '\n'.join(possible_paths)
|
|
|
|
|
+ raise FileNotFoundError(f"配置文件 {self.env_file} 不存在,已搜索以下路径:\n{searched_paths}")
|
|
|
|
|
+
|
|
|
|
|
+ # 加载环境变量
|
|
|
|
|
+ load_dotenv(env_path)
|
|
|
|
|
|
|
|
# 优先读取 DB_INFO(JSON 字符串)
|
|
# 优先读取 DB_INFO(JSON 字符串)
|
|
|
db_info_str = os.getenv('DB_INFO')
|
|
db_info_str = os.getenv('DB_INFO')
|
|
@@ -97,6 +99,15 @@ class DatabaseConfig:
|
|
|
|
|
|
|
|
return self._config
|
|
return self._config
|
|
|
|
|
|
|
|
|
|
+ @staticmethod
|
|
|
|
|
+ def _has_runtime_env_config() -> bool:
|
|
|
|
|
+ """检查是否已由运行环境注入数据库配置。"""
|
|
|
|
|
+ if os.getenv("DB_INFO"):
|
|
|
|
|
+ return True
|
|
|
|
|
+
|
|
|
|
|
+ required_keys = ("DB_HOST", "DB_USER", "DB_PASSWORD", "DB_NAME")
|
|
|
|
|
+ return all(os.getenv(k) for k in required_keys)
|
|
|
|
|
+
|
|
|
@staticmethod
|
|
@staticmethod
|
|
|
def _clean_env_value(value: str) -> str:
|
|
def _clean_env_value(value: str) -> str:
|
|
|
"""清理环境变量中可能存在的空白与包裹引号。"""
|
|
"""清理环境变量中可能存在的空白与包裹引号。"""
|