From d88a48a10a7411952a22ecf2c4cb8e100cff99e3 Mon Sep 17 00:00:00 2001 From: alihanisarr Date: Mon, 13 Jul 2026 02:40:54 +0500 Subject: [PATCH 1/6] added prometheus metrics --- docker-compose.yml | 12 ++++++++++++ requirements.txt | 1 - server.py | 1 + 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/docker-compose.yml b/docker-compose.yml index a625591..63bf446 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -6,3 +6,15 @@ services: ports: - "5000:5000" + prometheus: + image: prom/prometheus:latest + restart: always + volumes: + - ./prometheus.yml:/etc/prometheus/prometheus.yml + command: + - '--config.file=/etc/prometheus/prometheus.yml' + ports: + - "9090:9090" + + + diff --git a/requirements.txt b/requirements.txt index 6bd4692..fc1c223 100644 --- a/requirements.txt +++ b/requirements.txt @@ -18,4 +18,3 @@ typing_extensions==4.15.0 uvicorn==0.49.0 watchfiles==1.2.0 websockets==16.0 - diff --git a/server.py b/server.py index 4867a7e..fa3bb17 100644 --- a/server.py +++ b/server.py @@ -1,5 +1,6 @@ import collections import logging +import collections from fastapi import FastAPI, WebSocket, Request, HTTPException, Response import prometheus_client From 61e19ec775d944f7ff9272e9f6f015493d8cad9b Mon Sep 17 00:00:00 2001 From: evan Date: Sat, 25 Jul 2026 14:21:16 -0700 Subject: [PATCH 2/6] do rebase --- server.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server.py b/server.py index fa3bb17..2a0c447 100644 --- a/server.py +++ b/server.py @@ -46,7 +46,7 @@ async def webhook(subscription_id: str, request: Request): logger.error("Data sent to websocket client") return {"message":"received"} - + # something else: logger.error("Invalid subscription '%s', connection not accepted", subscription_id) return From 8cf687dbed5704dc9f50fdecc49b23bb898994d3 Mon Sep 17 00:00:00 2001 From: evan Date: Sat, 25 Jul 2026 14:21:57 -0700 Subject: [PATCH 3/6] remove prometheus container --- docker-compose.yml | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 63bf446..a625591 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -6,15 +6,3 @@ services: ports: - "5000:5000" - prometheus: - image: prom/prometheus:latest - restart: always - volumes: - - ./prometheus.yml:/etc/prometheus/prometheus.yml - command: - - '--config.file=/etc/prometheus/prometheus.yml' - ports: - - "9090:9090" - - - From 62bf751f60e3faf23c4a2745b8b203fd532b44da Mon Sep 17 00:00:00 2001 From: evan Date: Sat, 25 Jul 2026 15:00:42 -0700 Subject: [PATCH 4/6] finally block in websocket disconnect --- README.md | 17 ++++++++++++++--- server.py | 10 +++++++--- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index f79b7a1..d8c9d86 100644 --- a/README.md +++ b/README.md @@ -33,15 +33,26 @@ The server runs at `http://127.0.0.1:5000`. In one terminal, connect to the WebSocket tunnel: -```powershell -websocat ws://127.0.0.1:5000/tunnel +```sh +websocat ws://127.0.0.1:5000/tunnel/asdf + +# with an api key +websocat \ + --header="X-API-Key:hello" \ + - ws://127.0.0.1:5000/tunnel/asdf ``` In another terminal, send a webhook payload: ### bash ```sh -curl -X POST http://127.0.0.1:5000/webhook \ +curl -X POST http://127.0.0.1:5000/webhook/asdf \ + -H "Content-Type: application/json" \ + -d '{"message":"hello from webhook"}' + +# with an api key +curl -X POST http://127.0.0.1:5000/webhook/asdf \ + -H "X-API-Key: hello" \ -H "Content-Type: application/json" \ -d '{"message":"hello from webhook"}' ``` diff --git a/server.py b/server.py index 2a0c447..6a8532b 100644 --- a/server.py +++ b/server.py @@ -34,6 +34,7 @@ async def webhook(subscription_id: str, request: Request): if subscription_id is not None: header_val = request.headers.get("X-API-Key") if (header_val != "hello"): + logger.error('/webhook recieved invalid X-API-Key of "%s"', header_val) raise HTTPException(status_code=403, detail="API key is not valid ") data = await request.json() @@ -46,7 +47,6 @@ async def webhook(subscription_id: str, request: Request): logger.error("Data sent to websocket client") return {"message":"received"} - # something else: logger.error("Invalid subscription '%s', connection not accepted", subscription_id) return @@ -57,6 +57,7 @@ async def websocket_endpoint(subscription_id: str, websocket: WebSocket): api_key = websocket.headers.get("X-API-Key") if (api_key != "hello"): + logger.error('/tunnel recieved invalid X-API-Key of "%s"', api_key) MetricsHandler.failed_connections.labels( subscription_id=subscription_id, reason="bad_api_key", @@ -76,12 +77,14 @@ async def websocket_endpoint(subscription_id: str, websocket: WebSocket): while True: data = await websocket.receive_text() await websocket.send_text("Message received") - except Exception as e: + except Exception: + logger.exception('ok') MetricsHandler.failed_connections.labels( subscription_id=subscription_id, reason="websocket_receive_failed", ).inc() MetricsHandler.connected_clients.labels(subscription_id).dec() + finally: clients[subscription_id].remove(websocket) if not clients[subscription_id]: clients.pop(subscription_id, None) @@ -104,8 +107,9 @@ def get_metrics(): # metrics_handler referenced by the rest of the file. otherwise, # the thread interacts with an instance different than the one the # server uses +logger.error("!!!!!`") if __name__ == "server": MetricsHandler.init() if __name__ == "__main__": - uvicorn.run("server:app", host="0.0.0.0", port=5000) + uvicorn.run("server:app", host="0.0.0.0", port=5000, reload=True, timeout_graceful_shutdown=1) From 8e2356ed0d27a2757e187829201fb2a5616cd442 Mon Sep 17 00:00:00 2001 From: evan Date: Sat, 25 Jul 2026 15:01:59 -0700 Subject: [PATCH 5/6] add sce network to docker compose yml --- docker-compose.yml | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index a625591..91f5124 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -3,6 +3,8 @@ services: build: context: . dockerfile: ./Dockerfile - ports: - - "5000:5000" +networks: + default: + external: + name: sce From 770187cc3e5ed13d4480aa951fb7f0fb5b137355 Mon Sep 17 00:00:00 2001 From: evan Date: Sat, 25 Jul 2026 15:30:04 -0700 Subject: [PATCH 6/6] add sce.sjsu.edu section --- README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/README.md b/README.md index d8c9d86..2d2fad1 100644 --- a/README.md +++ b/README.md @@ -63,3 +63,15 @@ Invoke-RestMethod -Method Post -Uri http://127.0.0.1:5000/webhook -ContentType " ``` The connected WebSocket client should receive the JSON payload. + +## on the real website +```sh +curl -X POST https://sce.sjsu.edu/webhook/asdf \ + -H "X-API-Key: hello" \ + -H "Content-Type: application/json" \ + -d '{"message":"hello from webhook"}' + +websocat \ + --header="X-API-Key:hello" \ + - ws://sce.sjsu.edu/tunnel/asdf +```