-
Notifications
You must be signed in to change notification settings - Fork 1
feat(web): add run_server orchestration entry point #110
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,17 @@ | ||
| """WebSocket server for MoBI-View real-time data streaming. | ||
|
|
||
| This module provides WebSocket connection handling and static file serving | ||
| via the websockets process_request hook. | ||
| This module provides WebSocket connection handling, static file serving | ||
| via the websockets process_request hook, and the `run_server` entry | ||
| point that orchestrates the broadcaster and server lifecycle. | ||
| """ | ||
|
|
||
| import asyncio | ||
| import functools | ||
| import json | ||
| import logging | ||
| import mimetypes | ||
| import pathlib | ||
| import signal | ||
| from urllib import parse | ||
|
|
||
| from websockets import datastructures, http11 | ||
|
|
@@ -179,3 +183,86 @@ def _error_response(status_code: int, reason: str) -> http11.Response: | |
| """Build a plain-text HTTP error response.""" | ||
| headers = datastructures.Headers([("Content-Type", "text/plain")]) | ||
| return http11.Response(status_code, reason, headers, reason.encode()) | ||
|
|
||
|
|
||
| def run_server( | ||
| presenter: main_app_presenter.MainAppPresenter, | ||
| host: str = "localhost", | ||
| port: int = 8765, | ||
| ) -> None: | ||
| """Entry point that starts the server runtime from synchronous code. | ||
|
|
||
| This function bridges the synchronous application startup path | ||
| (`main()`) into the async server lifecycle by running | ||
| `_run_server_async` in a fresh event loop. | ||
|
|
||
| The async lifecycle handles broadcaster startup, WebSocket server | ||
| startup, HTTP static-file routing via `process_request`, and | ||
| graceful shutdown on SIGINT/SIGTERM. | ||
|
|
||
| Port 8765 is conventional for WebSocket development servers, | ||
| used by the `websockets` library examples and tutorials. | ||
|
|
||
| Args: | ||
| presenter: The MainAppPresenter providing stream data. | ||
| host: The hostname to bind to. | ||
| port: The port to listen on. | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Comment on why the default is 8765? seems like a default tcp port? |
||
| """ | ||
| asyncio.run(_run_server_async(presenter, host, port)) | ||
|
|
||
|
|
||
| async def _run_server_async( | ||
| presenter: main_app_presenter.MainAppPresenter, | ||
| host: str, | ||
| port: int, | ||
| ) -> None: | ||
| """Starts and manages the async WebSocket server lifecycle. | ||
|
|
||
| This initializes the Broadcaster's background polling thread and | ||
| starts the WebSocket server on the specified host and port. It acts | ||
| as the main router, serving static files for plain HTTP requests | ||
| and directing WebSocket connections to the streaming handler. | ||
|
|
||
| The server runs continuously until it receives a shutdown signal | ||
| (SIGINT/SIGTERM), ensuring a graceful teardown of the broadcaster. | ||
|
|
||
| Args: | ||
| presenter: The MainAppPresenter providing stream data. | ||
| host: The hostname to bind to. | ||
| port: The port to listen on. | ||
| """ | ||
| active_broadcaster = broadcaster.Broadcaster(presenter) | ||
| active_broadcaster.start() | ||
|
|
||
| stop_event = asyncio.Event() | ||
| _register_shutdown_signals(stop_event) | ||
|
|
||
| handler = functools.partial( | ||
| ws_handler, | ||
| active_broadcaster=active_broadcaster, | ||
| presenter=presenter, | ||
| ) | ||
|
|
||
| try: | ||
| async with server.serve( | ||
| handler, | ||
| host, | ||
| port, | ||
| process_request=process_request, | ||
| ): | ||
| logger.info("Server listening on ws://%s:%d", host, port) | ||
| await stop_event.wait() | ||
| finally: | ||
| active_broadcaster.stop() | ||
| logger.info("Server shut down") | ||
|
|
||
|
|
||
| def _register_shutdown_signals(stop_event: asyncio.Event) -> None: | ||
| """Register SIGINT and SIGTERM to set the stop event. | ||
|
|
||
| Args: | ||
| stop_event: The asyncio event to set when a signal is received. | ||
| """ | ||
| loop = asyncio.get_running_loop() | ||
| for sig in (signal.SIGINT, signal.SIGTERM): | ||
| loop.add_signal_handler(sig, stop_event.set) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now that this does something should we add a smoke test for main to check we actually open the web page (which has nothing in it right?)