5c028d7952
包含 FastAPI 后端、React 前端、队列/OCR/标签/待办等完整功能。 Co-authored-by: Cursor <cursoragent@cursor.com>
67 lines
1.7 KiB
Python
67 lines
1.7 KiB
Python
"""全局配置:路径、数据库、并发参数等。"""
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
from pathlib import Path
|
|
from pydantic import Field
|
|
from pydantic_settings import BaseSettings, SettingsConfigDict
|
|
|
|
|
|
# 默认数据目录:放在 backend/.data 下,便于零配置启动
|
|
_BACKEND_ROOT = Path(__file__).resolve().parents[2]
|
|
_DEFAULT_DATA_DIR = _BACKEND_ROOT / ".data"
|
|
|
|
|
|
class Settings(BaseSettings):
|
|
"""读取 .env 与环境变量的全局配置。"""
|
|
|
|
model_config = SettingsConfigDict(
|
|
env_file=str(_BACKEND_ROOT / ".env"),
|
|
env_file_encoding="utf-8",
|
|
extra="ignore",
|
|
)
|
|
|
|
# 应用基础
|
|
app_name: str = "snapAna"
|
|
debug: bool = False
|
|
host: str = "127.0.0.1"
|
|
port: int = 8765
|
|
|
|
# 数据目录
|
|
data_dir: Path = Field(default=_DEFAULT_DATA_DIR)
|
|
|
|
# 任务并发
|
|
analyze_concurrency: int = 4
|
|
max_retries: int = 3
|
|
|
|
# 缩略图
|
|
thumb_size: int = 320
|
|
vlm_max_side: int = 1280 # 上传 VLM 前压缩的长边像素
|
|
|
|
# CORS
|
|
cors_origins: list[str] = ["http://localhost:5173", "http://127.0.0.1:5173"]
|
|
|
|
@property
|
|
def db_path(self) -> Path:
|
|
"""SQLite 数据库文件路径。"""
|
|
return self.data_dir / "snapana.db"
|
|
|
|
@property
|
|
def db_url(self) -> str:
|
|
"""SQLAlchemy 连接串。"""
|
|
return f"sqlite:///{self.db_path.as_posix()}"
|
|
|
|
@property
|
|
def thumb_dir(self) -> Path:
|
|
"""缩略图缓存目录。"""
|
|
return self.data_dir / "thumbs"
|
|
|
|
def ensure_dirs(self) -> None:
|
|
"""确保所有运行期目录存在。"""
|
|
self.data_dir.mkdir(parents=True, exist_ok=True)
|
|
self.thumb_dir.mkdir(parents=True, exist_ok=True)
|
|
|
|
|
|
settings = Settings()
|
|
settings.ensure_dirs()
|