From c7edefebae123f722c85d18342b154d3654de71e Mon Sep 17 00:00:00 2001 From: Niklas Neugebauer Date: Fri, 27 Sep 2024 18:32:32 +0200 Subject: [PATCH] refactor: use FastAPI.mount and remove the __call__ function --- learning_loop_node/detector/detector_node.py | 21 +++----------------- 1 file changed, 3 insertions(+), 18 deletions(-) diff --git a/learning_loop_node/detector/detector_node.py b/learning_loop_node/detector/detector_node.py index b8b8934e..067b9999 100644 --- a/learning_loop_node/detector/detector_node.py +++ b/learning_loop_node/detector/detector_node.py @@ -130,8 +130,9 @@ def setup_sio_server(self) -> None: # Initialize the Socket.IO server self.sio = socketio.AsyncServer(async_mode='asgi') - # Create the ASGI app combining Socket.IO and FastAPI - self.sio_app = socketio.ASGIApp(self.sio, socketio_path='/ws/socket.io/') + # Initialize and mount the ASGI app + self.sio_app = socketio.ASGIApp(self.sio, socketio_path='/socket.io') + self.mount('/ws', self.sio_app) # Register event handlers self.log.info('>>>>>>>>>>>>>>>>>>>>>>> Setting up the SIO server') @@ -193,22 +194,6 @@ async def upload(sid, data: Dict) -> Optional[Dict]: def connect(sid, environ, auth) -> None: self.connected_clients.append(sid) - async def __call__(self, scope, receive, send): - if scope['type'] == 'lifespan': - # Handle lifespan events with FastAPI - await super().__call__(scope, receive, send) - elif scope['type'] in ('http', 'websocket'): - path = scope.get('path', '') - if path.startswith('/ws/socket.io'): - # Handle Socket.IO requests - await self.sio_app(scope, receive, send) - else: - # Handle other HTTP/WebSocket requests with FastAPI - await super().__call__(scope, receive, send) - else: - # For any other scope types, delegate to FastAPI - await super().__call__(scope, receive, send) - async def _check_for_update(self) -> None: if self.operation_mode == OperationMode.Startup: return