Skip to content

Commit 51000e6

Browse files
authored
durable-objects: route on the action segment, not a substring of the path (#110)
1 parent 38cc174 commit 51000e6

1 file changed

Lines changed: 10 additions & 9 deletions

File tree

‎durable-objects/src/entry.py‎

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,24 @@ class Default(WorkerEntrypoint):
2020
async def fetch(self, request):
2121
url = urlparse(request.url)
2222

23-
list_id = url.path.split("/")[1]
24-
if list_id == "":
25-
return Response(
26-
"Go to /<list_id>/show to see messages\nGo to /<list_id>/add/<message> to add a message",
27-
status=400,
28-
)
23+
# /<list_id>/<action>[/<message>]
24+
parts = url.path.split("/")
25+
list_id = parts[1]
26+
action = parts[2] if len(parts) > 2 else ""
27+
message = parts[3] if len(parts) > 3 else ""
28+
usage = "Go to /<list_id>/show to see messages\nGo to /<list_id>/add/<message> to add a message"
29+
if list_id == "" or (action == "add" and message == ""):
30+
return Response(usage, status=400)
2931

3032
do_id = self.env.LISTS.idFromName(list_id)
3133
stub = self.env.LISTS.get(do_id)
3234

3335
# Note: This is adding a message via GET URL path for simplicity, in a real app, use a POST
34-
if "/add" in url.path:
36+
if action == "add":
3537
# message = await request.text()
36-
message = url.path.split("/")[3]
3738
await stub.add_message(message)
3839
return Response("Message sent")
39-
elif "/show" in url.path:
40+
elif action == "show":
4041
messages = await stub.get_messages()
4142
if not messages:
4243
return Response("No messages")

0 commit comments

Comments
 (0)