From ed6d5c0dd577a11c2fc2effbeba5e7e19c682d60 Mon Sep 17 00:00:00 2001 From: James M Snell Date: Mon, 19 Sep 2022 12:59:22 -0700 Subject: [PATCH 1/2] Add simple hello world sample --- samples/helloworld/README.md | 28 ++++++++++++++++++++++++++++ samples/helloworld/config.capnp | 32 ++++++++++++++++++++++++++++++++ samples/helloworld/worker.js | 7 +++++++ 3 files changed, 67 insertions(+) create mode 100644 samples/helloworld/README.md create mode 100644 samples/helloworld/config.capnp create mode 100644 samples/helloworld/worker.js diff --git a/samples/helloworld/README.md b/samples/helloworld/README.md new file mode 100644 index 00000000000..a4f6f8bcdf0 --- /dev/null +++ b/samples/helloworld/README.md @@ -0,0 +1,28 @@ +# Hello World Example + +To run the example on http://localhost:8080 + +```sh +$ ./workerd serve config.capnp +``` + +To run using bazel + +```sh +$ bazel run //src/workerd/server:workerd -- serve ~/cloudflare/workerd/samples/helloworld/config.capnp +``` + +To create a standalone binary that can be run: + +```sh +$ ./workerd compile config.capnp > helloworld + +$ ./helloworld +``` + +To test: + +```sh +% curl http://localhost:8080 +Hello World +``` diff --git a/samples/helloworld/config.capnp b/samples/helloworld/config.capnp new file mode 100644 index 00000000000..41b38bc938c --- /dev/null +++ b/samples/helloworld/config.capnp @@ -0,0 +1,32 @@ +# Imports the base schema for workerd configuration files. + +# Refer to the comments in /src/workerd/server/workerd.capnp for more details. + +using Workerd = import "/workerd/workerd.capnp"; + +# A constant of type Workerd.Config defines the top-level configuration for an +# instance of the workerd runtime. A single config file can contain multiple +# Workerd.Config definitions and must have at least one. +const helloWorldExample :Workerd.Config = ( + + # Every workerd instance consists of a set of named services. A worker, for instance, + # is a type of service. Other types of services can include external servers, the + # ability to talk to a network, or accessing a disk directory. Here we create a single + # worker service. The configuration details for the worker are defined below. + services = [ (name = "main", worker = .helloWorld) ], + + # Every configuration defines the one or more sockets on which the server will listene. + # Here, we create a single socket that will listen on localhost port 8080, and will + # dispatch to the "main" service that we defined above. + sockets = [ ( name = "http", address = "*:8080", http = (), service = "main" ) ] +); + +# The definition of the actual helloWorld worker exposed using the "main" service. +# In this example the worker is implemented as a single simple script (see worker.js). +# The compatibilityDate is required. For more details on compatibility dates see: +# https://developers.cloudflare.com/workers/platform/compatibility-dates/ + +const helloWorld :Workerd.Worker = ( + serviceWorkerScript = embed "worker.js", + compatibilityDate = "2022-09-16", +); diff --git a/samples/helloworld/worker.js b/samples/helloworld/worker.js new file mode 100644 index 00000000000..d1824930db3 --- /dev/null +++ b/samples/helloworld/worker.js @@ -0,0 +1,7 @@ +addEventListener('fetch', event => { + event.respondWith(handle(event.request)); +}); + +async function handle(request) { + return new Response("Hello World\n"); +} From 79c7f638982f2fa0436bcd43dfe467ffadc0257a Mon Sep 17 00:00:00 2001 From: James M Snell Date: Mon, 19 Sep 2022 14:24:36 -0700 Subject: [PATCH 2/2] Add ESM version of the hello world example --- samples/helloworld_esm/README.md | 28 ++++++++++++++++++++++++ samples/helloworld_esm/config.capnp | 34 +++++++++++++++++++++++++++++ samples/helloworld_esm/worker.js | 5 +++++ 3 files changed, 67 insertions(+) create mode 100644 samples/helloworld_esm/README.md create mode 100644 samples/helloworld_esm/config.capnp create mode 100644 samples/helloworld_esm/worker.js diff --git a/samples/helloworld_esm/README.md b/samples/helloworld_esm/README.md new file mode 100644 index 00000000000..2f54c1e5baf --- /dev/null +++ b/samples/helloworld_esm/README.md @@ -0,0 +1,28 @@ +# Hello World Example + +To run the example on http://localhost:8080 + +```sh +$ ./workerd serve config.capnp +``` + +To run using bazel + +```sh +$ bazel run //src/workerd/server:workerd -- serve ~/cloudflare/workerd/samples/helloworld_esm/config.capnp +``` + +To create a standalone binary that can be run: + +```sh +$ ./workerd compile config.capnp > helloworld + +$ ./helloworld +``` + +To test: + +```sh +% curl http://localhost:8080 +Hello World +``` diff --git a/samples/helloworld_esm/config.capnp b/samples/helloworld_esm/config.capnp new file mode 100644 index 00000000000..5b34496dd4d --- /dev/null +++ b/samples/helloworld_esm/config.capnp @@ -0,0 +1,34 @@ +# Imports the base schema for workerd configuration files. + +# Refer to the comments in /src/workerd/server/workerd.capnp for more details. + +using Workerd = import "/workerd/workerd.capnp"; + +# A constant of type Workerd.Config defines the top-level configuration for an +# instance of the workerd runtime. A single config file can contain multiple +# Workerd.Config definitions and must have at least one. +const helloWorldExample :Workerd.Config = ( + + # Every workerd instance consists of a set of named services. A worker, for instance, + # is a type of service. Other types of services can include external servers, the + # ability to talk to a network, or accessing a disk directory. Here we create a single + # worker service. The configuration details for the worker are defined below. + services = [ (name = "main", worker = .helloWorld) ], + + # Every configuration defines the one or more sockets on which the server will listene. + # Here, we create a single socket that will listen on localhost port 8080, and will + # dispatch to the "main" service that we defined above. + sockets = [ ( name = "http", address = "*:8080", http = (), service = "main" ) ] +); + +# The definition of the actual helloWorld worker exposed using the "main" service. +# In this example the worker is implemented as an ESM module (see worker.js). +# The compatibilityDate is required. For more details on compatibility dates see: +# https://developers.cloudflare.com/workers/platform/compatibility-dates/ + +const helloWorld :Workerd.Worker = ( + modules = [ + (name = "worker", esModule = embed "worker.js") + ], + compatibilityDate = "2022-09-16", +); diff --git a/samples/helloworld_esm/worker.js b/samples/helloworld_esm/worker.js new file mode 100644 index 00000000000..3088e0e728c --- /dev/null +++ b/samples/helloworld_esm/worker.js @@ -0,0 +1,5 @@ +export default { + async fetch(req, env) { + return new Response("Hello World\n"); + } +};