mirror of
https://github.com/handsomezhuzhu/QQuiz.git
synced 2026-02-20 20:10:14 +00:00
单容器重构
This commit is contained in:
@@ -1,10 +1,13 @@
|
||||
"""
|
||||
QQuiz FastAPI Application
|
||||
QQuiz FastAPI Application - 单容器模式(前后端整合)
|
||||
"""
|
||||
from fastapi import FastAPI
|
||||
from fastapi import FastAPI, Request
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
from fastapi.responses import HTMLResponse, FileResponse
|
||||
from contextlib import asynccontextmanager
|
||||
import os
|
||||
from pathlib import Path
|
||||
from dotenv import load_dotenv
|
||||
|
||||
from database import init_db, init_default_config, get_db_context
|
||||
@@ -58,22 +61,6 @@ app.add_middleware(
|
||||
)
|
||||
|
||||
|
||||
@app.get("/")
|
||||
async def root():
|
||||
"""Root endpoint"""
|
||||
return {
|
||||
"message": "Welcome to QQuiz API",
|
||||
"version": "1.0.0",
|
||||
"docs": "/docs"
|
||||
}
|
||||
|
||||
|
||||
@app.get("/health")
|
||||
async def health_check():
|
||||
"""Health check endpoint"""
|
||||
return {"status": "healthy"}
|
||||
|
||||
|
||||
# Import and include routers
|
||||
from routers import auth, exam, question, mistake, admin
|
||||
|
||||
@@ -82,3 +69,50 @@ app.include_router(exam.router, prefix="/api/exams", tags=["Exams"])
|
||||
app.include_router(question.router, prefix="/api/questions", tags=["Questions"])
|
||||
app.include_router(mistake.router, prefix="/api/mistakes", tags=["Mistakes"])
|
||||
app.include_router(admin.router, prefix="/api/admin", tags=["Admin"])
|
||||
|
||||
|
||||
# API 健康检查
|
||||
@app.get("/health")
|
||||
async def health_check():
|
||||
"""Health check endpoint"""
|
||||
return {"status": "healthy"}
|
||||
|
||||
|
||||
# ============ 静态文件服务(前后端整合) ============
|
||||
|
||||
# 检查静态文件目录是否存在
|
||||
STATIC_DIR = Path(__file__).parent / "static"
|
||||
if STATIC_DIR.exists():
|
||||
# 挂载静态资源(JS、CSS、图片等)
|
||||
app.mount("/assets", StaticFiles(directory=str(STATIC_DIR / "assets")), name="static_assets")
|
||||
|
||||
# 前端应用的所有路由(SPA路由)
|
||||
@app.get("/{full_path:path}")
|
||||
async def serve_frontend(full_path: str):
|
||||
"""
|
||||
服务前端应用
|
||||
- API 路由已在上面定义,优先匹配
|
||||
- 其他所有路由返回 index.html(SPA 单页应用)
|
||||
"""
|
||||
index_file = STATIC_DIR / "index.html"
|
||||
if index_file.exists():
|
||||
return FileResponse(index_file)
|
||||
else:
|
||||
return {
|
||||
"message": "Frontend not built yet",
|
||||
"hint": "Run 'cd frontend && npm run build' to build the frontend"
|
||||
}
|
||||
else:
|
||||
print("⚠️ 静态文件目录不存在,前端功能不可用")
|
||||
print("提示:请先构建前端应用或使用开发模式")
|
||||
|
||||
# 如果没有静态文件,显示 API 信息
|
||||
@app.get("/")
|
||||
async def root():
|
||||
"""Root endpoint"""
|
||||
return {
|
||||
"message": "Welcome to QQuiz API",
|
||||
"version": "1.0.0",
|
||||
"docs": "/docs",
|
||||
"note": "Frontend not built. Please build frontend or use docker-compose."
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user