Skip to content

Commit

Permalink
refactor: use FastAPI.mount and remove the __call__ function
Browse files Browse the repository at this point in the history
  • Loading branch information
NiklasNeugebauer committed Sep 27, 2024
1 parent f53f293 commit c7edefe
Showing 1 changed file with 3 additions and 18 deletions.
21 changes: 3 additions & 18 deletions learning_loop_node/detector/detector_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit c7edefe

Please sign in to comment.