Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions apps/demo/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 10 additions & 1 deletion nginx/conf.d/edge.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions tests/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
Loading