Skip to content

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 8 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions source/news/2024/fermyon-spin-rust-sdk.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
:orphan:

############################################################
WebAssembly Components with Fermyon's Spin SDK for Rust
############################################################

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.

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.

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>`__.

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 running the following command:

.. 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, start up 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 created uses the request and response interfaces defined by the **wasi:http/proxy**, it can easily be deployed on NGINX Unit.
9 changes: 9 additions & 0 deletions source/news/2024/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ 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: Timo Stark
:description: Part 2 of our Wasm Component Model blog series. In this Blog post you will learn
how to build a Rust based Wasm Component and run it on NGINX Unit.
:email: [email protected]
Expand Down