from app.middleware.cors import add_cors_middleware from app.routes import websocket, stream from app.tasks.cleanup_task import cleanup_streams from fastapi import FastAPI from contextlib import asynccontextmanager import asyncio @asynccontextmanager async def lifespan(app: FastAPI): print("正在启动服务...") # 启动定时清理任务 asyncio.create_task(cleanup_streams()) yield # FastAPI 会在这里执行应用关闭时的清理任务 print("停止服务") app = FastAPI(lifespan=lifespan) # 添加跨域中间件 add_cors_middleware(app) app.include_router(websocket.router) app.include_router(stream.router)