diff --git a/Cargo.lock b/Cargo.lock index fab93b17..7b9e0c26 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1039,19 +1039,6 @@ dependencies = [ "tracing", ] -[[package]] -name = "handler" -version = "0.1.0" -dependencies = [ - "anyhow", - "http", - "quote", - "serde", - "serde_json", - "syn", - "wasi", -] - [[package]] name = "hashbrown" version = "0.12.3" @@ -2233,10 +2220,10 @@ version = "0.1.0" dependencies = [ "anyhow", "base64", - "handler", "http", "serde", "serde_json", + "worker", ] [[package]] @@ -2717,6 +2704,19 @@ dependencies = [ "wast 35.0.2", ] +[[package]] +name = "worker" +version = "0.1.0" +dependencies = [ + "anyhow", + "http", + "quote", + "serde", + "serde_json", + "syn", + "wasi", +] + [[package]] name = "zstd" version = "0.11.2+zstd.1.5.2" diff --git a/Cargo.toml b/Cargo.toml index a2fee790..022b417d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 diff --git a/README.md b/README.md index 13defff2..9ca2bb52 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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 ``` @@ -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 ``` @@ -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; @@ -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 | | --- | --- | --- | @@ -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 @@ -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 diff --git a/docs/docs/containers.md b/docs/docs/containers.md index e4df6275..d6b0ed03 100644 --- a/docs/docs/containers.md +++ b/docs/docs/containers.md @@ -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` @@ -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 ``` diff --git a/docs/docs/how-it-works.md b/docs/docs/how-it-works.md index 01f1434b..2163ab97 100644 --- a/docs/docs/how-it-works.md +++ b/docs/docs/how-it-works.md @@ -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 @@ -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" @@ -40,4 +40,4 @@ version = "1" namespace = "counter" ``` -These files are only required to enable extra features for your handlers. \ No newline at end of file +These files are only required to enable extra features for your workers. \ No newline at end of file diff --git a/docs/docs/start.md b/docs/docs/start.md index b260febf..7e8241e1 100644 --- a/docs/docs/start.md +++ b/docs/docs/start.md @@ -44,7 +44,7 @@ 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 ``` @@ -52,9 +52,9 @@ Now, open your browser at . ## 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). \ No newline at end of file diff --git a/docs/docs/tutorials/javascript-workers.md b/docs/docs/tutorials/javascript-workers.md index 96730019..019aef88 100644 --- a/docs/docs/tutorials/javascript-workers.md +++ b/docs/docs/tutorials/javascript-workers.md @@ -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 ``` @@ -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" @@ -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 ``` diff --git a/docs/docs/tutorials/rust-workers.md b/docs/docs/tutorials/rust-workers.md index d9756719..c9ea82af 100644 --- a/docs/docs/tutorials/rust-workers.md +++ b/docs/docs/tutorials/rust-workers.md @@ -9,7 +9,7 @@ Then, they are loaded by Wasm Workers Server and start processing requests. ## Your first worker -Every worker receives a [Request](https://docs.rs/http/0.2.8/http/request/struct.Request.html) struct and returns a [Response](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](https://docs.rs/http/0.2.8/http/request/struct.Request.html) struct and returns a [Response](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. @@ -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) -> Result> { Ok(http::Response::builder() .status(200) @@ -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) -> Result> { // Applied changes here to use the Response method. This requires changes // on signature and how it returns the data. @@ -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 ``` @@ -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, cache: &mut Cache) -> Result> { Ok(http::Response::builder() .status(200) @@ -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, cache: &mut Cache) -> Result> { // Applied changes here to use the Response method. This requires changes // on signature and how it returns the data. @@ -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" @@ -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 ``` @@ -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) -> Result> { // Read the environment variable using the std::env::var method let message = env::var("MESSAGE").unwrap_or_else(|_| String::from("Missing message")); diff --git a/docs/src/components/HomepageFeatures/index.js b/docs/src/components/HomepageFeatures/index.js index 3facfc7a..7e5fb6e3 100644 --- a/docs/src/components/HomepageFeatures/index.js +++ b/docs/src/components/HomepageFeatures/index.js @@ -17,7 +17,7 @@ const FeatureList = [ emoji: "⚙️", description: ( <> - Create handlers in different languages thanks to WebAssembly. + Create workers in different languages thanks to WebAssembly. ), }, @@ -32,7 +32,7 @@ const FeatureList = [ }, ]; -function Feature({emoji, title, description}) { +function Feature({ emoji, title, description }) { return (
diff --git a/examples/README.md b/examples/README.md index 3ab0932a..2384583c 100644 --- a/examples/README.md +++ b/examples/README.md @@ -4,10 +4,10 @@ This folder includes different handlers. All of them are based on other projects ## Build -### JavaScript handlers +### JavaScript workers -Wasm Workers Server includes a `QuickJS` interpreter. JavaScript handlers are automatically compatible and you don't need to compile them to WebAssembly, although they will run in it. +Wasm Workers Server includes a `QuickJS` interpreter. JavaScript workers are automatically compatible and you don't need to compile them to WebAssembly, although they will run in it. -### Rust handlers +### Rust workers -For Rust handlers, you need to use our `wasm-workers-server-kit` crate. This folder contains several examples of its usage. \ No newline at end of file +For Rust workers, you need to use our `wasm-workers-server-kit` crate. This folder contains several examples of its usage. \ No newline at end of file diff --git a/examples/pdf-create/src/main.rs b/examples/pdf-create/src/main.rs index bc45807c..f55114f2 100644 --- a/examples/pdf-create/src/main.rs +++ b/examples/pdf-create/src/main.rs @@ -2,12 +2,11 @@ use anyhow::Result; use printpdf::*; use std::io::BufWriter; use wasm_workers_rs::{ - handler, http::{self, Request, Response}, - Content, + worker, Content, }; -#[handler] +#[worker] fn handler(req: Request) -> Result> { let mut buf = BufWriter::new(Vec::new()); diff --git a/examples/rust-basic/Cargo.lock b/examples/rust-basic/Cargo.lock index a753bbdb..23a3bef5 100644 --- a/examples/rust-basic/Cargo.lock +++ b/examples/rust-basic/Cargo.lock @@ -26,19 +26,6 @@ version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" -[[package]] -name = "handler" -version = "0.1.0" -dependencies = [ - "anyhow", - "http", - "quote", - "serde", - "serde_json", - "syn", - "wasi", -] - [[package]] name = "http" version = "0.2.8" @@ -148,8 +135,21 @@ version = "0.1.0" dependencies = [ "anyhow", "base64", - "handler", "http", "serde", "serde_json", + "worker", +] + +[[package]] +name = "worker" +version = "0.1.0" +dependencies = [ + "anyhow", + "http", + "quote", + "serde", + "serde_json", + "syn", + "wasi", ] diff --git a/examples/rust-basic/src/main.rs b/examples/rust-basic/src/main.rs index 7f2d64ad..8d7e6eff 100644 --- a/examples/rust-basic/src/main.rs +++ b/examples/rust-basic/src/main.rs @@ -1,12 +1,11 @@ use anyhow::Result; use std::env; use wasm_workers_rs::{ - handler, http::{self, HeaderValue, Request, Response}, - Content, + worker, Content, }; -#[handler] +#[worker] fn handler(req: Request) -> Result> { let message = env::var("MESSAGE").unwrap_or_else(|_| String::from("Missing title")); diff --git a/examples/rust-kv/Cargo.lock b/examples/rust-kv/Cargo.lock index 5cce7369..e5b9dd46 100644 --- a/examples/rust-kv/Cargo.lock +++ b/examples/rust-kv/Cargo.lock @@ -8,6 +8,12 @@ version = "1.0.65" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "98161a4e3e2184da77bb14f02184cdd111e83bbbcc9979dfee3c44b9a85f5602" +[[package]] +name = "base64" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8" + [[package]] name = "bytes" version = "1.2.1" @@ -20,19 +26,6 @@ version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" -[[package]] -name = "handler" -version = "0.1.0" -dependencies = [ - "anyhow", - "http", - "quote", - "serde", - "serde_json", - "syn", - "wasi", -] - [[package]] name = "http" version = "0.2.8" @@ -141,8 +134,22 @@ name = "wasm-workers-rs" version = "0.1.0" dependencies = [ "anyhow", - "handler", + "base64", "http", "serde", "serde_json", + "worker", +] + +[[package]] +name = "worker" +version = "0.1.0" +dependencies = [ + "anyhow", + "http", + "quote", + "serde", + "serde_json", + "syn", + "wasi", ] diff --git a/examples/rust-kv/kv.toml b/examples/rust-kv/kv.toml index 8b5ae21b..a0c89f7c 100644 --- a/examples/rust-kv/kv.toml +++ b/examples/rust-kv/kv.toml @@ -4,3 +4,5 @@ version = "1" [data] [data.kv] namespace = "counter" + +[vars] \ No newline at end of file diff --git a/examples/rust-kv/src/main.rs b/examples/rust-kv/src/main.rs index 1d82f5fd..1c458bd0 100644 --- a/examples/rust-kv/src/main.rs +++ b/examples/rust-kv/src/main.rs @@ -1,11 +1,10 @@ use anyhow::Result; use wasm_workers_rs::{ - handler, http::{self, Request, Response}, - Cache, Content, + worker, Cache, Content, }; -#[handler(cache)] +#[worker(cache)] fn handler(_req: Request, cache: &mut Cache) -> Result> { // Applied changes here to use the Response method. This requires changes // on signature and how it returns the data. diff --git a/kits/javascript/README.md b/kits/javascript/README.md index 26f09088..e926a043 100644 --- a/kits/javascript/README.md +++ b/kits/javascript/README.md @@ -1,7 +1,7 @@ # Use it with JavaScript -The project already includes the compiled QuickJS engine. To simplify the usage of JS handlers, the tool will automatically load this engine and will pass the source code with the request data. +The project already includes the compiled QuickJS engine. To simplify the usage of JS workers, the tool will automatically load this engine and will pass the source code with the request data. This project is based on the [quickjs-wasm-rs](https://github.com/Shopify/javy/tree/main/crates/quickjs-wasm-rs) crate from Shopify. -Check our [examples](/examples) folders to see different handlers using this kit. +Check our [examples](/examples) folders to see different workers using this kit. diff --git a/kits/rust/Cargo.toml b/kits/rust/Cargo.toml index c7facbb4..6c9dc3cb 100644 --- a/kits/rust/Cargo.toml +++ b/kits/rust/Cargo.toml @@ -13,7 +13,7 @@ path = "src/lib.rs" [dependencies] anyhow = "1.0.63" http = "0.2.8" -handler = { path = "./handler" } +worker = { path = "./worker" } serde = { version = "1.0", features = ["derive"] } serde_json = "1.0.85" base64 = "0.13.1" \ No newline at end of file diff --git a/kits/rust/src/io.rs b/kits/rust/src/io.rs index 24e80aba..835a79aa 100644 --- a/kits/rust/src/io.rs +++ b/kits/rust/src/io.rs @@ -46,7 +46,7 @@ impl Input { } } -/// Represents the JSON output that the handler must return +/// Represents the JSON output that the worker must return /// back to the main project #[derive(Serialize, Deserialize)] pub struct Output { diff --git a/kits/rust/src/lib.rs b/kits/rust/src/lib.rs index f18425cd..fe3620e2 100644 --- a/kits/rust/src/lib.rs +++ b/kits/rust/src/lib.rs @@ -7,6 +7,6 @@ pub use cache::Cache; pub use content::Content; pub mod io; -pub use handler::handler; +pub use worker::worker; // Re-export http pub use http; diff --git a/kits/rust/handler/Cargo.toml b/kits/rust/worker/Cargo.toml similarity index 97% rename from kits/rust/handler/Cargo.toml rename to kits/rust/worker/Cargo.toml index fc3532c2..64319944 100644 --- a/kits/rust/handler/Cargo.toml +++ b/kits/rust/worker/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "handler" +name = "worker" version = "0.1.0" edition = "2021" license = "Apache-2.0" diff --git a/kits/rust/handler/src/args.rs b/kits/rust/worker/src/args.rs similarity index 93% rename from kits/rust/handler/src/args.rs rename to kits/rust/worker/src/args.rs index 80095bbf..28052cae 100644 --- a/kits/rust/handler/src/args.rs +++ b/kits/rust/worker/src/args.rs @@ -5,7 +5,7 @@ use std::collections::HashSet; use syn::parse::{Parse, ParseStream, Result}; use syn::{punctuated::Punctuated, Ident, Token}; -/// Parse valid arguments for the handler +/// Parse valid arguments for the worker pub struct Args { idents: HashSet, } diff --git a/kits/rust/handler/src/expand.rs b/kits/rust/worker/src/expand.rs similarity index 100% rename from kits/rust/handler/src/expand.rs rename to kits/rust/worker/src/expand.rs diff --git a/kits/rust/handler/src/lib.rs b/kits/rust/worker/src/lib.rs similarity index 64% rename from kits/rust/handler/src/lib.rs rename to kits/rust/worker/src/lib.rs index f85262f6..77b19087 100644 --- a/kits/rust/handler/src/lib.rs +++ b/kits/rust/worker/src/lib.rs @@ -6,9 +6,9 @@ mod expand; use proc_macro::TokenStream; -// General handler entrypoint. It will bind the input +// General worker entrypoint. It will bind the input // with Request and Response objects #[proc_macro_attribute] -pub fn handler(attr: TokenStream, item: TokenStream) -> TokenStream { +pub fn worker(attr: TokenStream, item: TokenStream) -> TokenStream { expand::expand_macro(attr, item) } diff --git a/src/config.rs b/src/config.rs index 2c5d92a8..c1e9b666 100644 --- a/src/config.rs +++ b/src/config.rs @@ -8,10 +8,10 @@ use std::path::PathBuf; use std::{env, fs}; use toml::from_slice; -/// Handlers configuration. These files are optional when no configuration change is required. +/// Workers configuration. These files are optional when no configuration change is required. #[derive(Deserialize, Clone)] pub struct Config { - /// Handler name. For logging purposes + /// Worker name. For logging purposes pub name: Option, /// Mandatory version of the file pub version: String, @@ -22,16 +22,16 @@ pub struct Config { pub vars: HashMap, } -/// Configure a data plugin for the handler +/// Configure a data plugin for the worker #[derive(Deserialize, Clone)] pub struct ConfigData { - /// Creates a Key/Value store associated to the given handler + /// Creates a Key/Value store associated to the given worker pub kv: Option, } impl Config { /// Try to read the configuration from a TOML file. The path contains the local path - /// to the handler configuration. The file should use the same name as the handler, + /// to the worker configuration. The file should use the same name as the worker, /// with the .toml extension /// /// # Examples diff --git a/src/data/README.md b/src/data/README.md index 5e5e5eb9..95821dba 100644 --- a/src/data/README.md +++ b/src/data/README.md @@ -1,6 +1,6 @@ # Data -Data are a way to inject and read external information in the wasm modules. These data services reads the data from a given location and injects the data to the handler. Others allow handlers to write data. +Data are a way to inject and read external information in the wasm modules. These data services reads the data from a given location and injects the data to the worker. Others allow workers to write data. ## List diff --git a/src/data/kv.rs b/src/data/kv.rs index d2956fd0..a787e942 100644 --- a/src/data/kv.rs +++ b/src/data/kv.rs @@ -4,15 +4,15 @@ use serde::Deserialize; use std::collections::HashMap; -/// The Key/Value store configuration. This information is read from handlers TOML files. +/// The Key/Value store configuration. This information is read from workers TOML files. #[derive(Deserialize, Clone)] pub struct KVConfigData { - /// The namespace the handler will access in the global Key / Value store + /// The namespace the worker will access in the global Key / Value store pub namespace: String, } /// An in-memory Key/Value store. It contains multiple namespaces which has their -/// own K/V store inside. This is used to scope the data handlers can access +/// own K/V store inside. This is used to scope the data workers can access pub struct KV { /// The available K/V stores pub stores: HashMap, diff --git a/src/main.rs b/src/main.rs index 635cd3c9..2dfbd5bc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,7 +24,7 @@ use std::io::Error; use std::path::PathBuf; use std::{collections::HashMap, sync::RwLock}; -// Provide a static root_path so it can be used in the default_handler to manage +// Provide a static root_path so it can be used in the default_worker to manage // static assets. lazy_static! { static ref ARGS: Args = Args::parse(); @@ -201,7 +201,7 @@ async fn main() -> std::io::Result<()> { }; println!( - " - http://{}:{}{}\n => {} (handler: {})", + " - http://{}:{}{}\n => {} (name: {})", &args.hostname, args.port, route.path, diff --git a/src/router.rs b/src/router.rs index 321dd2fa..4ecb76a6 100644 --- a/src/router.rs +++ b/src/router.rs @@ -146,7 +146,7 @@ fn is_in_public_folder(path: &Path) -> bool { /// Defines a prefix in the context of the application. /// This prefix will be used for the static assets and the -/// handlers. +/// workers. /// /// A prefix must have the format: /X. This method receives /// the optional prefix and returns a proper String. diff --git a/src/runner.rs b/src/runner.rs index 2ecad334..55a26d53 100644 --- a/src/runner.rs +++ b/src/runner.rs @@ -119,7 +119,7 @@ pub fn build_headers_hash(headers: &HeaderMap) -> HashMap { } #[derive(Clone)] -pub enum RunnerHandlerType { +pub enum RunnerWorkerType { Wasm, JavaScript, } @@ -131,7 +131,7 @@ pub struct Runner { /// Engine that runs the actual Wasm module engine: Engine, /// The type of the required runner - runner_type: RunnerHandlerType, + runner_type: RunnerWorkerType, /// Preloaded Module module: Module, /// Source code if required @@ -146,7 +146,7 @@ impl Runner { let module = Module::from_binary(&engine, JS_ENGINE_WASM)?; ( - RunnerHandlerType::JavaScript, + RunnerWorkerType::JavaScript, module, fs::read_to_string(path) .unwrap_or_else(|_| panic!("Error reading {}", path.display())), @@ -154,7 +154,7 @@ impl Runner { } else { let module = Module::from_file(&engine, path)?; - (RunnerHandlerType::Wasm, module, String::new()) + (RunnerWorkerType::Wasm, module, String::new()) }; Ok(Self { @@ -178,8 +178,8 @@ impl Runner { /// run. pub fn run(&self, input: &str, vars: &HashMap) -> Result { let stdin = match self.runner_type { - RunnerHandlerType::Wasm => ReadPipe::from(input), - RunnerHandlerType::JavaScript => { + RunnerWorkerType::Wasm => ReadPipe::from(input), + RunnerWorkerType::JavaScript => { let mut contents = String::new(); contents.push_str(&self.source); // Separator