From 3522885e1c52c5454c90cd1afe4fb08d18365614 Mon Sep 17 00:00:00 2001 From: berce <75228263+bercedev@users.noreply.github.com> Date: Wed, 21 Oct 2026 01:30:02 +0300 Subject: [PATCH] durable-objects: route on the action segment, not a substring of the path A list ID starting with "add" or "show" matched the wrong branch, and //add without a message raised IndexError. Split the path into list ID, action and message, compare the action exactly, and answer an add without a message with the usage text. Fixes #108. --- durable-objects/src/entry.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/durable-objects/src/entry.py b/durable-objects/src/entry.py index 505924f..945e9bf 100644 --- a/durable-objects/src/entry.py +++ b/durable-objects/src/entry.py @@ -20,23 +20,24 @@ class Default(WorkerEntrypoint): async def fetch(self, request): url = urlparse(request.url) - list_id = url.path.split("/")[1] - if list_id == "": - return Response( - "Go to //show to see messages\nGo to //add/ to add a message", - status=400, - ) + # //[/] + parts = url.path.split("/") + list_id = parts[1] + action = parts[2] if len(parts) > 2 else "" + message = parts[3] if len(parts) > 3 else "" + usage = "Go to //show to see messages\nGo to //add/ to add a message" + if list_id == "" or (action == "add" and message == ""): + return Response(usage, status=400) do_id = self.env.LISTS.idFromName(list_id) stub = self.env.LISTS.get(do_id) # Note: This is adding a message via GET URL path for simplicity, in a real app, use a POST - if "/add" in url.path: + if action == "add": # message = await request.text() - message = url.path.split("/")[3] await stub.add_message(message) return Response("Message sent") - elif "/show" in url.path: + elif action == "show": messages = await stub.get_messages() if not messages: return Response("No messages")