From: Michael Tremer Date: Sun, 29 Jun 2025 18:02:59 +0000 (+0000) Subject: API: Implement scaffolding for the builder's control connections X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c49c44e71e8d48036f18357890044a21b3dee6ff;p=pbs.git API: Implement scaffolding for the builder's control connections Signed-off-by: Michael Tremer --- diff --git a/src/api/builders.py b/src/api/builders.py index d942589c..f228b744 100644 --- a/src/api/builders.py +++ b/src/api/builders.py @@ -24,7 +24,7 @@ from . import apiv1 from . import auth from . import backend -from .. import users +from .. import builders # Create a new router for all builder endpoints router = fastapi.APIRouter( @@ -36,6 +36,38 @@ router = fastapi.APIRouter( async def get(): return [builder async for builder in backend.builders] +@router.websocket("/control") +async def control(websocket: fastapi.WebSocket, + builder = fastapi.Depends(auth.get_current_principal)): + # Close the connection is something else but a builder has connected + if not isinstance(builder, builders.Builder): + return await websocket.close(code=fastapi.status.WS_1008_POLICY_VIOLATION) + + # Fetch where the builder is connecting from + address, port = websocket.client + + # Accept the connection + await websocket.accept() + + # The builder is now connected + async with backend.db as session: + builder.connected(websocket, address=address) + + try: + # Wait for any messages from the builder + while True: + message = await websocket.receive_text() + + # Process the message + async with backend.db as session: + # builder.receive_message(message) + pass + + # Catch if the builder has disconnected + except fastapi.WebSocketDisconnect as e: + async with backend.db as session: + builder.disconnected() + # Add everything to the APIv1 apiv1.include_router(router)