add build-frontend API endpoint

This commit is contained in:
Sens 2026-02-21 02:20:59 +08:00
parent 7a71a2147b
commit 20956731ed
1 changed files with 35 additions and 0 deletions

View File

@ -1164,3 +1164,38 @@ async def serve_frontend_paths(path: str):
return FileResponse(file_path) return FileResponse(file_path)
# 回退到 index.html (SPA 路由) # 回退到 index.html (SPA 路由)
return FileResponse(FRONTEND_DIR / "index.html") return FileResponse(FRONTEND_DIR / "index.html")
@app.post("/admin/build-frontend")
async def build_frontend(background_tasks: BackgroundTasks):
"""构建前端 (仅用于部署)"""
import subprocess
frontend_path = FRONTEND_DIR.parent
async def run_build():
try:
result = subprocess.run(
["npm", "install"],
cwd=str(frontend_path),
capture_output=True,
text=True,
timeout=300
)
result = subprocess.run(
["npm", "run", "build"],
cwd=str(frontend_path),
capture_output=True,
text=True,
timeout=300
)
return result.returncode == 0, result.stdout, result.stderr
except Exception as e:
return False, "", str(e)
success, stdout, stderr = await run_build()
if success:
return {"status": "success", "message": "前端构建成功"}
else:
return {"status": "error", "message": stderr or "构建失败"}