Meet Sat, the tiny wasm function server
Sat (from Suborbital) is a very small HTTP server with no dependency and its unique duty is to serve a WebAssembly function, and Sat does it extraordinarily fast.
The possibilities offered by Sat are numerous. We will come back to this soon. But today, let's see how to use Sat.
I let you read these two blog posts about Sat:
Let's prepare our workspace
The Suborbital CLI
In a Suborbital context, we will call the WebAssembly function a Runnable.
A Runnable can be written in any of the supported languages (such as TypeScript, Rust, Swift or TinyGo).
Suborbital provides a tool, the Suborbital CLI (nickname: subo). Subo is used to build Wasm Runnables, and more.
So, to install subo, use the below command:
brew tap suborbital/subo
brew install subo
- See the GitHub Subo repository for other installation ways.
- You need Docker installed on your computer because Subo runs the build toolchain in a Docker container.
Build Sat
You can run Sat from a Docker image, but today, we'll build it:
git clone https://github.com/suborbital/sat.git
cd sat
make sat
sudo cp .bin/sat /usr/local/bin/
We copy the sat executable to
/usr/local/bin/
; so you can run it from anywhere in your directory structure.
Create a Runnable with Subo
To generate a Runnable project, use the below command:
subo create runnable hello
This command generated a Rust project of the Hello Runnable, have a look ./hello/src/lib.rs
:
use suborbital::runnable::*;
struct Hello{}
impl Runnable for Hello {
fn run(&self, input: Vec<u8>) -> Result<Vec<u8>, RunErr> {
let in_string = String::from_utf8(input).unwrap();
Ok(String::from(format!("hello {}", in_string)).as_bytes().to_vec())
}
}
// initialize the runner, do not edit below //
static RUNNABLE: &Hello = &Hello{};
#[no_mangle]
pub extern fn _start() {
use_runnable(RUNNABLE);
}
At the end of this blog post, we'll call this function (or Runnable) with a simple curl command:
curl localhost:8080 -d 'Bob Morane'
To build the hello wasm file from the Runnable source code, use the following command:
subo build ./hello
This 2nd command will build the wasm file: ./hello/hello.wasm
Now, it's time to serve the Hello Runnable
Serve the WebAssembly function (Runnable)
To serve the Runnable, listening on the HTTP port 8080
, run this command:
SAT_HTTP_PORT=8080 sat hello/hello.wasm
You should get logs like that:
{"log_message":"(W) configured to use HTTP with no TLS","timestamp":"2022-01-26T06:43:36.841357919Z","level":2}
{"log_message":"(I) starting hello ...","timestamp":"2022-01-26T06:43:36.841890307Z","level":3}
{"log_message":"(I) serving on :8080","timestamp":"2022-01-26T06:43:36.841961977Z","level":3}
{"log_message":"(I) [discovery-local] starting discovery, advertising endpoint 8080 /meta/message","timestamp":"2022-01-26T06:43:36.841951464Z","level":3}
Call the function
To call the Hello function, use a simple curl
command:
curl localhost:8080 -d 'Bob Morane'
# response: Hello Bob Morane
That's it! I love this simplicity
I plan to write, very soon, some new blog posts about Sat.
You can use my GitPod project to experiment: 01-first-function-with-sat
👋
- If you loved this "post" (or not), don't forget to use the emojis reactions
- Don't hesitate to add comments and/or ask questions
- You can subscribe to the Rss feed