This repository is a sample to demonstrate how to use WebAssembly components to build a softwaresystem based on independent, interchangeable components.
It is based on Rust and uses a cargo subcommand to create the components.
To create a new component with the name 'greeter' from scratch, you use the following command:
cargo component new greeter --lib
In directory of the component you can find a folder with the name wit and in the file word.wit is a definition of the interface in the WIT-language.
To build the component use the command cargo component build
.
Another component 'greeter-client' can use the previous created component. So we create another one, this time without the --lib
switch:
cargo component new greeter-client
In order to use the first component, the second needs to import it in thw world.wit:
world app {
import component:greeter/[email protected];
}
Ensure that the 'greeter-client' component can find the first wit-file by adding the location needs to the Cargo.toml:
[package.metadata.component.target.dependencies]
"component:greeter" = { path = "../greeter/wit" }
To compose the system you need the tool WAC.
From within the greeter-client directory you execute this command:
wac plug target/wasm32-wasip1/debug/greeter-client.wasm --plug ../greeter/target/wasm32-wasip1/debug/greeter.wasm -o ./composed-greeter.wasm
The system can be started with Wasmtime.
wasmtime ./composed-greeter.wasm
For composing the whole provided sample you can use this command:
wac plug target/wasm32-wasip1/debug/greeter-client.wasm --plug ../greeter/target/wasm32-wasip1/debug/greeter.wasm --plug ../even-odd/target/wasm32-wasip1/debug/evenodd.wasm -o ./composed-greeter.wasm