Files
findreve/app.py
于小丘 c1c36c606f Refactor config, logging, and startup structure
Introduced pkg modules for environment config, logging, and startup initialization. Replaced direct config and logging setup in main.py with modularized functions. Updated database and migration modules to use environment variables and improved DEBUG handling. Removed tool.py and migrated password utilities to pkg. Cleaned up legacy comments and unused code in models and routes.
2025-10-03 15:00:45 +08:00

59 lines
1.7 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)
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)
# 排除API路由
if path.startswith("api/"):
raise HTTPException(status_code=404)
# 检查是否是静态资源请求
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")