config.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. """
  2. 系统配置管理模块
  3. 提供系统配置的获取、验证和安全过滤功能
  4. """
  5. import logging
  6. import os
  7. from flask import current_app
  8. logger = logging.getLogger(__name__)
  9. def get_system_config():
  10. """
  11. 获取系统配置信息
  12. 过滤掉敏感的配置项
  13. Returns:
  14. dict: 过滤后的系统配置信息
  15. """
  16. try:
  17. # 收集系统配置信息(去除敏感信息)
  18. config_info = {
  19. "environment": current_app.config["FLASK_ENV"],
  20. "debug_mode": current_app.config["DEBUG"],
  21. "platform": current_app.config["PLATFORM"],
  22. "port": current_app.config["PORT"],
  23. "allowed_extensions": list(current_app.config["ALLOWED_EXTENSIONS"]),
  24. "bucket_name": current_app.config["BUCKET_NAME"],
  25. "prefix": current_app.config["PREFIX"],
  26. }
  27. return config_info
  28. except Exception as e:
  29. logger.error(f"获取系统配置失败: {str(e)}")
  30. return {"error": str(e)}
  31. def validate_config():
  32. """
  33. 验证系统配置的有效性
  34. 检查必要的配置项是否存在且有效
  35. Returns:
  36. tuple: (是否有效, 错误信息)
  37. """
  38. errors = []
  39. # 检查Neo4j配置
  40. if "NEO4J_URI" not in current_app.config or not current_app.config["NEO4J_URI"]:
  41. errors.append("NEO4J_URI未配置")
  42. if "NEO4J_USER" not in current_app.config or not current_app.config["NEO4J_USER"]:
  43. errors.append("NEO4J_USER未配置")
  44. if (
  45. "NEO4J_PASSWORD" not in current_app.config
  46. or not current_app.config["NEO4J_PASSWORD"]
  47. ):
  48. errors.append("NEO4J_PASSWORD未配置")
  49. # 检查MinIO配置
  50. if "MINIO_HOST" not in current_app.config or not current_app.config["MINIO_HOST"]:
  51. errors.append("MINIO_HOST未配置")
  52. if "MINIO_USER" not in current_app.config or not current_app.config["MINIO_USER"]:
  53. errors.append("MINIO_USER未配置")
  54. if (
  55. "MINIO_PASSWORD" not in current_app.config
  56. or not current_app.config["MINIO_PASSWORD"]
  57. ):
  58. errors.append("MINIO_PASSWORD未配置")
  59. # 检查其他必要配置
  60. if "BUCKET_NAME" not in current_app.config or not current_app.config["BUCKET_NAME"]:
  61. errors.append("BUCKET_NAME未配置")
  62. if "PREFIX" not in current_app.config:
  63. errors.append("PREFIX未配置")
  64. return (len(errors) == 0, errors)
  65. def get_config_file_paths():
  66. """
  67. 获取系统所有配置文件的路径
  68. Returns:
  69. list: 配置文件路径列表
  70. """
  71. base_dir = os.path.dirname(
  72. os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  73. )
  74. config_dir = os.path.join(base_dir, "config")
  75. if not os.path.exists(config_dir):
  76. logger.warning(f"配置目录不存在: {config_dir}")
  77. return []
  78. config_files = []
  79. for file in os.listdir(config_dir):
  80. if file.endswith(".py") or file.endswith(".yaml") or file.endswith(".json"):
  81. config_files.append(os.path.join(config_dir, file))
  82. return config_files