-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
52 lines (41 loc) · 1 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package main
import (
"context"
"fmt"
"io"
"net/http"
"github.com/wasm-outbound-http-examples/go-plugin/protobufs"
)
func main() {
ctx := context.Background()
p, err := protobufs.NewFetchServicePlugin(ctx)
if err != nil {
panic(err)
}
fetchPlugin, err := p.Load(ctx, "plugin/plugin.wasm", myHostFunctions{})
if err != nil {
panic(err)
}
defer fetchPlugin.Close(ctx)
response, err := fetchPlugin.Fetch(ctx, &protobufs.HttpGetRequest{
Url: "https://httpbin.org/anything",
})
if err != nil {
panic(err)
}
fmt.Println("text:\n" + string(response.GetResponse()))
}
type myHostFunctions struct{}
var _ protobufs.HostFunctions = (*myHostFunctions)(nil)
func (myHostFunctions) HttpGet(_ context.Context, req *protobufs.HttpGetRequest) (*protobufs.HttpGetResponse, error) {
res, err := http.Get(req.Url)
if err != nil {
return nil, err
}
defer res.Body.Close()
buf, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
return &protobufs.HttpGetResponse{Response: buf}, nil
}