12345678910111213141516171819202122232425262728293031323334353637 |
- #!/usr/bin/env python
- """
- @Contact : liuyuqi.gov@msn.cn
- @Time : 2024/03/22 09:27:16
- @License : Copyright © 2017-2022 liuyuqi. All Rights Reserved.
- @Desc :
- """
- from fastapi import APIRouter, FastAPI, Request, HTTPException, status
- from fastapi.responses import (
- StreamingResponse,
- JSONResponse,
- HTMLResponse,
- FileResponse,
- RedirectResponse,
- Response,
- )
- from pathlib import Path
- router = APIRouter()
- @router.get("/")
- async def index():
- return {"code": 200, "message": "this is backend api"}
- @app.get("/{full_path:path}", include_in_schema=False)
- def spa(full_path: str):
- dist_dir = Path(__file__).parent / "dist"
- # TODO: hacky way to only serve index.html on root urls
- files = [entry.name for entry in dist_dir.iterdir() if entry.is_file()]
- if full_path in files:
- return FileResponse(dist_dir / full_path)
- if "." in full_path:
- raise HTTPException(status_code=404, detail="Asset not found")
- return HTMLResponse((dist_dir / "index.html").read_bytes())
|