1
0

config.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. from pathlib import Path
  2. from typing import Dict
  3. from typing import ClassVar
  4. import socket
  5. from pydantic_settings import BaseSettings, SettingsConfigDict
  6. class Settings(BaseSettings):
  7. BASE_PATH: ClassVar[Path] = Path(__file__).parent.parent.parent.absolute()
  8. TEST_DATA: str = '环境变量读取测试, 目前使用本地字段'
  9. @property
  10. def TEST_DATA_END(self) -> str:
  11. return f'{self.TEST_DATA}/end'
  12. SERVER_PORT:int = 7755
  13. APP_NAME: str = "card-score-data-server"
  14. APP_ENV: str = "dev"
  15. LOG_LEVEL: str = "INFO"
  16. # --- MinIO 配置 ---
  17. MINIO_ENDPOINT: str = "192.168.77.249:9000"
  18. MINIO_ACCESS_KEY: str = "pZEwCGnpNN05KPnmC2Yh"
  19. MINIO_SECRET_KEY: str = "KfJRuWiv9pVxhIMcFqbkv8hZT9SnNTZ6LPx592D4"
  20. MINIO_SECURE: bool = False # 是否使用 https
  21. MINIO_BUCKET: str = "grading"
  22. MINIO_BASE_PREFIX: str = "score_server_data"
  23. # DATA_HOST_URL 现在直接指向 MinIO 的前缀路径
  24. # (注意: 需要在 MinIO 后台将 grading 存储桶的访问权限配置为 Public 或开启特定读策略)
  25. @property
  26. def DATA_HOST_URL(self) -> str:
  27. return f"http://{self.MINIO_ENDPOINT}/{self.MINIO_BUCKET}/{self.MINIO_BASE_PREFIX}"
  28. # CONFIG_PATH: str = BASE_PATH / 'Config.json'
  29. API_PREFIX: str = "/api" # 通用前缀
  30. # 导入流程内部自调用卡牌/图片接口时使用的基础地址(用于绕过 ingress 鉴权)
  31. # 为空时回退到 request.base_url
  32. INTERNAL_API_BASE_URL: str = ""
  33. SCORE_CONFIG_PATH: Path = BASE_PATH / "app/core/scoring_config.json"
  34. # 分数计算接口url
  35. SCORE_UPDATE_SERVER_URL: str = "http://127.0.0.1:7754"
  36. RATING_SAVE_PROGRESS: str = "http://order/api/rating/order/progress"
  37. # --- Fluentd 日志配置 ---
  38. FLUENTD_ENABLED: bool = False
  39. FLUENTD_HOST: str = "fluentd"
  40. FLUENTD_PORT: int = 24224
  41. FLUENTD_TAG: str = "card_score.server"
  42. FLUENTD_TAG_PREFIX: str = "python"
  43. FLUENTD_HOSTNAME: str = ""
  44. FLUENTD_TIMEOUT_SEC: float = 3.0
  45. @property
  46. def FLUENTD_EFFECTIVE_TAG(self) -> str:
  47. host = self.FLUENTD_HOSTNAME or socket.gethostname()
  48. return f"{self.FLUENTD_TAG_PREFIX}.{self.FLUENTD_TAG}.{host}"
  49. @property
  50. def SCORE_RECALCULATE_ENDPOINT(self) -> str:
  51. return f"{self.SCORE_UPDATE_SERVER_URL}/api/card_score/score_recalculate"
  52. # 分数计算配置接口
  53. @property
  54. def SCORE_SERVER_CONFIG_URL(self) -> str:
  55. return f"{self.SCORE_UPDATE_SERVER_URL}/api/config/scoring_config"
  56. # --- 数据库配置 ---
  57. DB_NAME: str = 'card_score_gray_database'
  58. DB_CARD_TABLE_NAME: str = 'cards'
  59. DB_IMAGE_TABLE_NAME: str = 'card_images'
  60. DB_GRAY_IMAGE_TABLE_NAME: str = 'card_gray_images' # 灰度图表名
  61. DB_USER_CARD_TABLE_NAME: str = 'user_card_bindings'
  62. RATING_REPORT_HISTORY_TABLE_NAME: str = "rating_report_history"
  63. DB_USER: str = 'root'
  64. DB_PASSWORD: str = '123456'
  65. DB_HOST: str = '127.0.0.1'
  66. DB_PORT: int = 3306
  67. @property
  68. def DATABASE_CONFIG(self) -> Dict[str, str]:
  69. return {
  70. 'user': self.DB_USER,
  71. 'password': self.DB_PASSWORD,
  72. 'host': self.DB_HOST,
  73. 'port': self.DB_PORT,
  74. }
  75. # 连接到指定数据库的配置
  76. @property
  77. def DATABASE_CONFIG_WITH_DB(self) -> Dict[str, str]:
  78. return {
  79. **self.DATABASE_CONFIG,
  80. 'database': self.DB_NAME
  81. }
  82. # 优先级:系统环境变量 > .env 文件
  83. model_config = SettingsConfigDict(env_file=".env",
  84. env_file_encoding='utf-8')
  85. def get_full_url(self, path: str) -> str:
  86. """将相对路径转换为可以直接打开的 MinIO 绝对 URL"""
  87. if not path:
  88. return path
  89. if str(path).startswith("http"):
  90. return path
  91. # 移除开头的斜杠防止双斜杠 (如: /Data/xxx -> Data/xxx)
  92. clean_path = str(path).lstrip("/\\")
  93. return f"{self.DATA_HOST_URL}/{clean_path}"
  94. settings = Settings()