config.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. from pathlib import Path
  2. from typing import Dict, List
  3. from enum import Enum
  4. from typing import ClassVar
  5. import json
  6. from pydantic_settings import BaseSettings, SettingsConfigDict
  7. class Settings(BaseSettings):
  8. BASE_PATH: ClassVar[Path] = Path(__file__).parent.parent.parent.absolute()
  9. TEST_DATA: str = '环境变量读取测试, 目前使用本地字段'
  10. @property
  11. def TEST_DATA_END(self) -> str:
  12. return f'{self.TEST_DATA}/end'
  13. SERVER_PORT:int = 7755
  14. # 用户系统重要的KEY
  15. ADMIN_GRANT_KEY: str = "CardScoreAdminKey2026_aaabbbccc"
  16. TOKEN_SECRET_KEY: str = "CardScoreDataServerTokenSecret2026"
  17. # --- MinIO 配置 ---
  18. MINIO_ENDPOINT: str = "192.168.77.249:9000"
  19. MINIO_ACCESS_KEY: str = "pZEwCGnpNN05KPnmC2Yh"
  20. MINIO_SECRET_KEY: str = "KfJRuWiv9pVxhIMcFqbkv8hZT9SnNTZ6LPx592D4"
  21. MINIO_SECURE: bool = False # 是否使用 https
  22. MINIO_BUCKET: str = "grading"
  23. MINIO_BASE_PREFIX: str = "score_server_data"
  24. # DATA_HOST_URL 现在直接指向 MinIO 的前缀路径
  25. # (注意: 需要在 MinIO 后台将 grading 存储桶的访问权限配置为 Public 或开启特定读策略)
  26. @property
  27. def DATA_HOST_URL(self) -> str:
  28. return f"http://{self.MINIO_ENDPOINT}/{self.MINIO_BUCKET}/{self.MINIO_BASE_PREFIX}"
  29. # CONFIG_PATH: str = BASE_PATH / 'Config.json'
  30. API_PREFIX: str = "/api" # 通用前缀
  31. SCORE_CONFIG_PATH: Path = BASE_PATH / "app/core/scoring_config.json"
  32. # 分数计算接口url
  33. SCORE_UPDATE_SERVER_URL: str = "http://127.0.0.1:7754"
  34. @property
  35. def SCORE_RECALCULATE_ENDPOINT(self) -> str:
  36. return f"{self.SCORE_UPDATE_SERVER_URL}/api/card_score/score_recalculate"
  37. # 分数计算配置接口
  38. @property
  39. def SCORE_SERVER_CONFIG_URL(self) -> str:
  40. return f"{self.SCORE_UPDATE_SERVER_URL}/api/config/scoring_config"
  41. # --- 数据库配置 ---
  42. DB_NAME: str = 'card_score_gray_database'
  43. DB_CARD_TABLE_NAME: str = 'cards'
  44. DB_IMAGE_TABLE_NAME: str = 'card_images'
  45. DB_GRAY_IMAGE_TABLE_NAME: str = 'card_gray_images' # 灰度图表名
  46. DB_USER_TABLE_NAME: str = 'users'
  47. DB_USER_CARD_TABLE_NAME: str = 'user_card_bindings'
  48. RATING_REPORT_HISTORY_TABLE_NAME: str = "rating_report_history"
  49. DB_USER: str = 'root'
  50. DB_PASSWORD: str = '123456'
  51. DB_HOST: str = '127.0.0.1'
  52. @property
  53. def DATABASE_CONFIG(self) -> Dict[str, str]:
  54. return {
  55. 'user': self.DB_USER,
  56. 'password': self.DB_PASSWORD,
  57. 'host': self.DB_HOST,
  58. }
  59. # 连接到指定数据库的配置
  60. @property
  61. def DATABASE_CONFIG_WITH_DB(self) -> Dict[str, str]:
  62. return {
  63. **self.DATABASE_CONFIG,
  64. 'database': self.DB_NAME
  65. }
  66. # 优先级:系统环境变量 > .env 文件
  67. model_config = SettingsConfigDict(env_file=".env",
  68. env_file_encoding='utf-8')
  69. def get_full_url(self, path: str) -> str:
  70. """将相对路径转换为可以直接打开的 MinIO 绝对 URL"""
  71. if not path:
  72. return path
  73. if str(path).startswith("http"):
  74. return path
  75. # 移除开头的斜杠防止双斜杠 (如: /Data/xxx -> Data/xxx)
  76. clean_path = str(path).lstrip("/\\")
  77. return f"{self.DATA_HOST_URL}/{clean_path}"
  78. settings = Settings()
  79. print("测试读取env", settings.TEST_DATA)
  80. print("测试拼接env", settings.TEST_DATA_END)