Skip to content

Commit daa7f1d

Browse files
authored
Merge pull request #123 from nginx/wasm-spin-sdk
Add Wasm Blog Post about Fermyon's Spin SDK for Rust
2 parents 383d7d9 + 30a6e0d commit daa7f1d

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
:orphan:
2+
3+
############################################################
4+
WebAssembly Components with Fermyon's Spin SDK for Rust
5+
############################################################
6+
7+
In our blog series `Part 1 </news/2024/wasm-component-model-part-1/>`__ and `Part 2 </news/2024/wasm-component-model-part-2/>`__ , we have covered the core mechanism of the WebAssembly Component Model and showcased how to create a Wasm Component using WASI 0.2 APIs and the **wasi/http:proxy** world.
8+
9+
In this blog post, we will have a look at 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 Wasm Component that can be hosted on NGINX Unit.
10+
11+
The Spin SDKs provide a great developer experience, as they wrap a lot of the manual work in easy to consume APIs. In this blog post we will focus on Rust, but if you would like to learn more about the other language SDKs, please see the official `documentation <https://developer.fermyon.com/spin/v2/language-support-overview>`__.
12+
13+
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.
14+
15+
16+
.. code-block:: bash
17+
18+
$ cargo new --lib test-spin-component
19+
$ cd test-spin-component
20+
21+
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 running the following command:
22+
23+
.. code-block:: bash
24+
25+
$ cargo add spin-sdk anyhow
26+
27+
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.
28+
29+
.. code-block:: toml
30+
31+
[lib]
32+
crate-type = ["cdylib"]
33+
34+
[package.metadata.component]
35+
package = "component:test-component"
36+
proxy = true
37+
38+
[package.metadata.component.dependencies]
39+
40+
Next, replace the content of **src/lib.rs** file with the following code:
41+
42+
.. code-block:: rust
43+
44+
use spin_sdk::http::{IntoResponse, Request, Response};
45+
use spin_sdk::http_component;
46+
47+
#[http_component]
48+
fn handle_hello_world(_req: Request) -> anyhow::Result<impl IntoResponse> {
49+
let body_string = String::from("Hello, this is a Wasm Component using Spin SDK");
50+
51+
Ok(Response::builder()
52+
.status(200)
53+
.header("Content-Type", "text/plain")
54+
.header("Content-Lenght", body_string.len().to_string())
55+
.body(body_string)
56+
.build())
57+
}
58+
59+
Compile the Rust Library into a Wasm Component using **cargo component**:
60+
61+
.. code-block:: bash
62+
63+
$ cargo component build --release
64+
65+
To run the Wasm Component on NGINX Unit, start up Unit and use this initial configuration.
66+
67+
.. note:: Make sure you point to the Wasm component by using an absolute path.
68+
69+
.. code-block:: json
70+
71+
{
72+
"listeners": {
73+
"127.0.0.1:8085": {
74+
"pass": "applications/my-spin-component"
75+
}
76+
},
77+
"applications": {
78+
"my-spin-component": {
79+
"type": "wasm-wasi-component",
80+
"component": "target/wasm32-wasi/release/test_spin_component.wasm"
81+
}
82+
}
83+
}
84+
85+
As the Wasm Component we have just created uses the request and response interfaces defined by the **wasi:http/proxy**, it can easily be deployed on NGINX Unit.

source/news/2024/index.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ News archive for the year 2024.
66

77
.. nxt_news_entry::
88
:author: Timo Stark
9+
:description: Building Wasm Components using Feryon's Spin SDK for Rust
10+
and run them on NGINX Unit.
11+
12+
:title: Wasm Components: Working with the Spin SDK for Rust
13+
:url: news/2024/fermyon-spin-rust-sdk
14+
:date: 2024-03-13
15+
16+
.. nxt_news_entry::
17+
:author: Timo Stark
918
:description: Part 2 of our Wasm Component Model blog series. In this Blog post you will learn
1019
how to build a Rust based Wasm Component and run it on NGINX Unit.
1120

0 commit comments

Comments
 (0)