26 lines
726 B
Python
26 lines
726 B
Python
from app.middleware.cors import add_cors_middleware
|
|
from app.middleware.security import add_security_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):
|
|
# 启动定时清理任务
|
|
asyncio.create_task(cleanup_streams())
|
|
yield # FastAPI 会在这里执行应用关闭时的清理任务
|
|
print("停止服务")
|
|
|
|
app = FastAPI(lifespan=lifespan)
|
|
# 添加跨域中间件
|
|
add_cors_middleware(app)
|
|
# 添加鉴权中间件
|
|
add_security_middleware(app)
|
|
app.include_router(websocket.router)
|
|
app.include_router(stream.router)
|
|
|
|
|
|
|