Files
update-server/app/api/routes/frontend.py
T

69 lines
2.3 KiB
Python

from fastapi import APIRouter, FastAPI, HTTPException
from fastapi.responses import FileResponse, Response
from fastapi.staticfiles import StaticFiles
from app.core.config import configured_path
router = APIRouter(include_in_schema=False)
ADMIN_HTML_PATH = configured_path("ADMIN_HTML_PATH", "legacy/admin.html")
ADMIN_UI_DIST_PATH = configured_path("ADMIN_UI_DIST_PATH", "admin-ui/dist")
ADMIN_UI_INDEX_PATH = ADMIN_UI_DIST_PATH / "index.html"
ADMIN_UI_ASSETS_PATH = ADMIN_UI_DIST_PATH / "assets"
def mount_admin_assets(app: FastAPI):
if ADMIN_UI_ASSETS_PATH.is_dir():
app.mount(
"/assets",
StaticFiles(directory=str(ADMIN_UI_ASSETS_PATH)),
name="admin_ui_assets",
)
@router.get("/")
@router.get("/admin.html")
def admin_page():
if ADMIN_UI_INDEX_PATH.is_file():
return FileResponse(ADMIN_UI_INDEX_PATH, headers={"Cache-Control": "no-store"})
if not ADMIN_HTML_PATH.is_file():
raise HTTPException(status_code=404, detail="管理页面文件不存在")
return FileResponse(ADMIN_HTML_PATH, headers={"Cache-Control": "no-store"})
@router.get("/legacy-admin.html")
def legacy_admin_page():
if not ADMIN_HTML_PATH.is_file():
raise HTTPException(status_code=404, detail="旧管理页面文件不存在")
return FileResponse(ADMIN_HTML_PATH, headers={"Cache-Control": "no-store"})
@router.get("/favicon.ico")
def favicon():
icon_path = ADMIN_UI_DIST_PATH / "favicon.ico"
if icon_path.is_file():
return FileResponse(icon_path, headers={"Cache-Control": "public, max-age=3600"})
return Response(status_code=204)
@router.get("/platform-config.json")
def admin_platform_config():
config_path = ADMIN_UI_DIST_PATH / "platform-config.json"
if not config_path.is_file():
raise HTTPException(status_code=404, detail="前端平台配置不存在")
return FileResponse(config_path, headers={"Cache-Control": "no-store"})
@router.get("/logo.svg")
def admin_logo():
logo_path = ADMIN_UI_DIST_PATH / "logo.svg"
if not logo_path.is_file():
raise HTTPException(status_code=404, detail="前端 Logo 不存在")
return FileResponse(logo_path, headers={"Cache-Control": "public, max-age=3600"})
@router.get("/health")
def health_check():
return {"status": "ok"}