Files
Deal/archive/leadradar-legacy/backend/app/routers/events_routes.py
T
Rustam Khalimov 713d554dc2 Нормализовать переводы строк в LF
Решение по TD-STYLE-ANALYZERS: LF — инструменты проекта (Python/Node) пишут LF,
CRLF-.sh не работают на Linux CI (sh scripts/ci.sh), большинство файлов уже были
LF. Добавлен .gitattributes (* text=auto eol=lf, бинарные исключения),
.editorconfig переведён на lf, 1029 файлов конвертированы, git add --renormalize.
Из индекса убраны закравшиеся archive/**/__pycache__/*.pyc.
2026-09-11 19:01:42 +03:00

39 lines
1.0 KiB
Python

"""SSE-события (realtime) для фронтенда."""
from __future__ import annotations
import asyncio
from fastapi import APIRouter, Depends, Request
from fastapi.responses import StreamingResponse
from ..auth import current_login
from ..sse import broker
router = APIRouter(prefix="/api", tags=["events"])
@router.get("/events")
async def events(request: Request, _: str = Depends(current_login)):
async def stream():
q = await broker.subscribe()
try:
while True:
if await request.is_disconnected():
break
try:
yield await asyncio.wait_for(q.get(), timeout=15)
except asyncio.TimeoutError:
yield ": ping\n\n"
finally:
await broker.unsubscribe(q)
return StreamingResponse(
stream(),
media_type="text/event-stream",
headers={
"Cache-Control": "no-cache",
"Connection": "keep-alive",
"X-Accel-Buffering": "no",
},
)