Go HTTP Key-Value CRUD
This example is a WebAssembly component that demonstrates simple CRUD operations (Create, Read, Update, Destroy) with the wasi:keyvalue/store
interface.
📦 Dependencies
Before starting, ensure that you have the following installed in addition to the Go (1.23+) toolchain:
tinygo
for compiling Go (always use the latest version)wasm-tools
for Go bindings- wasmCloud Shell (
wash
) for building and running the components and wasmCloud environment
👟 Run the example
Clone the wasmCloud/go repository:
git clone https://github.com/wasmCloud/go.git
Change directory to examples/component/http-keyvalue-crud
:
cd examples/component/http-keyvalue-crud
In addition to the standard elements of a Go project, the example directory includes the following files and directories:
build/
: Target directory for compiled.wasm
binariesgen/
: Target directory for Go bindings of interfaceswit/
: Directory for WebAssembly Interface Type (WIT) packages that define interfacesbindings.wadge.go
: Automatically generated test bindingswadm.yaml
: Declarative application manifestwasmcloud.lock
: Automatically generated lockfile for WIT packageswasmcloud.toml
: Configuration file for a wasmCloud application
Start a local development loop
Run wash dev
to start a local development loop:
wash dev
The wash dev
command will:
- Start a local wasmCloud environment
- Build this component
- Deploy your application and all dependencies to run the application locally
- Watch your code for changes and re-deploy when necessary.
Once the application is deployed, open another terminal tab. To ensure that the application is deployed, you can use wash app list
:
wash app list
Send a request
The user provides keys as query strings and values as JSON payloads, with the application performing CRUD operations according to the HTTP method of the request.
To Create (or Update) a value:
curl -X POST localhost:8000/crud/key -d '{"foo": "bar", "woo": "hoo"}'
{"message":"Set key", "value":"{"foo": "bar", "woo": "hoo"}"}
To Read a value:
curl localhost:8000/crud/key
{"message":"Got key", "value":"{"foo": "bar", "woo": "hoo"}"}
To Destroy a value:
curl -X DELETE localhost:8000/crud/key
{"message":"Deleted key"}
Read through the comments in main.go
for step-by-step explanation of how the example works.
Clean up
You can stop the wash dev
process with Ctrl-C
.
📖 Further reading
When running this example with the wash dev
command, wasmCloud uses its included NATS key-value store to back key-value operations, but the application could use another store like Redis with no change to the Go code.
You can learn more about capabilities like key-value storage are fulfilled by swappable providers in the wasmCloud Quickstart.