feat: 添加前端静态文件服务

This commit is contained in:
Sens 2026-02-21 01:49:42 +08:00
parent 3d62957fed
commit 9cc280a892
1 changed files with 24 additions and 0 deletions

View File

@ -1,5 +1,7 @@
from fastapi import FastAPI, Depends, HTTPException, BackgroundTasks from fastapi import FastAPI, Depends, HTTPException, BackgroundTasks
from fastapi.middleware.cors import CORSMiddleware from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
from fastapi.responses import FileResponse
from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.future import select from sqlalchemy.future import select
from typing import List from typing import List
@ -1140,3 +1142,25 @@ async def update_ai_config(config: LLMConfig):
"provider": config.provider, "provider": config.provider,
"requires_restart": True "requires_restart": True
} }
# 前端静态文件服务
FRONTEND_DIR = Path(__file__).parent.parent / "frontend" / "dist"
@app.get("/")
async def serve_frontend():
"""服务前端首页"""
index_path = FRONTEND_DIR / "index.html"
if index_path.exists():
return FileResponse(index_path)
return {"message": "Frontend not found", "frontend_dir": str(FRONTEND_DIR)}
@app.get("/{path:path}")
async def serve_frontend_paths(path: str):
"""服务前端其他路由"""
# 先尝试直接文件
file_path = FRONTEND_DIR / path
if file_path.exists() and file_path.is_file():
return FileResponse(file_path)
# 回退到 index.html (SPA 路由)
return FileResponse(FRONTEND_DIR / "index.html")