Files
findreve/app.py

59 lines
1.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from fastapi import FastAPI
from fastapi.responses import FileResponse
from fastapi import Request, HTTPException
from contextlib import asynccontextmanager
from routes import (session, admin, object)
import model.database
import os, asyncio
import pkg.conf
# 初始化数据库
asyncio.run(model.database.Database().init_db())
# Findreve 的生命周期
@asynccontextmanager
async def lifespan(app: FastAPI):
await model.database.Database().init_db()
yield
# 定义 Findreve 服务器
app = FastAPI(
title=pkg.conf.APP_NAME,
version=pkg.conf.VERSION,
summary=pkg.conf.summary,
description=pkg.conf.description,
lifespan=lifespan
)
# 挂载后端路由
app.include_router(admin.Router)
app.include_router(session.Router)
app.include_router(object.Router)
@app.get("/")
def read_root():
if not os.path.exists("dist/index.html"):
raise HTTPException(status_code=404, detail="Frontend not built. Please build the frontend first.")
return FileResponse("dist/index.html")
# 回退路由
@app.get("/{path:path}")
async def serve_spa(request: Request, path: str):
if not os.path.exists("dist/index.html"):
raise HTTPException(status_code=404, detail="Frontend not built. Please build the frontend first.")
# 排除API路由
if path.startswith("api/"):
raise HTTPException(status_code=404, detail="Not Found")
# 检查是否是静态资源请求
if path.startswith("assets/") and os.path.exists(f"dist/{path}"):
return FileResponse(f"dist/{path}")
# 检查文件是否存在于dist目录
dist_file_path = os.path.join("dist", path)
if os.path.exists(dist_file_path) and not os.path.isdir(dist_file_path):
return FileResponse(dist_file_path)
# 对于所有其他前端路由返回index.html让Vue Router处理
return FileResponse("dist/index.html")