Files
cursornew2026/backend/app/main.py
huangzhenpc bc883df6e8 v2.1.5: 密钥状态显示优化 + 版本号统一
- 密钥无到期时间/积分数据时显示"待验证"而非"已激活"
- 后端验证完成后自动更新为真实状态
- 版本号统一更新到2.1.5(provider/client/main)
- 添加.vscodeignore排除大文件

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-09 23:20:06 +08:00

87 lines
2.1 KiB
Python

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
import logging
from app.database import engine, Base
from app.api import client_router, admin_router
from app.tasks import start_scheduler, stop_scheduler, run_startup_tasks
# 配置日志
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [%(name)s] %(levelname)s: %(message)s"
)
@asynccontextmanager
async def lifespan(app: FastAPI):
# 启动时创建数据库表
Base.metadata.create_all(bind=engine)
# 启动后台任务调度器
start_scheduler()
# 运行启动任务
await run_startup_tasks()
yield
# 关闭时停止调度器
stop_scheduler()
app = FastAPI(
title="蜂鸟Pro 管理后台",
description="蜂鸟Pro 账号管理系统 API v2.1",
version="2.1.5",
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="""
<html>
<head><title>CursorPro 管理后台</title></head>
<body>
<h1>CursorPro 管理后台</h1>
<p>请访问 <a href="/docs">/docs</a> 查看 API 文档</p>
</body>
</html>
""")
@app.get("/health")
async def health_check():
"""健康检查"""
return {"status": "ok"}