Skip to content
Merged
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
6 changes: 6 additions & 0 deletions .changeset/loud-boxes-grow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"create-cloudflare": minor
"miniflare": minor
---

Use new default entrypoint handlers for Python examples
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from workers import DurableObject, Response, handler
from workers import DurableObject, Response, WorkerEntrypoint

"""
* Welcome to Cloudflare Workers! This is your first Durable Objects application.
Expand Down Expand Up @@ -48,20 +48,20 @@ async def say_hello(self, name):
* @param {ExecutionContext} ctx - The execution context of the Worker
* @returns {Promise<Response>} The response to be sent back to the client
"""
@handler
async def on_fetch(request, env, ctx):
# Create a `DurableObjectId` for an instance of the `MyDurableObject`
# class named "foo". Requests from all Workers to the instance named
# "foo" will go to a single globally unique Durable Object instance.
id = env.MY_DURABLE_OBJECT.idFromName("foo")
class Default(WorkerEntrypoint):
async def fetch(self, request):
# Create a `DurableObjectId` for an instance of the `MyDurableObject`
# class named "foo". Requests from all Workers to the instance named
# "foo" will go to a single globally unique Durable Object instance.
id = self.env.MY_DURABLE_OBJECT.idFromName("foo")

# Create a stub to open a communication channel with the Durable
# Object instance.
stub = env.MY_DURABLE_OBJECT.get(id)
# Create a stub to open a communication channel with the Durable
# Object instance.
stub = self.env.MY_DURABLE_OBJECT.get(id)

# Call the `say_hello()` RPC method on the stub to invoke the method on
# the remote Durable Object instance
greeting = await stub.say_hello("world")
# Call the `say_hello()` RPC method on the stub to invoke the method on
# the remote Durable Object instance
greeting = await stub.say_hello("world")

return Response(greeting)
return Response(greeting)

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from workers import DurableObject, Response, handler
from workers import DurableObject, Response, WorkerEntrypoint

"""
* Welcome to Cloudflare Workers! This is your first Durable Objects application.
Expand Down Expand Up @@ -40,28 +40,28 @@ async def say_hello(self, name):
return f"Hello, {name}!"


"""
* This is the standard fetch handler for a Cloudflare Worker
*
* @param {Request} request - The request submitted to the Worker from the client
* @param {Env} env - The interface to reference bindings declared in wrangler.jsonc
* @param {ExecutionContext} ctx - The execution context of the Worker
* @returns {Promise<Response>} The response to be sent back to the client
"""
@handler
async def on_fetch(request, env, ctx):
# Create a `DurableObjectId` for an instance of the `MyDurableObject`
# class named "foo". Requests from all Workers to the instance named
# "foo" will go to a single globally unique Durable Object instance.
id = env.MY_DURABLE_OBJECT.idFromName("foo")
class Default(WorkerEntrypoint):
"""
* This is the standard fetch handler for a Cloudflare Worker
*
* @param {Request} request - The request submitted to the Worker from the client
* @param {Env} env - The interface to reference bindings declared in wrangler.jsonc
* @param {ExecutionContext} ctx - The execution context of the Worker
* @returns {Promise<Response>} The response to be sent back to the client
"""
async def fetch(self, request):
# Create a `DurableObjectId` for an instance of the `MyDurableObject`
# class named "foo". Requests from all Workers to the instance named
# "foo" will go to a single globally unique Durable Object instance.
id = self.env.MY_DURABLE_OBJECT.idFromName("foo")

# Create a stub to open a communication channel with the Durable
# Object instance.
stub = env.MY_DURABLE_OBJECT.get(id)
# Create a stub to open a communication channel with the Durable
# Object instance.
stub = self.env.MY_DURABLE_OBJECT.get(id)

# Call the `say_hello()` RPC method on the stub to invoke the method on
# the remote Durable Object instance
greeting = await stub.say_hello("world")
# Call the `say_hello()` RPC method on the stub to invoke the method on
# the remote Durable Object instance
greeting = await stub.say_hello("world")

return Response(greeting)
return Response(greeting)

Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from workers import Response, handler
from workers import Response, WorkerEntrypoint
from uuid import uuid4
from urllib.parse import urlparse

@handler
async def on_fetch(request, env):
url = urlparse(request.url)
if url.path == '/message':
return Response('Hello, World!')
if url.path == '/random':
return Response(uuid4())
return Response('Not Found', status=404)
class Default(WorkerEntrypoint):
async def fetch(self, request):
url = urlparse(request.url)
if url.path == '/message':
return Response('Hello, World!')
if url.path == '/random':
return Response(uuid4())
return Response('Not Found', status=404)
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from workers import Response, handler
from workers import Response, WorkerEntrypoint

@handler
async def on_fetch(request, env):
return Response("Hello World!")
class Default(WorkerEntrypoint):
async def fetch(self, request):
return Response("Hello World!")
4 changes: 2 additions & 2 deletions packages/miniflare/test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1386,15 +1386,15 @@ test("Miniflare: python modules", async (t) => {
type: "PythonModule",
path: "index.py",
contents:
"from test_module import add; from js import Response;\ndef on_fetch(request):\n return Response.new(add(2,2))",
"from test_module import add; from workers import Response, WorkerEntrypoint;\nclass Default(WorkerEntrypoint):\n def fetch(self, request):\n return Response(str(add(2,2)))",
},
{
type: "PythonModule",
path: "test_module.py",
contents: `def add(a, b):\n return a + b`,
},
],
compatibilityFlags: ["python_workers"],
compatibilityFlags: ["python_workers", "python_no_global_handlers"],
});
t.teardown(() => mf.dispose());
const res = await mf.dispatchFetch("http://localhost");
Expand Down
Loading
Loading