Wazero
This example comes from Wazero's "import go" example and shows how to define, import and call a wasm blob from Go and run it on Unikraft Cloud. To run this it, follow these steps:
-
Install the
kraftCLI tool and a container runtime engine, for example Docker. -
Clone the
examplesrepository andcdinto theexamples/wazero-import-go/directory:
Code(bash)
Make sure to log into Unikraft Cloud by setting your token and a metro close to you.
This guide uses fra (Frankfurt, ๐ฉ๐ช):
Code(bash)
When done, invoke the following command to deploy this app on Unikraft Cloud:
Code(bash)
The output shows the instance address and other details:
Code(ansi)
In this case, the instance name is wazero-import-go-r4dx8 and the address is https://cool-morning-camrrhsa.fra.unikraft.app.
They're different for each run.
Use curl to query the Unikraft Cloud instance of the Go/wazero server:
Code(bash)
Code(text)
You can list information about the instance by running:
Code(bash)
Code(text)
When done, you can remove the instance:
Code(bash)
Background
WebAssembly has neither a mechanism to get the current year, nor one to print to the console, so this example defines these in Go.
Like Go, WebAssembly functions are namespaced into modules instead of packages.
With Go only exported functions can import into another module.
age-calculator.go shows how to export functions using HostModuleBuilder and how a WebAssembly module defined in its text format imports it.
This only uses the text format for demonstration purposes, to show you what's going on.
It's likely, you will use another language to compile a Wasm (WebAssembly Module) binary, such as TinyGo.
Regardless of how wasm produces, the export/import mechanics are the same!
Using WASI
WebAssembly System Interface (WASI) is a modular system interface for WebAssembly. This uses an ad-hoc Go-defined function to print to the console. An emerging specification standardizes system calls (like Go's x/sys) called WebAssembly System Interface (WASI). While this isn't yet a W3C standard, wazero includes a wasi package.
Customize your app
To customize the app, update the files in the repository, listed below:
agecalculator.go: The Go web server that calls the WASM/Wazero blog for the age calculationKraftfile: the Unikraft Cloud specificationDockerfile: the Docker-specified app filesystem
Learn more
Use the --help option for detailed information on using Unikraft Cloud:
Code(bash)
Or visit the CLI Reference.