Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions durable-objects/src/entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 /<list_id>/show to see messages\nGo to /<list_id>/add/<message> to add a message",
status=400,
)
# /<list_id>/<action>[/<message>]
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 /<list_id>/show to see messages\nGo to /<list_id>/add/<message> 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")
Expand Down
Loading