from . import auth
from . import backend
-from .. import users
+from .. import builders
# Create a new router for all builder endpoints
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)