-
Notifications
You must be signed in to change notification settings - Fork 113
Add Wasm Blog Posts: Wasm Component Model Part 1 and 2 #122
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+368
−0
Merged
Changes from 1 commit
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
735c6f7
Add Blog-Posts for Wasm I/O Barcelona 2024
bf5377c
Update Split the Wasm Component Model Blog Post into two parts
651c083
Apply suggestions from code review
8798e5d
Apply suggestions from code review
e44d425
Update Blockquote Styling and Wording
f474d2e
Update Wording
40fd021
Update source/news/2024/wasm-component-model-part-1.rst
JTorreG 2dbda55
Update source/news/2024/wasm-component-model-part-1.rst
JTorreG b5bcdf2
Update source/news/2024/wasm-component-model-part-1.rst
JTorreG 7b5b9b4
Update source/news/2024/wasm-component-model-part-1.rst
JTorreG a84068e
Update source/news/2024/wasm-component-model-part-1.rst
JTorreG b41128a
Update source/news/2024/wasm-component-model-part-2.rst
JTorreG eed92f7
Update source/news/2024/wasm-component-model-part-2.rst
JTorreG fbd02a8
Update source/news/2024/wasm-component-model-part-2.rst
JTorreG b6cc2d2
Update source/news/2024/wasm-component-model-part-2.rst
JTorreG dad84b8
Update source/news/2024/wasm-component-model-part-2.rst
JTorreG e4b1849
Update source/news/2024/wasm-component-model-part-2.rst
JTorreG File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
:orphan: | ||
|
||
############################################################ | ||
WebAssembly Components with Fermyons Spin SDK for Rust | ||
############################################################ | ||
|
||
In our `previous blog post <https://unit.nginx.org/news/2024/wasm-component-model-intro/>`__ we have covered the core mechanism of the WebAssembly Component Model | ||
and show cased how to create a Wasm Component using WASI 0.2 APIs and the **wasi/http:proxy** world. | ||
|
||
In this blog post, we will have a look on the `Fermyon Spin <https://www.fermyon.com/spin>`__ SDK for `Rust <https://fermyon.github.io/rust-docs/spin/main/spin_sdk/index.html>`__ and create a component that can be hosted on NGINX Unit. | ||
tippexs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
The Spin SDK for Rust comes with a great developer experience as it wraps a lot of the manual work in an easy to consume Rust API. | ||
|
||
Let's start by creating a new Rust library using **cargo new**. This will create a new library project in a sub-directory **test-spin-component** of our current work directory. | ||
|
||
|
||
.. code-block:: bash | ||
|
||
$ cargo new --lib test-spin-component | ||
$ cd test-spin-component | ||
|
||
Add the latest version of the "spin-sdk" and "anyhow" (Flexible Error Types and a dependency of the Spin SDK) crates to the project by issuing: | ||
|
||
.. code-block:: bash | ||
|
||
$ cargo add spin-sdk anyhow | ||
|
||
Before we implement the actual functionality, we must modify our **Cargo.toml** file. Open the **Cargo.toml** with an editor of your | ||
choice and append the following to the bottom of your existing **Cargo.toml** file. | ||
|
||
.. code-block:: toml | ||
|
||
[lib] | ||
crate-type = ["cdylib"] | ||
|
||
[package.metadata.component] | ||
package = "component:test-component" | ||
proxy = true | ||
|
||
[package.metadata.component.dependencies] | ||
|
||
Next, replace the content of **src/lib.rs** file with the following code: | ||
|
||
.. code-block:: rust | ||
|
||
use spin_sdk::http::{IntoResponse, Request, Response}; | ||
use spin_sdk::http_component; | ||
|
||
#[http_component] | ||
fn handle_hello_world(_req: Request) -> anyhow::Result<impl IntoResponse> { | ||
let body_string = String::from("Hello, this is a Wasm Component using Spin SDK"); | ||
|
||
Ok(Response::builder() | ||
.status(200) | ||
.header("Content-Type", "text/plain") | ||
.header("Content-Lenght", body_string.len().to_string()) | ||
.body(body_string) | ||
.build()) | ||
} | ||
|
||
Compile the Rust Library into a Wasm Component using **cargo component**: | ||
|
||
.. code-block:: bash | ||
|
||
$ cargo component build --release | ||
|
||
To run the Wasm Component on NGINX Unit, startup Unit and use this initial configuration. | ||
|
||
.. note:: Make sure you point to the Wasm component by using an absolute path. | ||
|
||
.. code-block:: json | ||
|
||
{ | ||
"listeners": { | ||
"127.0.0.1:8085": { | ||
"pass": "applications/my-spin-component" | ||
} | ||
}, | ||
"applications": { | ||
"my-spin-component": { | ||
"type": "wasm-wasi-component", | ||
"component": "target/wasm32-wasi/release/test_spin_component.wasm" | ||
} | ||
} | ||
} | ||
|
||
As the Wasm Component we have just crated uses the request and response interfaces defined by the **wasi:http/proxy** | ||
it can easily be deployed on NGINX Unit. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,24 @@ News of 2024 | |
|
||
News archive for the year 2024. | ||
|
||
.. nxt_news_entry:: | ||
:author: Timo Stark | ||
:description: A deep dive into the Wasm Component Model and a Rust based | ||
Tutorial about how to get started with Wasm Components on Unit. | ||
:email: [email protected] | ||
:title: The WebAssembly Component Model - The Why, How and What | ||
:url: news/2024/wasm-component-model-intro | ||
:date: 2024-03-13 | ||
|
||
.. nxt_news_entry:: | ||
:author: Timo Stark | ||
:description: Building Wasm Components using Feryons Spin SDK for Rust | ||
JTorreG marked this conversation as resolved.
Show resolved
Hide resolved
|
||
and run them on NGINX Unit. | ||
:email: [email protected] | ||
:title: Wasm Components: Working with the Spin SDK for Rust | ||
:url: news/2024/fermyon-spin-rust-sdk | ||
:date: 2024-03-13 | ||
|
||
.. nxt_news_entry:: | ||
:author: Unit Team | ||
:description: Version 1.32.0 Adds Support for WebAssembly Components | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.