from fastapi import FastAPI, WebSocket, WebSocketDisconnect from fastapi.middleware.cors import CORSMiddleware from database import SessionLocal, create_tables from models import Profile from routes.videos import profiles_router, router as videos_router app = FastAPI() app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"], ) app.include_router(videos_router) app.include_router(profiles_router) # --- WebSocket --- connected_clients: set[WebSocket] = set() @app.websocket("/ws") async def websocket_endpoint(websocket: WebSocket): await websocket.accept() connected_clients.add(websocket) try: while True: await websocket.receive_text() except WebSocketDisconnect: connected_clients.discard(websocket) async def notify_clients(profile_ids: list[int]): message = ",".join(str(pid) for pid in profile_ids) for client in list(connected_clients): try: await client.send_text(message) except Exception: connected_clients.discard(client) # --- Startup --- @app.on_event("startup") def startup(): create_tables() db = SessionLocal() if db.query(Profile).count() == 0: db.add(Profile(name="Standard")) db.commit() db.close() @app.get("/") def root(): return {"status": "running"}