config.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import os
  2. from pathlib import Path
  3. import yaml
  4. class Settings:
  5. secret_key: str = ''
  6. sgb_base_url: str = ''
  7. sgb_websocket_url: str = ''
  8. fwr_base_url: str = ''
  9. database_url: str = ''
  10. sgb_db_url: str = ''
  11. fwr_db_url: str = ''
  12. fetch_sgb_agent: str = ''
  13. fetch_fwr_agent: str = ''
  14. PUBLIC_KEY: str
  15. PRIVATE_KEY: str
  16. PASSWORD_KEY: str
  17. basic_base_url: str = ''
  18. basic_paper_url: str = ''
  19. dify_base_url: str = ''
  20. dify_api_token: str = ''
  21. dify_workflow_clean: str = ''
  22. dify_workflow_report: str = ''
  23. postgresql_database_url: str = ''
  24. dify_database_url: str = ''
  25. def __init__(self, **kwargs):
  26. # 替换配置中的IP地址
  27. host_ip = os.getenv('HOST_IP', '127.0.0.1')
  28. kwargs['sgb_base_url'] = kwargs.get('sgb_base_url', '').replace('127.0.0.1', host_ip)
  29. kwargs['sgb_websocket_url'] = kwargs.get('sgb_websocket_url', '').replace('127.0.0.1', host_ip)
  30. kwargs['fwr_base_url'] = kwargs.get('fwr_base_url', '').replace('127.0.0.1', host_ip)
  31. kwargs['sgb_db_url'] = kwargs.get('sgb_db_url', '').replace('127.0.0.1', host_ip)
  32. kwargs['fwr_db_url'] = kwargs.get('fwr_db_url', '').replace('127.0.0.1', host_ip)
  33. kwargs['dify_base_url'] = kwargs.get('dify_base_url', '').replace('127.0.0.1', host_ip)
  34. kwargs['basic_base_url'] = kwargs.get('basic_base_url', '').replace('127.0.0.1', host_ip)
  35. kwargs['dify_database_url'] = kwargs.get('dify_database_url', '').replace('127.0.0.1', host_ip)
  36. # Check if all required fields are provided and set them
  37. for field in self.__annotations__.keys():
  38. if field not in kwargs:
  39. raise ValueError(f"Missing setting: {field}")
  40. setattr(self, field, kwargs[field])
  41. def to_dict(self):
  42. """Return the settings as a dictionary."""
  43. return {k: getattr(self, k) for k in self.__annotations__.keys()}
  44. def __repr__(self):
  45. """Return a string representation of the settings."""
  46. return f"Settings({self.to_dict()})"
  47. def load_yaml(file_path: Path) -> dict:
  48. with file_path.open('r', encoding="utf-8") as fr:
  49. try:
  50. data = yaml.safe_load(fr)
  51. return data
  52. except yaml.YAMLError as e:
  53. print(f"Error loading YAML file {file_path}: {e}")
  54. return {}
  55. # Use pathlib to handle file paths
  56. config_yaml_path = Path(__file__).parent / 'config.yaml'
  57. settings_data = load_yaml(config_yaml_path)
  58. # Initialize settings object
  59. settings = Settings(**settings_data)
  60. # Print the loaded settings
  61. print(f"Loaded settings: {settings}")