增加了版本号系统

This commit is contained in:
yml2213
2026-06-25 09:38:54 +08:00
parent f21f82cbed
commit e94b37c602
9 changed files with 131 additions and 22 deletions
+8 -1
View File
@@ -12,6 +12,8 @@ from starlette.middleware.base import BaseHTTPMiddleware
from .database import init_db
from .routers import auth, users, accounts, login, proxy, cookies
from .schemas import AppInfo
from .version import get_app_version
from utils import setup_logger
@@ -28,7 +30,7 @@ async def lifespan(app: FastAPI):
app = FastAPI(
title="斗鱼批量登录后台",
version="1.0.0",
version=get_app_version(),
lifespan=lifespan,
)
@@ -74,6 +76,11 @@ def health():
return {"status": "ok"}
@app.get("/api/app-info", response_model=AppInfo)
def app_info():
return AppInfo(version=get_app_version())
# ---- 生产环境:serve 前端静态文件 ----
# Docker 部署时前端构建产物会被复制到 web/frontend/dist
_FRONTEND_DIST = Path(__file__).resolve().parents[2] / "web" / "frontend" / "dist"
+4
View File
@@ -202,6 +202,10 @@ class PlatformInfo(BaseModel):
# ---- 通用 ----
class AppInfo(BaseModel):
version: str
class MessageResponse(BaseModel):
message: str
success: bool = True
+23
View File
@@ -0,0 +1,23 @@
"""应用版本信息。"""
from functools import lru_cache
from importlib.metadata import PackageNotFoundError, version as package_version
from pathlib import Path
import tomllib
@lru_cache(maxsize=1)
def get_app_version() -> str:
"""从 pyproject.toml 读取应用版本,读取失败时回退到包元数据。"""
pyproject_path = Path(__file__).resolve().parents[2] / "pyproject.toml"
if pyproject_path.exists():
with pyproject_path.open("rb") as file:
pyproject = tomllib.load(file)
version = pyproject.get("project", {}).get("version")
if isinstance(version, str) and version:
return version
try:
return package_version("douyu-login-py")
except PackageNotFoundError:
return "0.0.0"