]> git.ipfire.org Git - pbs.git/commitdiff
API: Implement scaffolding for the builder's control connections
authorMichael Tremer <michael.tremer@ipfire.org>
Sun, 29 Jun 2025 18:02:59 +0000 (18:02 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sun, 29 Jun 2025 18:02:59 +0000 (18:02 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/api/builders.py

index d942589c47475e2a0bc07651ad7c33f3b0228f1a..f228b7446c9887fc02a99071f41b788619914e21 100644 (file)
@@ -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)