-
Notifications
You must be signed in to change notification settings - Fork 113
Add Wasm Blog Post about Fermyon's Spin SDK for Rust #123
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
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e15f04f
Add Wasm Blog Post about Fermyon's Spin SDK for Rust
94c427e
Apply suggestions from code review
d203c56
Update Title and Reference to other Spin SDKs
6b8a4c8
Update source/news/2024/fermyon-spin-rust-sdk.rst
c72ce45
Update source/news/2024/fermyon-spin-rust-sdk.rst
JTorreG 514e6ae
Update source/news/2024/fermyon-spin-rust-sdk.rst
JTorreG 557a337
Update source/news/2024/fermyon-spin-rust-sdk.rst
JTorreG 30a6e0d
Merge branch 'master' into wasm-spin-sdk
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 blog series `Part 1 </news/2024/wasm-component-model-part-1/>`__ and `2 </news/2024/wasm-component-model-part-2/>`__ 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. | ||
tippexs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
In this blog post, we will have a look on the `Fermyon's 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. | ||
|
||
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. | ||
tippexs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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: | ||
tippexs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
.. 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. | ||
tippexs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
.. 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. | ||
tippexs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
.. 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. | ||
tippexs marked this conversation as resolved.
Show resolved
Hide resolved
|
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,15 @@ News of 2024 | |
|
||
News archive for the year 2024. | ||
|
||
.. nxt_news_entry:: | ||
:author: Timo Stark | ||
:description: Building Wasm Components using Feryon's Spin SDK for Rust | ||
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 | ||
|
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.