The Spin Rust SDK
The Spin Rust SDK makes it easy to build Spin components in Rust.
Writing Spin HTTP components in Rust
This library simplifies writing Spin HTTP components. Below is an example of such a component:
// lib.rs
use Result;
use ;
/// A simple Spin HTTP component.
The important things to note in the function above:
- the
spin_sdk::http_component
macro — this marks the function as the entrypoint for the Spin component - the function signature —
fn hello_world(req: Request) -> Result<Response>
—req
can be any number of types including the built inRequest
type or thehttp::Request
from the popularhttp
crate. Likewise, the response type can be many things including the built inResponse
type or thehttp::Response
type from thehttp
crate.
Making outbound HTTP requests
Let's see an example where the component makes an outbound HTTP request to a server, modifies the result, then returns it:
async
In order for the component above to be allowed to make the outbound HTTP request, the destination host must be declared in the Spin application configuration:
[[]]
= "hello"
= "target/wasm32-wasi/release/spinhelloworld.wasm"
= [ "https://fermyon.com" ]
[]
= "/hello"
Making a request to this component, we can see the appended header, and that the response contains the expected body:
$ curl -I localhost:3000/hello
HTTP/1.1 200 OK
content-length: 29350
content-type: text/html; charset=utf-8
server: spin/0.1.0