Create new section on how to use HTTP in components#336
Create new section on how to use HTTP in components#336hakilebara wants to merge 1 commit intobytecodealliance:mainfrom
Conversation
kate-goldenring
left a comment
There was a problem hiding this comment.
LGTM! Ran through it all in just a minute. Thank you for adding this! One small nit suggestion
| cd wasm-http-hello-world | ||
| ``` | ||
|
|
||
| Add [`wstd`][wstd], a Rust async standard library for Wasm Components. |
There was a problem hiding this comment.
Can you elaborate here what wstd does for us? Maybe explain that it prevents the need to directly pull the WASI HTTP WITs and directly use the bindings generated by wit-bindgen.
Includes a Rust guide on creating an HTTP Wasm component with wstd.
847bacd to
42ca2d2
Compare
kate-goldenring
left a comment
There was a problem hiding this comment.
One small reword and then i think we are good to go
|
|
||
|
|
||
| > [!NOTE] | ||
| > You can build an HTTP component in Rust without `wstd`. You would have to define the interfaces of the component in the WIT language, fetch their dependencies with `wkg` and generate the Rust bindings with `wit-bindgen`. Both approaches are valid. |
There was a problem hiding this comment.
| > You can build an HTTP component in Rust without `wstd`. You would have to define the interfaces of the component in the WIT language, fetch their dependencies with `wkg` and generate the Rust bindings with `wit-bindgen`. Both approaches are valid. | |
| > You can build an HTTP component in Rust without `wstd`. You would have to fetch the [WASI HTTP WIT files](https://github.com/WebAssembly/WASI/tree/main/proposals/http) and their dependencies with `wkg` and then generate the Rust bindings with `wit-bindgen`. Both approaches are valid. |
|
closes #182 |
vados-cosmonic
left a comment
There was a problem hiding this comment.
LGTM 🚀
I left a few nits -- please feel free to rewrite/reword things as you'd like. I've also tried to add more links to things that readers may not be immediately familiar with (for example if they just landed directly on this page).
| ```console | ||
| cargo add wstd | ||
| ``` | ||
| `wstd` provides bindings for `wasi:http` as an idiomatic Rust async API. That's why, in this guide, you won't add WIT files to your project nor depend on `wit-bindgen` directly. |
There was a problem hiding this comment.
| `wstd` provides bindings for `wasi:http` as an idiomatic Rust async API. That's why, in this guide, you won't add WIT files to your project nor depend on `wit-bindgen` directly. | |
| `wstd` provides idiomatic Rust bindings for WASI standard interfaces (`wasi: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. |
Slight re-wording of this paragraph
| ``` | ||
|
|
||
|
|
||
| Add [`wstd`][wstd], a Rust async standard library for Wasm components. |
There was a problem hiding this comment.
| Add [`wstd`][wstd], a Rust async standard library for Wasm components. | |
| Add [`wstd`][wstd], a Rust async standard library for Wasm components as a dependency with `cargo add`: |
|
|
||
|
|
||
| > [!NOTE] | ||
| > You can build an HTTP component in Rust without `wstd`. You would have to define the interfaces of the component in the WIT language, fetch their dependencies with `wkg` and generate the Rust bindings with `wit-bindgen`. Both approaches are valid. |
There was a problem hiding this comment.
| > You can build an HTTP component in Rust without `wstd`. You would have to define the interfaces of the component in the WIT language, fetch their dependencies with `wkg` and generate the Rust bindings with `wit-bindgen`. Both approaches are valid. | |
| > It is possible to build an HTTP component in Rust without `wstd`. Building a HTTP component without `wstd` would require defining the `wasi: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. |
| // transforms the user's `fn main` into the appropriate `handle` function. | ||
| #[wstd::http_server] | ||
| async fn main(req: Request<Body>) -> Result<Response<Body>> { | ||
| match req.uri().path_and_query().unwrap().as_str() { |
There was a problem hiding this comment.
| match req.uri().path_and_query().unwrap().as_str() { | |
| match req.uri().path() { |
We can use just path() here
| ``` | ||
|
|
||
|
|
||
| The resulting `.wasm` file will be compiled in `target/wasm32-wasip2/release/wasm-http-hello-world.wasm`. |
There was a problem hiding this comment.
| The resulting `.wasm` file will be compiled in `target/wasm32-wasip2/release/wasm-http-hello-world.wasm`. | |
| The `.wasm` binary for the component can be found at `target/wasm32-wasip2/release/wasm-http-hello-world.wasm`. |
| The resulting `.wasm` file will be compiled in `target/wasm32-wasip2/release/wasm-http-hello-world.wasm`. | ||
|
|
||
|
|
||
| Run the following Wasmtime command, it will spin-up an HTTP server on [`http://0.0.0.0:8080`](http://0.0.0.0:8080/) that redirect requests to instances of our component. |
There was a problem hiding this comment.
| Run the following Wasmtime command, it will spin-up an HTTP server on [`http://0.0.0.0:8080`](http://0.0.0.0:8080/) that redirect requests to instances of our component. | |
| To run the component, we can use [`wasmtime`](https://github.com/bytecodealliance/wasmtime/), a reference implementation host that supports the Component Model. | |
| In particular, we can use `wasmtime serve` subcommand, which will spin-up an HTTP server at `http://localhost:8080` which will use our component to fulfill web requests. `wasmtime` creates a *fresh* instance of the component every time a request is served. |

Create a new section in
Creating Componentsthat focuses on using HTTP in components.It includes a Rust guide on how to create a basic
Hello, world!HTTP component usingwstd.#182