From c39781d4dc7e91ab3c4d143251c0402c8a4183b1 Mon Sep 17 00:00:00 2001 From: wood chen Date: Sun, 23 Feb 2025 00:29:07 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dbug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile | 2 +- config.py | 18 ++++++++++++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/Dockerfile b/Dockerfile index eccf595..a092fb6 100644 --- a/Dockerfile +++ b/Dockerfile @@ -18,7 +18,7 @@ COPY main.py . RUN pip install -r requirements.txt # 创建配置文件和日志目录 -RUN mkdir -p logs && touch .env +RUN mkdir -p logs && touch config.yaml # 运行程序 CMD ["python", "main.py"] \ No newline at end of file diff --git a/config.py b/config.py index fdc2787..9102b1c 100644 --- a/config.py +++ b/config.py @@ -1,6 +1,7 @@ import os from typing import Dict, List import yaml +from loguru import logger # 加载YAML配置 def load_config() -> Dict: @@ -8,8 +9,21 @@ def load_config() -> Dict: yaml_files = ["config.yaml", "config.example.yaml"] for yaml_file in yaml_files: if os.path.exists(yaml_file): - with open(yaml_file, "r", encoding="utf-8") as f: - return yaml.safe_load(f) + try: + with open(yaml_file, "r", encoding="utf-8") as f: + config = yaml.safe_load(f) + logger.info(f"成功加载配置文件: {yaml_file}") + # 验证必要的配置项 + if not config.get("tencent", {}).get("secret_id"): + logger.error(f"配置文件 {yaml_file} 中缺少必要的配置项: tencent.secret_id") + return {} + if not config.get("tencent", {}).get("secret_key"): + logger.error(f"配置文件 {yaml_file} 中缺少必要的配置项: tencent.secret_key") + return {} + return config + except Exception as e: + logger.error(f"加载配置文件 {yaml_file} 失败: {str(e)}") + logger.error("未找到有效的配置文件") return {} # 加载配置