config.py 3.0 KB

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