Skip to content
Closed
Show file tree
Hide file tree
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
10 changes: 6 additions & 4 deletions durable-objects/src/entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ class Default(WorkerEntrypoint):
async def fetch(self, request):
url = urlparse(request.url)

list_id = url.path.split("/")[1]
segments = url.path.split("/")
list_id = segments[1]
if list_id == "":
return Response(
"Go to /<list_id>/show to see messages\nGo to /<list_id>/add/<message> to add a message",
Expand All @@ -31,12 +32,13 @@ async def fetch(self, request):
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:
action = segments[2] if len(segments) > 2 else ""
if action == "add" and len(segments) > 3:
# message = await request.text()
message = url.path.split("/")[3]
message = segments[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
5 changes: 5 additions & 0 deletions tests/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ def test_durable_objects(dev_server):
response = requests.get(f"http://localhost:{port}/room-2/show")
assert response.status_code == 200
assert response.text == "No messages"
response = requests.get(f"http://localhost:{port}/address-book/show")
assert response.status_code == 200
assert response.text == "No messages"
response = requests.get(f"http://localhost:{port}/room-1/add")
assert response.status_code == 404


def test_sync_http_clients(dev_server):
Expand Down
Loading