Skip to content

Commit

Permalink
feat: add new Go bindings to perform HTTP requests (#173)
Browse files Browse the repository at this point in the history
* feat: add new Go bindings to perform HTTP requests

* docs: improve go kit README by adding the code-block types

Co-authored-by: Rafael Fernández López <[email protected]>

---------

Co-authored-by: Rafael Fernández López <[email protected]>
  • Loading branch information
Angelmmiguel and ereslibre authored Jul 11, 2023
1 parent c41a8a5 commit 6c9655e
Show file tree
Hide file tree
Showing 16 changed files with 1,150 additions and 2 deletions.
4 changes: 2 additions & 2 deletions docs/docs/languages/go.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ In this example, the worker will get a request and print all the related informa
1. Add the Wasm Workers Server Go dependency
```
go get -u github.com/vmware-labs/wasm-workers-server/kits/go/worker
go get -u github.com/vmware-labs/wasm-workers-server/kits/go/worker@v1.3.0
```
1. Create a `worker.go` file with the following contents:
Expand Down Expand Up @@ -142,7 +142,7 @@ To add a KV store to your worker, follow these steps:
1. Add the Wasm Workers Server Go dependency
```
go get -u github.com/vmware-labs/wasm-workers-server/kits/go/worker
go get -u github.com/vmware-labs/wasm-workers-server/kits/go/worker@v1.3.0
```
1. Create a `worker-kv.go` file with the following contents:
Expand Down
12 changes: 12 additions & 0 deletions examples/go-basic/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module go-basic

go 1.20

require github.com/vmware-labs/wasm-workers-server v1.3.0

require (
github.com/tidwall/gjson v1.14.4 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.1 // indirect
github.com/tidwall/sjson v1.2.5 // indirect
)
12 changes: 12 additions & 0 deletions examples/go-basic/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
github.com/tidwall/gjson v1.14.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/gjson v1.14.4 h1:uo0p8EbA09J7RQaflQ1aBRffTR7xedD2bcIVSYxLnkM=
github.com/tidwall/gjson v1.14.4/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4=
github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tidwall/sjson v1.2.5 h1:kLy8mja+1c9jlljvWTlSazM7cKDRfJuR/bOJhcY5NcY=
github.com/tidwall/sjson v1.2.5/go.mod h1:Fvgq9kS/6ociJEDnK0Fk1cpYF4FIW6ZF7LAe+6jwd28=
github.com/vmware-labs/wasm-workers-server v1.3.0 h1:Sm+Ycp327kRIaQIlWDXjKUITnCsaZaEqUDnPQZGPrKk=
github.com/vmware-labs/wasm-workers-server v1.3.0/go.mod h1:cigUhoitjUTLsUzR4+q0cz2FymdvJtfrfIS2hYAj69c=
31 changes: 31 additions & 0 deletions examples/go-fetch/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Go fetch example

Compile a Go worker to WebAssembly and run it in Wasm Workers Server. It performs a POST call to the [JSON placeholder API](https://jsonplaceholder.typicode.com/).

## Prerequisites

* Wasm Workers Server (wws):

```shell-session
curl -fsSL https://workers.wasmlabs.dev/install | bash
```

* [Go](https://go.dev/)
* [TinyGo](https://tinygo.org/getting-started/install/)

## Build

```shell-session
tinygo build -o index.wasm -target wasi main.go
```

## Run

```shell-session
wws .
```

## Resources

* [Go documentation](https://workers.wasmlabs.dev/docs/languages/go)
* [Announcing Go support for Wasm Workers Server](https://wasmlabs.dev/articles/go-support-on-wasm-workers-server/)
15 changes: 15 additions & 0 deletions examples/go-fetch/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module go-basic

go 1.20

require github.com/vmware-labs/wasm-workers-server v1.3.0

require (
github.com/tidwall/gjson v1.14.4 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.1 // indirect
github.com/tidwall/sjson v1.2.5 // indirect
)

replace github.com/vmware-labs/wasm-workers-server => ../../
replace github.com/vmware-labs/wasm-workers-server/kits/go/worker/bindings => ../../kits/go/worker/bindings
12 changes: 12 additions & 0 deletions examples/go-fetch/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
github.com/tidwall/gjson v1.14.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/gjson v1.14.4 h1:uo0p8EbA09J7RQaflQ1aBRffTR7xedD2bcIVSYxLnkM=
github.com/tidwall/gjson v1.14.4/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4=
github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tidwall/sjson v1.2.5 h1:kLy8mja+1c9jlljvWTlSazM7cKDRfJuR/bOJhcY5NcY=
github.com/tidwall/sjson v1.2.5/go.mod h1:Fvgq9kS/6ociJEDnK0Fk1cpYF4FIW6ZF7LAe+6jwd28=
github.com/vmware-labs/wasm-workers-server v1.3.0 h1:Sm+Ycp327kRIaQIlWDXjKUITnCsaZaEqUDnPQZGPrKk=
github.com/vmware-labs/wasm-workers-server v1.3.0/go.mod h1:cigUhoitjUTLsUzR4+q0cz2FymdvJtfrfIS2hYAj69c=
44 changes: 44 additions & 0 deletions examples/go-fetch/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package main

import (
"bytes"
"io"
"net/http"

"github.com/vmware-labs/wasm-workers-server/kits/go/worker"

"github.com/tidwall/sjson"
)

func main() {
worker.ServeFunc(func(w http.ResponseWriter, r *http.Request) {
// Build a JSON body
body, _ := sjson.Set("", "title", "New POST!")
body, _ = sjson.Set(body, "body", "This is the body")
body, _ = sjson.Set(body, "userId", 1)

url := "https://jsonplaceholder.typicode.com/posts"

// Create the request
req, err := http.NewRequest(http.MethodPost, url, bytes.NewBufferString(body))
if err != nil {
panic(err)
}
req.Header.Set("Content-Type", "application/json")

res, err := worker.SendHttpRequest(req)
if err != nil {
panic(err)
}

// Read the response
resBody, err := io.ReadAll(res.Body)
if err != nil {
panic(err)
}
res.Body.Close()

w.Header().Set("x-generated-by", "wasm-workers-server")
w.Write([]byte(resBody))
})
}
61 changes: 61 additions & 0 deletions kits/go/worker/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Go kit

This folder contains the Go kit or SDK for Wasm Workers Server. Currently, it uses the regular STDIN / STDOUT approach to receive the request and provide the response. In the latest version we introduced the new HTTP bindings to send HTTP requests from inside the worker.

## Bindings

Wasm Workers Server is on the road to adopt Wasm components, but it's not there yet. However, we started adopting WIT to generate the bindings for the different languages.

The host (Wasm Workers Server) and other languages like Rust and JavaScript rely on [wit-bindgen v0.2](https://github.com/bytecodealliance/wit-bindgen/tree/v0.2.0). However, the Go bindings were not available on that version so it caused some extra work to generate the Go bindings.

These are the steps to recreate the current Go bindings:

- Clone the wit-binding repository and checkout to the [35cb45f2](https://github.com/bytecodealliance/wit-bindgen/commit/35cb45f25eb113b54406f269778d46a37716a7c5) commit (between v0.6 - v0.7). This commit produces compatible binding identifiers and fixes an error with the types on the generated C / Go code:

```shell-session
git clone https://github.com/bytecodealliance/wit-bindgen/tree/main && \
git checkout 35cb45f25eb113b54406f269778d46a37716a7c5
```
- Compile the project:
```shell-session
cargo build --release
```
- Change your current directory to `wasm-workers-server/kits/go/worker/bindings`.
- Now, you need to use the compiled `wit-bindgen`:
```shell-session
~/YOUR_LOCATION/wit-bindgen/target/release/wit-bindgen tiny-go ../../../../wit/go-ephemeral/
```
- Just note that we're using a specific `wit` folder for Go. The reason is that the syntax changed from v0.3. We will consolidate it once we adopt components.
- Edit the `bindings.c` file to define the `canonical_abi_realloc` and `canonical_abi_free`. wit-bindgen v0.2 expects these methods to be exported. However, the first method was renamed to `cabi_realloc` and the second was removed on v3.0. To fix it, locate the `__attribute__((__weak__, __export_name__("cabi_realloc")))` and replace it with the following two methods:
```c
__attribute__((__weak__, __export_name__("canonical_abi_realloc"))) void *cabi_realloc(void *ptr, size_t old_size, size_t align, size_t new_size)
{
if (new_size == 0)
return (void *)align;
void *ret = realloc(ptr, new_size);
if (!ret)
abort();
return ret;
}
__attribute__((weak, export_name("canonical_abi_free"))) void canonical_abi_free(
void *ptr,
size_t size,
size_t align)
{
free(ptr);
}
```
- Done!
## References
* [Go documentation](https://workers.wasmlabs.dev/docs/languages/go)
* [Announcing Go Support for Wasm Workers Server](https://wasmlabs.dev/articles/go-support-on-wasm-workers-server/)
Loading

0 comments on commit 6c9655e

Please sign in to comment.