Files
sound-analyze/services/api/app/main.py
Iwwww 1b864228d4 feat(api): add backend
routes and WebSockets
2025-12-26 19:26:06 +03:00

47 lines
1.1 KiB
Python

from fastapi import FastAPI
import asyncio
from contextlib import asynccontextmanager, suppress
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from app.api.v1.router import router as v1_router
from app.core.config import settings
from app.ws.router import router as ws_router
from app.ws.broadcaster import audio_live_broadcaster
@asynccontextmanager
async def lifespan(app: FastAPI):
task = asyncio.create_task(audio_live_broadcaster())
try:
yield
finally:
task.cancel()
with suppress(asyncio.CancelledError):
await task
def create_app() -> FastAPI:
app = FastAPI(
title="Audio Analyzer API",
version="1.0.0",
docs_url="/api/docs",
redoc_url="/api/redoc",
lifespan=lifespan,
)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
app.include_router(v1_router, prefix=settings.API_V1_PREFIX)
app.include_router(ws_router) # /ws/live
return app
app = create_app()