From 8ca7ba29f2ea63ce27349d585b05a7ee1fff27da Mon Sep 17 00:00:00 2001 From: Federico Caselli Date: Thu, 25 Jul 2024 23:24:01 +0200 Subject: [PATCH] feat(ws-tutorial): add web page used by the echo client --- docs/user/tutorial-websockets.rst | 1 + examples/wslook/wslook/app.py | 6 ++ examples/wslook/wslook/static/index.html | 80 ++++++++++++++++++++++++ 3 files changed, 87 insertions(+) create mode 100644 examples/wslook/wslook/static/index.html diff --git a/docs/user/tutorial-websockets.rst b/docs/user/tutorial-websockets.rst index a9d48b36a..264bf721f 100644 --- a/docs/user/tutorial-websockets.rst +++ b/docs/user/tutorial-websockets.rst @@ -461,6 +461,7 @@ Things we've changed: - Added a new middleware class `AuthMiddleware` that will check the token on the first message. - Opening a websocket connection is now handled by the middleware. - The client now sends a token as the first message, if required for that route. +- Falcon was configured to server a simple html page to use the echo websocket client for a browser. If you try to query the reports endpoint now, everything works as expected on an authenticated route. diff --git a/examples/wslook/wslook/app.py b/examples/wslook/wslook/app.py index 4db3a59ca..1c3760341 100644 --- a/examples/wslook/wslook/app.py +++ b/examples/wslook/wslook/app.py @@ -1,5 +1,6 @@ from datetime import datetime import logging +import pathlib import uvicorn @@ -115,5 +116,10 @@ async def on_websocket(self, req: Request, ws: WebSocket): app.add_middleware(LoggerMiddleware()) app.add_middleware(AuthMiddleware(['/reports'])) +# usually a web server, like Nginx or Caddy, should server static assets, but +# for the purpose of this example we use falcon. +static_path = pathlib.Path(__file__).parent / 'static' +app.add_static_route('/', static_path, fallback_filename='index.html') + if __name__ == '__main__': uvicorn.run(app, host='localhost', port=8000) # pragma: no cover diff --git a/examples/wslook/wslook/static/index.html b/examples/wslook/wslook/static/index.html new file mode 100644 index 000000000..f3432548f --- /dev/null +++ b/examples/wslook/wslook/static/index.html @@ -0,0 +1,80 @@ + + + + + WebSocket Echo + + + + + +

WebSocket Echo Test

+
+ + +
+

+
+
+
\ No newline at end of file