feat(api): add backend

routes and WebSockets
This commit is contained in:
2025-12-26 18:19:06 +03:00
parent cfec8d0ff6
commit 1b864228d4
28 changed files with 631 additions and 2 deletions

View File

@@ -0,0 +1,41 @@
from __future__ import annotations
import asyncio
from contextlib import suppress
from datetime import timezone
from sqlalchemy.ext.asyncio import AsyncSession
from app.db.session import SessionLocal
from app.repositories.audio_repository import AudioRepository
from app.ws.manager import manager
def _iso_z(dt) -> str:
# dt ожидается timezone-aware
return dt.astimezone(timezone.utc).isoformat().replace("+00:00", "Z")
async def audio_live_broadcaster(poll_interval_sec: float = 0.2) -> None:
last_time = None
while True:
try:
async with SessionLocal() as db: # AsyncSession
repo = AudioRepository(db)
rows = await repo.latest(1)
if rows:
row = rows[0]
if last_time is None or row.time > last_time:
last_time = row.time
await manager.broadcast_json(
{
"time": _iso_z(row.time),
"rms_db": float(row.rms_db),
"freq_hz": int(row.frequency_hz),
}
)
except Exception:
# чтобы WS не умирал из-за временных проблем с БД
pass
await asyncio.sleep(poll_interval_sec)

View File

@@ -0,0 +1,39 @@
from __future__ import annotations
import asyncio
from typing import Any
from fastapi import WebSocket
class ConnectionManager:
def __init__(self) -> None:
self._connections: set[WebSocket] = set()
self._lock = asyncio.Lock()
async def connect(self, ws: WebSocket) -> None:
await ws.accept()
async with self._lock:
self._connections.add(ws)
async def disconnect(self, ws: WebSocket) -> None:
async with self._lock:
self._connections.discard(ws)
async def broadcast_json(self, payload: dict[str, Any]) -> None:
async with self._lock:
conns = list(self._connections)
to_remove: list[WebSocket] = []
for ws in conns:
try:
await ws.send_json(payload)
except Exception:
to_remove.append(ws)
if to_remove:
async with self._lock:
for ws in to_remove:
self._connections.discard(ws)
manager = ConnectionManager()

View File

@@ -0,0 +1,19 @@
from fastapi import APIRouter, WebSocket
from starlette.websockets import WebSocketDisconnect
from app.ws.manager import manager
router = APIRouter()
@router.websocket("/ws/live")
async def ws_live(ws: WebSocket) -> None:
await manager.connect(ws)
try:
# Держим соединение
while True:
await ws.receive_text()
except WebSocketDisconnect:
await manager.disconnect(ws)
except Exception:
await manager.disconnect(ws)