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 from linxyun.core import Linxyun from linxyun.config import Config, Upload @asynccontextmanager async def lifespan(app: FastAPI): # 启动定时清理任务 asyncio.create_task(cleanup_streams()) yield # FastAPI 会在这里执行应用关闭时的清理任务 print("停止服务") # 创建 config config = Config( entCode="57", project="smartroadlamp", role={ "1101": ["/**"], "1102": ["/**"] }, white_list=[ "/**" ], upload=Upload(img_quality=0.5) ) linxyun = Linxyun(config) app = FastAPI(lifespan=lifespan) # 添加跨域中间件 add_cors_middleware(app) # 添加鉴权中间件 linxyun.add_security_middleware(app) app.include_router(websocket.router) app.include_router(stream.router)