Demonstrates composing a name
component and a greeter
component with either:
- a
cli
component - a
http
component
Updated to use the wasm32-wasip2
target in Rust nightly.
- Rust nightly —
rustup toolchain install nightly
- wac —
cargo binstall wac-cli
- wasmtime —
brew install wasmtime
reads the NAME
environment variable and returns it.
world name {
import wasi:cli/environment@0.2.0;
export name: func() -> string;
}
impl Guest for Component {
fn name() -> String {
wasi::cli::environment::get_environment()
.into_iter()
.find(|(k, _)| k == "NAME")
.map(|(_, v)| v)
.unwrap_or("World".to_string())
}
}
calls into the name
component and returns a greeting.
world greeter {
import name: func() -> string;
export greet: func() -> string;
}
impl Guest for Component {
fn greet() -> String {
let name = name();
format!("Hello, {name}!")
}
}
NAME=Stu ./cli.fish
outputs:
Hello, Stu!
NAME=Stu ./http.fish
Then in a new terminal:
curl http://localhost:8080
outputs:
Hello, Stu!