from fastapi import FastAPI, Request from fastapi.staticfiles import StaticFiles from fastapi.middleware.cors import CORSMiddleware from fastapi.responses import HTMLResponse, FileResponse from contextlib import asynccontextmanager import os from app.database import engine, Base from app.api import client_router, admin_router @asynccontextmanager async def lifespan(app: FastAPI): # 启动时创建数据库表 Base.metadata.create_all(bind=engine) yield # 关闭时清理 app = FastAPI( title="CursorPro 管理后台", description="Cursor 账号管理系统 API", version="1.0.0", lifespan=lifespan ) # CORS 配置 app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) # 静态文件 static_path = os.path.join(os.path.dirname(__file__), "..", "static") if os.path.exists(static_path): app.mount("/static", StaticFiles(directory=static_path), name="static") # 模板路径 templates_path = os.path.join(os.path.dirname(__file__), "..", "templates") # 注册路由 app.include_router(client_router) app.include_router(admin_router) @app.get("/", response_class=HTMLResponse) async def index(): """管理后台首页""" index_file = os.path.join(templates_path, "index.html") if os.path.exists(index_file): return FileResponse(index_file, media_type="text/html") return HTMLResponse(content="""
请访问 /docs 查看 API 文档
""") @app.get("/health") async def health_check(): """健康检查""" return {"status": "ok"}