diff --git a/component-model/src/SUMMARY.md b/component-model/src/SUMMARY.md index 7601aa4..f0f1e37 100644 --- a/component-model/src/SUMMARY.md +++ b/component-model/src/SUMMARY.md @@ -37,6 +37,8 @@ - [Other languages](./language-support/creating-runnable-components/other-languages.md) - [Using WIT resources](./using-wit-resources.md) - [Rust](./language-support/using-wit-resources/rust.md) + - [Using HTTP in components](./using-http-in-components.md) + - [Rust](./language-support/using-http-in-components/rust.md) - [Running Components](./running-components.md) - [Wasmtime](./running-components/wasmtime.md) - [jco](./running-components/jco.md) diff --git a/component-model/src/language-support/using-http-in-components/rust.md b/component-model/src/language-support/using-http-in-components/rust.md new file mode 100644 index 0000000..3f2677f --- /dev/null +++ b/component-model/src/language-support/using-http-in-components/rust.md @@ -0,0 +1,123 @@ +# Rust + +## 1. Setup + +Add the `wasm32-wasip2` target to the Rust toolchain. + +```rust +rustup target add wasm32-wasip2 +``` + + +Install [`wasmtime`][wasmtime]. The Wasmtime CLI has a built-in HTTP server that supports serving WebAssembly HTTP components. + +```console +curl https://wasmtime.dev/install.sh -sSf | bash +``` + +[wasmtime]: https://github.com/bytecodealliance/wasmtime#installation + +## 2. Creating a Rust WebAssemly project + +Create a new Rust project with `cargo new`: + + +```console +cargo new wasm-http-hello-world +cd wasm-http-hello-world +``` + + +Add [`wstd`][wstd], a Rust async standard library for Wasm components as a dependency with `cargo add`: +```console +cargo add wstd +``` +`wstd` provides idiomatic Rust bindings for WASI standard interfaces [(`wasi:http`)](https://github.com/WebAssembly/WASI/tree/main/proposals/http) to increase ease-of-use for Rust WebAssembly components. Since we are using `wstd`, we will not need to add WIT files or depend on [`wit-bindgen`](https://crates.io/crates/wit-bindgen) directly. + + +> [!NOTE] + +> It is possible to build an HTTP component in Rust without `wstd`. Building a HTTP component without `wstd` would require defining the [`wasi:http`](https://github.com/WebAssembly/WASI/tree/main/proposals/http) imports/exports of the component in WIT, fetching WIT dependencies with `wkg` and generating the Rust bindings with `wit-bindgen`. +> +> Both approaches are valid, but `wstd` offers superior developer experience, so we opt to use it here. +> +> `wstd` and `wit-bindgen` are not mutually exclusive and can co-exist in the same project. + +[wstd]: https://docs.rs/wstd/latest/wstd/index.html + +## 3. Writing the HTTP handler + +We will implement the HTTP handler in `src/main.rs`. The file should look like the following: +```rust +use wstd::http::{Body, Request, Response, Result, StatusCode}; + +// WASI HTTP server components don't use a traditional `main` function. +// They export a function named `handle` which takes a `Request` +// argument, and which may be called multiple times on the same +// instance. To let users write a familiar `fn main` in a file +// named src/main.rs, wstd provides this `wstd::http_server` macro, which +// transforms the user's `fn main` into the appropriate `handle` function. +#[wstd::http_server] +async fn main(req: Request
) -> Result