Skip to content

Commit

Permalink
feat: rename attr macro handler to worker (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
Angelmmiguel authored Nov 29, 2022
1 parent 90e11f6 commit dcec209
Show file tree
Hide file tree
Showing 30 changed files with 136 additions and 130 deletions.
28 changes: 14 additions & 14 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ clap = { version = "4.0.10", features = ["derive"] }
[workspace]
members = [
"kits/rust",
"kits/rust/handler",
"kits/rust/worker",
"kits/javascript"
]
# Exclude examples
Expand Down
26 changes: 13 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Wasm Workers Server

Wasm Workers Server (`wws`) is an HTTP server that runs applications with WebAssembly. These applications are composed by multiple modules, called "handlers" or "functions". Each of these modules is in charge of replying to a specific HTTP path in your application.
Wasm Workers Server (`wws`) is an HTTP server that runs applications with WebAssembly. These applications are composed by multiple modules, called "workers" or "handlers". Each of these modules is in charge of replying to a specific HTTP path in your application.

The server loads the existing Wasm modules and compatible languages in the current path. The filenames and folders determine the final routes that will be served. This is called "filesystem routing" and is a popular technique. Successful frameworks such as [NextJS](https://nextjs.org/) and [Eleventy](https://www.11ty.dev/) work in this way.

Expand All @@ -14,9 +14,9 @@ $ wws .
⚙️ Loading routes from: .
🗺 Detected routes:
- http://127.0.0.1:8080/
=> index.wasm (handler: default)
=> index.wasm (name: default)
- http://127.0.0.1:8080/api/hello
=> api/hello.js (handler: default)
=> api/hello.js (name: default)
🚀 Start serving requests at http://127.0.0.1:8080
```

Expand Down Expand Up @@ -58,7 +58,7 @@ The server will start immediately:
⚙️ Loading routes from: .
🗺 Detected routes:
- http://127.0.0.1:8080/
=> index.js (handler: default)
=> index.js (name: default)
🚀 Start serving requests at http://127.0.0.1:8080
```

Expand All @@ -77,10 +77,10 @@ curl https://raw.githubusercontent.com/vmware-labs/wasm-workers-server/main/inst

If you don't want to install anything locally you can just run `wws` from the `projects.registry.vmware.com/wasmlabs/containers/wasm-workers-server:latest` container image. All you need to do is:

- map a local folder with handlers to `/app` within the container
- expose port `8080` from the container
- Map a local folder with workers to `/app` within the container
- Expose port `8080` from the container

Here is how to quickly run a container with an ad-hoc handler from the /tmp/wws-app folder:
Here is how to quickly run a container with an ad-hoc worker from the /tmp/wws-app folder:

```bash
mkdir /tmp/wws-app 2>/dev/null;
Expand All @@ -89,7 +89,7 @@ docker run --rm -v /tmp/wws-app:/app -p 8080:8080 projects.registry.vmware.com/w
```
## Language Support

Wasm Workers Server focuses on simplicity. We want you to run handlers (written in different languages) safely in WebAssembly. For interpreted languages, we add different interpreters:
Wasm Workers Server focuses on simplicity. We want you to run workers (written in different languages) safely in WebAssembly. For interpreted languages, we add different interpreters:

| Language | Wasm module | Interpreter |
| --- | --- | --- |
Expand All @@ -99,11 +99,11 @@ Wasm Workers Server focuses on simplicity. We want you to run handlers (written

We will include more interpreters in the future.

### JavaScript handlers
### JavaScript Workers

The integrated interpreter is based on [QuickJS](https://bellard.org/quickjs/) (compiled with the [quickjs-wasm-rs](https://crates.io/crates/quickjs-wasm-rs) crate). The compatible handlers follow the Web Workers API approach. However, not all the Web Workers API is available in these handlers. These are some of the missing features:
The integrated interpreter is based on [QuickJS](https://bellard.org/quickjs/) (compiled with the [quickjs-wasm-rs](https://crates.io/crates/quickjs-wasm-rs) crate). The compatible workers follow the Web Workers API approach. However, not all the Web Workers API is available in these workers. These are some of the missing features:

- No modules available. Handlers must be a single file
- No modules available. Workers must be a single file
- Fetch API
- Async / Await

Expand All @@ -126,9 +126,9 @@ After installing the different prerequisites, you can run the development enviro
$ cargo run -- --help
```

This command will run the server and look for `.wasm` and compatible modules (like `.js`) in the folder you pass via arguments. Check the [examples](./examples/) folder to get more information about creating Wasm handlers.
This command will run the server and look for `.wasm` and compatible modules (like `.js`) in the folder you pass via arguments. Check the [examples](./examples/) folder to get more information about creating Wasm workers.

### Documentation

* `src`: includes the source code for the Wasm Workers Server project
* `examples`: folder to generate different example handlers. Check the README file inside to get more information about how to build those
* `examples`: folder to generate different example workers. Check the README file inside to get more information about how to build those
6 changes: 3 additions & 3 deletions docs/docs/containers.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ sidebar_position: 3

For convenience we have published a container image that contains Wasm Workers Server. It is available at `projects.registry.vmware.com/wasmlabs/containers/wasm-workers-server:latest`. Any container that runs it will get the `wws` binary, running and:

- looking for worker handlers in the `/app` folder
- listening on `0.0.0.0:8080` inside the container
- Looking for workers in the `/app` folder
- Listening on `0.0.0.0:8080` inside the container

The image is based on `debian:bullseye-slim` + the `wws` binary. It includes support for the `linux/amd64` and `linux/arm64/v8` platforms. The image size should be around `~100MiB`

Expand All @@ -16,7 +16,7 @@ The image is based on `debian:bullseye-slim` + the `wws` binary. It includes sup
A typical one-liner to run a local container for development purposes would look like:

```bash
docker run -v /path/to/handlers/on/host:/app -p 8080:8080 \
docker run -v /path/to/workers/on/host:/app -p 8080:8080 \
projects.registry.vmware.com/wasmlabs/containers/wasm-workers-server:latest
```

Expand Down
12 changes: 6 additions & 6 deletions docs/docs/how-it-works.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ sidebar_position: 4

Wasm Workers is built around two main ideas:

* **Handlers receive requests and return responses**.
* **Workers receive requests and return responses**.

We follow this approach as it's a widely used pattern for creating serverless functions. Following this pattern helps us to keep compatibility with multiple platforms and avoid vendor-locking on our own tool.
We follow this approach as it's a widely used pattern for creating serverless functions. Following this pattern helps us to keep compatibility with multiple platforms and avoid vendor-locking on our own tool.

* **Handlers receive and return data via WASI Stdio**.
* **Workers receive and return data via WASI Stdio**.

To increase compatibility and simplify the integration with existing languages, we decided to send and receive data using `STDIN` / `STDOUT`. So, any language that can be compiled using WASI standard can create compatible handlers with Wasm Workers.
To increase compatibility and simplify the integration with existing languages, we decided to send and receive data using `STDIN` / `STDOUT`. So, any language that can be compiled using WASI standard can create compatible workers with Wasm Workers.

## Runner

Expand All @@ -29,7 +29,7 @@ Based on these two principles, the server performs the following tasks:

Wasm Workers assume the HTTP routes from the filesystem. This approach is pretty similar to other very successful projects like NextJS. This simplifies the server interface by running without adding any configuring file.

For extra features such as the Key / Value store, you need to write a configuration file. By default, Wasm Workers doesn't enable any extra feature to any worker. This is an example configuration file to enable the Key / Value store for a handler:
For extra features such as the Key / Value store, you need to write a configuration file. By default, Wasm Workers doesn't enable any extra feature to any worker. This is an example configuration file to enable the Key / Value store for a worker:

```toml title="./counter.toml"
name = "counter"
Expand All @@ -40,4 +40,4 @@ version = "1"
namespace = "counter"
```

These files are only required to enable extra features for your handlers.
These files are only required to enable extra features for your workers.
8 changes: 4 additions & 4 deletions docs/docs/start.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,17 @@ wws .
⚙️ Loading routes from: ./examples
🗺 Detected routes:
- http://127.0.0.1:8080/
=> index.js (handler: default)
=> index.js (name: default)
🚀 Start serving requests at http://127.0.0.1:8080
```

Now, open your browser at <http://127.0.0.1:8080>.

## Next Steps

Now you got the taste of Wasm Workers, it's time to create your first handler:
Now you got the taste of Wasm Workers, it's time to create your first worker:

* [Create your first JavaScript handler](./tutorials/javascript-workers.md)
* [Create your first Rust handler](./tutorials/rust-workers.md)
* [Create your first JavaScript worker](./tutorials/javascript-workers.md)
* [Create your first Rust worker](./tutorials/rust-workers.md)

And if you are curious, here you have a guide about [how it works](./how-it-works.md).
6 changes: 3 additions & 3 deletions docs/docs/tutorials/javascript-workers.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ In this example, the worker will get a request and print all the related informa
⚙️ Loading routes from: .
🗺 Detected routes:
- http://127.0.0.1:8080/
=> index.js (handler: default)
=> index.js (name: default)
🚀 Start serving requests at http://127.0.0.1:8080
```
Expand Down Expand Up @@ -96,7 +96,7 @@ To add a KV store to your worker, follow these steps:
});
```
1. Create a `counter.toml` file with the following content. Note the name of the TOML file must match the name of the handler. In this case we have `counter.js` and `counter.toml` in the same folder:
1. Create a `counter.toml` file with the following content. Note the name of the TOML file must match the name of the worker. In this case we have `counter.js` and `counter.toml` in the same folder:
```toml title="./counter.toml"
name = "counter"
Expand All @@ -115,7 +115,7 @@ To add a KV store to your worker, follow these steps:
⚙️ Loading routes from: .
🗺 Detected routes:
- http://127.0.0.1:8080/counter
=> counter.js (handler: default)
=> counter.js (name: default)
🚀 Start serving requests at http://127.0.0.1:8080
```

Expand Down
32 changes: 16 additions & 16 deletions docs/docs/tutorials/rust-workers.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Then, they are loaded by Wasm Workers Server and start processing requests.

## Your first worker

Every worker receives a [Request<String\>](https://docs.rs/http/0.2.8/http/request/struct.Request.html) struct and returns a [Response<Content\>](https://docs.rs/http/0.2.8/http/response/struct.Response.html). These structs come from the widely known [`http` crate](https://docs.rs/http/) and the `Content` struct is defined in our rust kit. It allows you returning different types. Finally, the `handler` macro connects your worker with `wws`.
Every worker receives a [Request<String\>](https://docs.rs/http/0.2.8/http/request/struct.Request.html) struct and returns a [Response<Content\>](https://docs.rs/http/0.2.8/http/response/struct.Response.html). These structs come from the widely known [`http` crate](https://docs.rs/http/) and the `Content` struct is defined in our rust kit. It allows you returning different types. Finally, the `worker` macro connects your worker with `wws`.

In this example, the worker will get a request and print all the related information.

Expand All @@ -32,17 +32,17 @@ In this example, the worker will get a request and print all the related informa
wasm-workers-rs = { git = "https://github.com/vmware-labs/wasm-workers-server/" }
```
1. Add the `reply` function to the `src/main.rs` file. You will need to import the required resources from the `wasm-workers-rs` crate and use the `handler` macro:
1. Add the `reply` function to the `src/main.rs` file. You will need to import the required resources from the `wasm-workers-rs` crate and use the `worker` macro:
```rust title="src/main.rs"
use anyhow::Result;
use wasm_workers_rs::{
handler,
worker,
http::{self, Request, Response},
Content,
};
#[handler]
#[worker]
fn reply(req: Request<String>) -> Result<Response<Content>> {
Ok(http::Response::builder()
.status(200)
Expand All @@ -56,11 +56,11 @@ In this example, the worker will get a request and print all the related informa
```rust title="src/main.rs"
use anyhow::Result;
use wasm_workers_rs::{
handler,
worker,
http::{self, HeaderValue, Request, Response},
};
#[handler]
#[worker]
fn reply(req: Request<String>) -> Result<Response<String>> {
// Applied changes here to use the Response method. This requires changes
// on signature and how it returns the data.
Expand Down Expand Up @@ -108,7 +108,7 @@ In this example, the worker will get a request and print all the related informa
⚙️ Loading routes from: .
🗺 Detected routes:
- http://127.0.0.1:8080/worker
=> worker.wasm (handler: default)
=> worker.wasm (name: default)
🚀 Start serving requests at http://127.0.0.1:8080
```
Expand Down Expand Up @@ -139,17 +139,17 @@ To add a KV store to your worker, follow these steps:
wasm-workers-rs = { git = "https://github.com/vmware-labs/wasm-workers-server/" }
```
1. Add the `reply` function to the `src/main.rs` file. You will need to import the required resources from the `wasm-workers-rs` crate and use the `handler` macro. In this case, we will add a new attribute to the `handler` macro called `cache` and update the function signature:
1. Add the `reply` function to the `src/main.rs` file. You will need to import the required resources from the `wasm-workers-rs` crate and use the `worker` macro. In this case, we will add a new attribute to the `worker` macro called `cache` and update the function signature:
```rust title="src/main.rs"
use anyhow::Result;
use wasm_workers_rs::{
handler,
worker,
http::{self, Request, Response},
Content,
};
#[handler(cache)]
#[worker(cache)]
fn handler(_req: Request<String>, cache: &mut Cache) -> Result<Response<Content>> {
Ok(http::Response::builder()
.status(200)
Expand All @@ -163,12 +163,12 @@ To add a KV store to your worker, follow these steps:
```rust title="src/main.rs"
use anyhow::Result;
use wasm_workers_rs::{
handler,
worker,
http::{self, Request, Response},
Cache, Content,
};
#[handler(cache)]
#[worker(cache)]
fn handler(_req: Request<String>, cache: &mut Cache) -> Result<Response<Content>> {
// Applied changes here to use the Response method. This requires changes
// on signature and how it returns the data.
Expand Down Expand Up @@ -205,7 +205,7 @@ To add a KV store to your worker, follow these steps:
cargo build --release --target wasm32-wasi
```
1. Create a `worker-kv.toml` file with the following content. Note the name of the TOML file must match the name of the handler. In this case we have `worker-kv.wasm` and `worker-kv.toml` in the same folder (`target/wasm32-wasi/release`):
1. Create a `worker-kv.toml` file with the following content. Note the name of the TOML file must match the name of the worker. In this case we have `worker-kv.wasm` and `worker-kv.toml` in the same folder (`target/wasm32-wasi/release`):
```toml title="target/wasm32-wasi/release/worker-kv.toml"
name = "workerkv"
Expand All @@ -225,7 +225,7 @@ To add a KV store to your worker, follow these steps:
⚙️ Loading routes from: .
🗺 Detected routes:
- http://127.0.0.1:8080/worker-kv
=> worker-kv.wasm (handler: default)
=> worker-kv.wasm (name: default)
🚀 Start serving requests at http://127.0.0.1:8080
```
Expand All @@ -249,12 +249,12 @@ Now, you can read the `MESSAGE` variable using the [`std::env` Rust library](htt
use anyhow::Result;
use std::env;
use wasm_workers_rs::{
handler,
worker,
http::{self, Request, Response},
Content,
};
#[handler]
#[worker]
fn handler(req: Request<String>) -> Result<Response<Content>> {
// Read the environment variable using the std::env::var method
let message = env::var("MESSAGE").unwrap_or_else(|_| String::from("Missing message"));
Expand Down
4 changes: 2 additions & 2 deletions docs/src/components/HomepageFeatures/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const FeatureList = [
emoji: "⚙️",
description: (
<>
Create handlers in different languages thanks to WebAssembly.
Create workers in different languages thanks to WebAssembly.
</>
),
},
Expand All @@ -32,7 +32,7 @@ const FeatureList = [
},
];

function Feature({emoji, title, description}) {
function Feature({ emoji, title, description }) {
return (
<div className={clsx('col col--4')}>
<div className="text--center">
Expand Down
Loading

0 comments on commit dcec209

Please sign in to comment.