https://github.com/wasmcloud/component-sdk-go
SDK for writing wasmCloud components in Go.
https://github.com/wasmcloud/component-sdk-go
golang wasi wasmcloud webassembly
Last synced: 29 days ago
JSON representation
SDK for writing wasmCloud components in Go.
- Host: GitHub
- URL: https://github.com/wasmcloud/component-sdk-go
- Owner: wasmCloud
- License: apache-2.0
- Created: 2024-09-10T17:00:21.000Z (8 months ago)
- Default Branch: main
- Last Pushed: 2024-11-06T15:27:42.000Z (6 months ago)
- Last Synced: 2024-11-06T16:34:13.371Z (6 months ago)
- Topics: golang, wasi, wasmcloud, webassembly
- Language: Go
- Homepage:
- Size: 384 KB
- Stars: 8
- Watchers: 2
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
# Go Component SDK
> [!IMPORTANT]
> We have consolidated all of the wasmCloud Go SDKs and tooling under [github.com/wasmCloud/go](https://github.com/wasmCloud/go) where the future work and improvements will be done.[](https://pkg.go.dev/go.wasmcloud.dev/component)
The Go Component SDK provides a set of packages to simplify the development of WebAssembly components targeting the [wasmCloud](https://wasmcloud.com) host runtime.
Writing a wasmCloud Capability Provider? Check out the [Go Provider SDK](https://github.com/wasmCloud/provider-sdk-go).
# Setup
Requires:
* [`tinygo`](https://tinygo.org/getting-started/install/) 0.33.0 or above.
* [`wasm-tools`](https://github.com/bytecodealliance/wasm-tools?tab=readme-ov-file#installation) 1.220.0 or above.Import `go.wasmcloud.dev/component` in your Go module.
```bash
go get go.wasmcloud.dev/[email protected]
```Import the SDK WIT. In `wit/deps.toml`:
```toml
wasmcloud-component = "https://github.com/wasmCloud/component-sdk-go/archive/v0.0.5.tar.gz"
```
Run `wit-deps` to update your wit dependencies.
And in your world definition:
```
include wasmcloud:component-go/[email protected];
```
# Adapters
## net/wasihttp
The `wasihttp` package provides an implementation of `http.Handler` backed by `wasi:http`, as well as a `http.RoundTripper` backed by `wasi:http`.
### http.Handler
`wasihttp.Handle` registers an `http.Handler` to be served at a given path, converting `wasi:http` requests/responses into standard `http.Request` and `http.ResponseWriter` objects.
```go
package mainimport (
"net/http"
"go.wasmcloud.dev/component/net/wasihttp"
)func httpServe(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello, world!"))
}func init() {
// request will be fulfilled via wasi:http/incoming-handler
wasihttp.HandleFunc(httpServe)
}
```### http.RoundTripper
```go
package mainimport (
"net/http"
"go.wasmcloud.dev/component/net/wasihttp"
)var (
wasiTransport = &wasihttp.Transport{}
httpClient = &http.Client{Transport: wasiTransport}
)func httpClient() {
// request will be fulfilled via wasi:http/outgoing-handler
httpClient.Get("http://example.com")
}
```## log/wasilog
The `wasilog` package provides an implementation of `slog.Handler` backed by `wasi:logging`.
Sample usage:
```go
package mainimport (
"log/slog"
"go.wasmcloud.dev/component/log/wasilog"
)func wasilog() {
logger := slog.New(wasilog.DefaultOptions().NewHandler())logger.Info("Hello")
logger.Info("Hello", "planet", "Earth")
logger.Info("Hello", slog.String("planet", "Earth"))
}
```See `wasilog.Options` for log level & other configuration options.
## Community
Similar projects:
- [rajatjindal/wasi-go-sdk](https://github.com/rajatjindal/wasi-go-sdk)
- [dev-wasm/dev-wasm-go](https://github.com/dev-wasm/dev-wasm-go)
- [Mossaka/hello-wasi-http-tinygo](https://github.com/Mossaka/hello-wasi-http-tinygo)