diff --git a/apps/demo/app.py b/apps/demo/app.py index e86ee41..cd26e8e 100644 --- a/apps/demo/app.py +++ b/apps/demo/app.py @@ -25,18 +25,25 @@ async def ws_handler(request): raise web.HTTPNotFound() ws = web.WebSocketResponse(heartbeat=30, max_msg_size=1024 * 1024) await ws.prepare(request) - async for msg in ws: - if msg.type == WSMsgType.TEXT: - await ws.send_str(f"echo:{msg.data}") - elif msg.type == WSMsgType.BINARY: - await ws.send_bytes(msg.data) - elif msg.type == WSMsgType.ERROR: - break + try: + async for msg in ws: + if msg.type == WSMsgType.TEXT: + await ws.send_str(f"echo:{msg.data}") + elif msg.type == WSMsgType.BINARY: + await ws.send_bytes(msg.data) + elif msg.type in (WSMsgType.CLOSE, WSMsgType.CLOSING, WSMsgType.CLOSED, WSMsgType.ERROR): + break + except (ConnectionResetError, RuntimeError): + pass + finally: + if not ws.closed: + await ws.close() return ws def create_app(): app = web.Application(client_max_size=10 * 1024 * 1024) app.router.add_get("/health", health) + app.router.add_get("/ws", ws_handler) app.router.add_get("/ws/", ws_handler) app.router.add_get("/{tail:.*}", index) return app diff --git a/nginx/conf.d/edge.conf b/nginx/conf.d/edge.conf index de397f6..9d1a805 100644 --- a/nginx/conf.d/edge.conf +++ b/nginx/conf.d/edge.conf @@ -2,9 +2,18 @@ server { listen 80 default_server; server_name _; + + location = /health { + access_log off; + default_type text/plain; + return 200 'routekernel ok\n'; + } + # Port 8443 is exposed by the default development compose mapping. # For production 80/443, change this to https://$host$request_uri. - return 308 https://$host:8443$request_uri; + location / { + return 308 https://$host:8443$request_uri; + } } server { diff --git a/tests/test_project.py b/tests/test_project.py index 2de5ec4..e4e3753 100644 --- a/tests/test_project.py +++ b/tests/test_project.py @@ -41,6 +41,9 @@ def test_demo_app_role_routes(self): source = load('apps/demo/app.py') self.assertIn('async def ws_handler', source) self.assertIn('async def health', source) + self.assertIn('add_get("/ws", ws_handler)', source) + self.assertIn('add_get("/ws/", ws_handler)', source) + self.assertIn('WSMsgType.CLOSE', source) def test_security_headers_and_hardening(self): cfg = load('nginx/conf.d/edge.conf')