Skip to content

Commit ec559c2

Browse files
authored
Merge pull request #1197 from rajatjindal/redo-tinygo-ex
make go kv example same as rust
2 parents 4831c2f + 2a94105 commit ec559c2

File tree

1 file changed

+34
-36
lines changed

1 file changed

+34
-36
lines changed

examples/tinygo-key-value/main.go

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,70 @@
11
package main
22

33
import (
4+
"io"
45
"net/http"
5-
"reflect"
6-
"fmt"
76

87
spin_http "github.com/fermyon/spin/sdk/go/http"
98
"github.com/fermyon/spin/sdk/go/key_value"
109
)
1110

1211
func init() {
13-
1412
// handler for the http trigger
1513
spin_http.Handle(func(w http.ResponseWriter, r *http.Request) {
16-
store, err := key_value.Open("default");
14+
store, err := key_value.Open("default")
1715
if err != nil {
1816
http.Error(w, err.Error(), http.StatusInternalServerError)
1917
return
2018
}
21-
2219
defer key_value.Close(store)
2320

24-
if err := key_value.Set(store, "foo", []byte("bar")); err != nil {
21+
body, err := io.ReadAll(r.Body)
22+
if err != nil {
2523
http.Error(w, err.Error(), http.StatusInternalServerError)
2624
return
2725
}
2826

29-
{
30-
expected := []byte("bar")
31-
if value, err := key_value.Get(store, "foo"); err != nil {
27+
switch r.Method {
28+
case http.MethodPost:
29+
err := key_value.Set(store, r.URL.Path, body)
30+
if err != nil {
3231
http.Error(w, err.Error(), http.StatusInternalServerError)
3332
return
34-
} else if !reflect.DeepEqual(value, expected) {
35-
http.Error(
36-
w,
37-
fmt.Sprintf("expected %v, got %v", expected, value),
38-
http.StatusInternalServerError,
39-
)
33+
}
34+
35+
w.WriteHeader(http.StatusOK)
36+
case http.MethodGet:
37+
value, err := key_value.Get(store, r.URL.Path)
38+
if err != nil {
39+
http.Error(w, err.Error(), http.StatusInternalServerError)
4040
return
4141
}
42-
}
4342

44-
{
45-
expected := []string{"foo"}
46-
if value, err := key_value.GetKeys(store); err != nil {
43+
w.WriteHeader(http.StatusOK)
44+
w.Write(value)
45+
case http.MethodDelete:
46+
err := key_value.Delete(store, r.URL.Path)
47+
if err != nil {
4748
http.Error(w, err.Error(), http.StatusInternalServerError)
4849
return
49-
} else if !reflect.DeepEqual(value, expected) {
50-
http.Error(
51-
w,
52-
fmt.Sprintf("expected %v, got %v", expected, value),
53-
http.StatusInternalServerError,
54-
)
50+
}
51+
52+
w.WriteHeader(http.StatusOK)
53+
case http.MethodHead:
54+
exists, err := key_value.Exists(store, r.URL.Path)
55+
if err != nil {
56+
http.Error(w, err.Error(), http.StatusInternalServerError)
5557
return
5658
}
57-
}
5859

59-
if err := key_value.Delete(store, "foo"); err != nil {
60-
http.Error(w, err.Error(), http.StatusInternalServerError)
61-
return
62-
}
60+
if exists {
61+
w.WriteHeader(http.StatusOK)
62+
return
63+
}
6364

64-
if exists, err := key_value.Exists(store, "foo"); err != nil {
65-
http.Error(w, err.Error(), http.StatusInternalServerError)
66-
return
67-
} else if exists {
68-
http.Error(w, "key was not deleted as expected", http.StatusInternalServerError)
69-
return
65+
w.WriteHeader(http.StatusNotFound)
66+
default:
67+
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
7068
}
7169
})
7270
}

0 commit comments

Comments
 (0)