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")