-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.go
84 lines (75 loc) · 1.85 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package main
import (
"fmt"
"io/ioutil"
"net/http"
"unsafe"
wasiclient "github.com/dev-wasm/dev-wasm-go/lib/http/client"
"github.com/dev-wasm/dev-wasm-go/lib/wasi/cli/run"
"go.bytecodealliance.org/cm"
)
// This is required for building the module for some reason
// I don't think it should be. I'm probably doing something
// wrong.
//
// fwiw, I think this is likely buggy and either leaks memory
// or has race conditions.
//
//go:wasmexport cabi_realloc
//export cabi_realloc
func wasmexport_cabi_realloc(ptr, oldSize, align, newSize uint32) uint32 {
if newSize == 0 {
return align
}
arr := make([]uint8, newSize)
newPtr := unsafe.Pointer(unsafe.SliceData(arr))
return uint32(uintptr(newPtr))
}
func printResponse(r *http.Response) {
fmt.Printf("Status: %d\n", r.StatusCode)
for k, v := range r.Header {
fmt.Printf("%s: %s\n", k, v[0])
}
body, err := ioutil.ReadAll(r.Body)
if err != nil {
panic(err.Error())
}
fmt.Printf("Body: \n%s\n", body)
}
func Run() cm.BoolResult {
main()
return cm.BoolResult(false)
}
func init() {
run.Exports.Run = Run
}
func main() {
client := &http.Client{
Transport: wasiclient.WasiRoundTripper{},
}
req, err := http.NewRequest("GET", "https://postman-echo.com/get", nil)
if err != nil {
panic(err.Error())
}
if req == nil {
panic("Nil request!")
}
res, err := client.Do(req)
if err != nil {
panic(err.Error())
}
defer res.Body.Close()
printResponse(res)
res, err = client.Post("https://postman-echo.com/post", "application/json", wasiclient.BodyReaderCloser([]byte("{\"foo\": \"bar\"}")))
if err != nil {
panic(err.Error())
}
defer res.Body.Close()
printResponse(res)
res, err = wasiclient.Put(client, "http://postman-echo.com/put", "application/json", wasiclient.BodyReaderCloser([]byte("{\"baz\": \"blah\"}")))
if err != nil {
panic(err.Error())
}
defer res.Body.Close()
printResponse(res)
}