Skip to content

Commit 65030ab

Browse files
committed
Update templates to use default entrypoint handlers for Python.
1 parent 0c04da9 commit 65030ab

File tree

5 files changed

+51
-51
lines changed

5 files changed

+51
-51
lines changed

packages/create-cloudflare/templates/hello-world-durable-object-with-assets/py/src/entry.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from workers import DurableObject, Response, handler
1+
from workers import DurableObject, Response, WorkerEntrypoint
22

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

58-
# Create a stub to open a communication channel with the Durable
59-
# Object instance.
60-
stub = env.MY_DURABLE_OBJECT.get(id)
58+
# Create a stub to open a communication channel with the Durable
59+
# Object instance.
60+
stub = env.MY_DURABLE_OBJECT.get(id)
6161

62-
# Call the `say_hello()` RPC method on the stub to invoke the method on
63-
# the remote Durable Object instance
64-
greeting = await stub.say_hello("world")
62+
# Call the `say_hello()` RPC method on the stub to invoke the method on
63+
# the remote Durable Object instance
64+
greeting = await stub.say_hello("world")
6565

66-
return Response(greeting)
66+
return Response(greeting)
6767

packages/create-cloudflare/templates/hello-world-durable-object/py/src/entry.py

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from workers import DurableObject, Response, handler
1+
from workers import DurableObject, Response, WorkerEntrypoint
22

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

4242

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

58-
# Create a stub to open a communication channel with the Durable
59-
# Object instance.
60-
stub = env.MY_DURABLE_OBJECT.get(id)
58+
# Create a stub to open a communication channel with the Durable
59+
# Object instance.
60+
stub = env.MY_DURABLE_OBJECT.get(id)
6161

62-
# Call the `say_hello()` RPC method on the stub to invoke the method on
63-
# the remote Durable Object instance
64-
greeting = await stub.say_hello("world")
62+
# Call the `say_hello()` RPC method on the stub to invoke the method on
63+
# the remote Durable Object instance
64+
greeting = await stub.say_hello("world")
6565

66-
return Response(greeting)
66+
return Response(greeting)
6767

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
from workers import Response, handler
1+
from workers import Response, WorkerEntrypoint
22
from uuid import uuid4
33
from urllib.parse import urlparse
44

5-
@handler
6-
async def on_fetch(request, env):
7-
url = urlparse(request.url)
8-
if url.path == '/message':
9-
return Response('Hello, World!')
10-
if url.path == '/random':
11-
return Response(uuid4())
12-
return Response('Not Found', status=404)
5+
class Default(WorkerEntrypoint):
6+
async def fetch(self, request, env):
7+
url = urlparse(request.url)
8+
if url.path == '/message':
9+
return Response('Hello, World!')
10+
if url.path == '/random':
11+
return Response(uuid4())
12+
return Response('Not Found', status=404)
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
from workers import Response, handler
1+
from workers import Response, WorkerEntrypoint
22

3-
@handler
4-
async def on_fetch(request, env):
5-
return Response("Hello World!")
3+
class Default(WorkerEntrypoint):
4+
async def fetch(self, request, env):
5+
return Response("Hello World!")

packages/miniflare/test/index.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1386,15 +1386,15 @@ test("Miniflare: python modules", async (t) => {
13861386
type: "PythonModule",
13871387
path: "index.py",
13881388
contents:
1389-
"from test_module import add; from js import Response;\ndef on_fetch(request):\n return Response.new(add(2,2))",
1389+
"from test_module import add; from workers import Response, WorkerEntrypoint;\nclass Default(WorkerEntrypoint):\n def fetch(self, request):\n return Response(add(2,2))",
13901390
},
13911391
{
13921392
type: "PythonModule",
13931393
path: "test_module.py",
13941394
contents: `def add(a, b):\n return a + b`,
13951395
},
13961396
],
1397-
compatibilityFlags: ["python_workers"],
1397+
compatibilityFlags: ["python_workers", "python_no_global_handlers"],
13981398
});
13991399
t.teardown(() => mf.dispose());
14001400
const res = await mf.dispatchFetch("http://localhost");

0 commit comments

Comments
 (0)