101 lines
2.9 KiB
Python
101 lines
2.9 KiB
Python
import os
|
|
from pathlib import Path
|
|
from typing import List, Optional
|
|
from pydantic_settings import BaseSettings, SettingsConfigDict
|
|
import yaml
|
|
|
|
|
|
_raw_base_dir = os.environ.get("CADHUB_BASE_DIR")
|
|
if _raw_base_dir:
|
|
BASE_DIR = Path(_raw_base_dir)
|
|
else:
|
|
BASE_DIR = Path(__file__).resolve().parents[1]
|
|
|
|
|
|
class Settings(BaseSettings):
|
|
"""应用配置类"""
|
|
model_config = SettingsConfigDict(
|
|
env_file=str(BASE_DIR / ".env"),
|
|
env_file_encoding="utf-8"
|
|
)
|
|
|
|
# 基础配置
|
|
app_name: str = "CadHubManage"
|
|
debug: bool = False
|
|
host: str = "0.0.0.0"
|
|
port: int = 8000
|
|
|
|
# JWT配置
|
|
secret_key: str = "your-secret-key-change-in-production"
|
|
algorithm: str = "HS256"
|
|
access_token_expire_minutes: int = 30
|
|
refresh_token_expire_days: int = 7
|
|
|
|
# 软件配置文件路径
|
|
software_config_path: str = str(BASE_DIR / "configs" / "software_config.yaml")
|
|
users_config_path: str = str(BASE_DIR / "configs" / "users.json")
|
|
|
|
# CAD文件存储路径
|
|
cad_files_path: str = r"C:\Users\Public\Documents"
|
|
|
|
|
|
# CORS配置
|
|
cors_origins: List[str] = ["*"]
|
|
|
|
# 日志配置
|
|
log_level: str = "INFO"
|
|
log_file: str = str(BASE_DIR / "logs" / "app.log")
|
|
|
|
# 安全配置
|
|
allowed_ips: List[str] = ["*"]
|
|
|
|
|
|
class SoftwareConfig:
|
|
"""软件配置管理类"""
|
|
|
|
def __init__(self, config_path: str):
|
|
self.config_path = config_path
|
|
self._config = None
|
|
|
|
def load_config(self) -> dict:
|
|
"""加载软件配置"""
|
|
try:
|
|
with open(self.config_path, 'r', encoding='utf-8') as f:
|
|
self._config = yaml.safe_load(f)
|
|
return self._config
|
|
except FileNotFoundError:
|
|
raise FileNotFoundError(f"软件配置文件未找到: {self.config_path}")
|
|
except yaml.YAMLError as e:
|
|
raise ValueError(f"软件配置文件格式错误: {e}")
|
|
|
|
def get_software_list(self) -> List[str]:
|
|
"""获取可用软件列表"""
|
|
if not self._config:
|
|
self.load_config()
|
|
|
|
return list(self._config.get('software', {}).keys())
|
|
|
|
def get_software_config(self, software_id: str) -> Optional[dict]:
|
|
"""获取特定软件配置"""
|
|
if not self._config:
|
|
self.load_config()
|
|
|
|
return self._config.get('software', {}).get(software_id)
|
|
|
|
def validate_software_exists(self, software_id: str) -> bool:
|
|
"""验证软件是否存在于配置中"""
|
|
return software_id in self.get_software_list()
|
|
|
|
def get_cad_files_path(self) -> str:
|
|
"""获取CAD文件存储路径"""
|
|
if not self._config:
|
|
self.load_config()
|
|
|
|
file_storage = self._config.get('file_storage', {})
|
|
return file_storage.get('cad_files_path', r'C:\Users\Public\Documents')
|
|
|
|
|
|
|
|
# 创建全局配置实例
|
|
settings = Settings()
|
|
software_config = SoftwareConfig(settings.software_config_path) |