| 1 |
#!/usr/bin/env python |
| 2 |
|
| 3 |
import asyncio |
| 4 |
import http |
| 5 |
import os |
| 6 |
import signal |
| 7 |
|
| 8 |
from websockets.asyncio.server import serve |
| 9 |
|
| 10 |
|
| 11 |
async def echo(websocket): |
| 12 |
async for message in websocket: |
| 13 |
await websocket.send(message) |
| 14 |
|
| 15 |
|
| 16 |
def health_check(connection, request): |
| 17 |
if request.path == "/healthz": |
| 18 |
return connection.respond(http.HTTPStatus.OK, "OK\n") |
| 19 |
|
| 20 |
|
| 21 |
async def main(): |
| 22 |
port = int(os.environ["PORT"]) |
| 23 |
async with serve(echo, "", port, process_request=health_check) as server: |
| 24 |
loop = asyncio.get_running_loop() |
| 25 |
loop.add_signal_handler(signal.SIGTERM, server.close) |
| 26 |
await server.wait_closed() |
| 27 |
|
| 28 |
|
| 29 |
if __name__ == "__main__": |
| 30 |
asyncio.run(main()) |