11package main
22
33import (
4+ "fmt"
45 "net/http"
56 "os"
6- "strconv"
77 "reflect"
8- "fmt"
8+ "sort"
9+ "strconv"
910
10- "golang.org/x/exp/slices"
1111 spin_http "github.com/fermyon/spin/sdk/go/http"
1212 "github.com/fermyon/spin/sdk/go/redis"
1313)
1414
1515func init () {
1616
1717 // handler for the http trigger
18- spin_http .Handle (func (w http.ResponseWriter , r * http.Request ) {
18+ spin_http .Handle (func (w http.ResponseWriter , _ * http.Request ) {
1919
2020 // addr is the environment variable set in `spin.toml` that points to the
2121 // address of the Redis server.
@@ -28,19 +28,21 @@ func init() {
2828 // payload is the data publish to the redis channel.
2929 payload := []byte (`Hello redis from tinygo!` )
3030
31- if err := redis .Publish (addr , channel , payload ); err != nil {
31+ rdb := redis .NewClient (addr )
32+
33+ if err := rdb .Publish (channel , payload ); err != nil {
3234 http .Error (w , err .Error (), http .StatusInternalServerError )
3335 return
3436 }
3537
3638 // set redis `mykey` = `myvalue`
37- if err := redis .Set (addr , "mykey" , []byte ("myvalue" )); err != nil {
39+ if err := rdb .Set ("mykey" , []byte ("myvalue" )); err != nil {
3840 http .Error (w , err .Error (), http .StatusInternalServerError )
3941 return
4042 }
4143
4244 // get redis payload for `mykey`
43- if payload , err := redis .Get (addr , "mykey" ); err != nil {
45+ if payload , err := rdb .Get ("mykey" ); err != nil {
4446 http .Error (w , err .Error (), http .StatusInternalServerError )
4547 return
4648 } else {
@@ -50,7 +52,7 @@ func init() {
5052 }
5153
5254 // incr `spin-go-incr` by 1
53- if payload , err := redis .Incr (addr , "spin-go-incr" ); err != nil {
55+ if payload , err := rdb .Incr ("spin-go-incr" ); err != nil {
5456 http .Error (w , err .Error (), http .StatusInternalServerError )
5557 return
5658 } else {
@@ -60,27 +62,27 @@ func init() {
6062 }
6163
6264 // delete `spin-go-incr` and `mykey`
63- if payload , err := redis .Del (addr , [] string { "spin-go-incr" , "mykey" , "non-existing-key" } ); err != nil {
65+ if payload , err := rdb .Del ("spin-go-incr" , "mykey" , "non-existing-key" ); err != nil {
6466 http .Error (w , err .Error (), http .StatusInternalServerError )
6567 } else {
6668 w .Write ([]byte ("deleted keys num: " ))
6769 w .Write ([]byte (strconv .FormatInt (payload , 10 )))
6870 w .Write ([]byte ("\n " ))
6971 }
7072
71- if _ , err := redis .Sadd (addr , "myset" , [] string { "foo" , "bar" } ); err != nil {
73+ if _ , err := rdb .Sadd ("myset" , "foo" , "bar" ); err != nil {
7274 http .Error (w , err .Error (), http .StatusInternalServerError )
7375 return
7476 }
7577
7678 {
7779 expected := []string {"bar" , "foo" }
78- payload , err := redis .Smembers (addr , "myset" )
80+ payload , err := rdb .Smembers ("myset" )
7981 if err != nil {
8082 http .Error (w , err .Error (), http .StatusInternalServerError )
8183 return
8284 }
83- slices . Sort (payload )
85+ sort . Strings (payload )
8486 if ! reflect .DeepEqual (payload , expected ) {
8587 http .Error (
8688 w ,
@@ -95,14 +97,14 @@ func init() {
9597 }
9698 }
9799
98- if _ , err := redis .Srem (addr , "myset" , [] string { "bar" } ); err != nil {
100+ if _ , err := rdb .Srem ("myset" , "bar" ); err != nil {
99101 http .Error (w , err .Error (), http .StatusInternalServerError )
100102 return
101103 }
102104
103105 {
104106 expected := []string {"foo" }
105- if payload , err := redis .Smembers (addr , "myset" ); err != nil {
107+ if payload , err := rdb .Smembers ("myset" ); err != nil {
106108 http .Error (w , err .Error (), http .StatusInternalServerError )
107109 return
108110 } else if ! reflect .DeepEqual (payload , expected ) {
@@ -119,27 +121,24 @@ func init() {
119121 }
120122 }
121123
122- message := redis.RedisParameter {Kind : redis .RedisParameterKindBinary , Val : []byte ("message" )}
123- hello := redis.RedisParameter {Kind : redis .RedisParameterKindBinary , Val : []byte ("hello" )}
124- if _ , err := redis .Execute (addr , "set" , []redis.RedisParameter {message , hello }); err != nil {
124+ if _ , err := rdb .Execute ("set" , "message" , "hello" ); err != nil {
125125 http .Error (w , err .Error (), http .StatusInternalServerError )
126126 return
127127 }
128128
129- world := redis.RedisParameter {Kind : redis .RedisParameterKindBinary , Val : []byte (" world" )}
130- if _ , err := redis .Execute (addr , "append" , []redis.RedisParameter {message , world }); err != nil {
129+ if _ , err := rdb .Execute ("append" , "message" , " world" ); err != nil {
131130 http .Error (w , err .Error (), http .StatusInternalServerError )
132131 return
133132 }
134133
135- if payload , err := redis .Execute (addr , "get" , []redis. RedisParameter { message } ); err != nil {
134+ if payload , err := rdb .Execute ("get" , " message" ); err != nil {
136135 http .Error (w , err .Error (), http .StatusInternalServerError )
137136 return
138137 } else if ! reflect .DeepEqual (
139138 payload ,
140- []redis.RedisResult {redis. RedisResult {
141- Kind : redis .RedisResultKindBinary ,
142- Val : []byte ("hello world" ),
139+ []* redis.Result { {
140+ Kind : redis .ResultKindBinary ,
141+ Val : []byte ("hello world" ),
143142 }}) {
144143 http .Error (w , "unexpected GET result" , http .StatusInternalServerError )
145144 return
0 commit comments