|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "net/http" |
| 6 | + "os" |
| 7 | + "reflect" |
| 8 | + "sort" |
| 9 | + "strconv" |
| 10 | + |
| 11 | + spin_http "github.com/spinframework/spin-go-sdk/v3/http" |
| 12 | + "github.com/spinframework/spin-go-sdk/v3/redis" |
| 13 | +) |
| 14 | + |
| 15 | +func init() { |
| 16 | + |
| 17 | + // handler for the http trigger |
| 18 | + spin_http.Handle(func(w http.ResponseWriter, _ *http.Request) { |
| 19 | + |
| 20 | + // addr is the environment variable set in `spin.toml` that points to the |
| 21 | + // address of the Redis server. |
| 22 | + addr := os.Getenv("REDIS_ADDRESS") |
| 23 | + |
| 24 | + // channel is the environment variable set in `spin.toml` that specifies |
| 25 | + // the Redis channel that the component will publish to. |
| 26 | + channel := os.Getenv("REDIS_CHANNEL") |
| 27 | + |
| 28 | + // payload is the data publish to the redis channel. |
| 29 | + payload := []byte(`Hello redis from tinygo!`) |
| 30 | + |
| 31 | + rdb, err := redis.NewClient(addr) |
| 32 | + if err != nil { |
| 33 | + http.Error(w, err.Error(), http.StatusInternalServerError) |
| 34 | + return |
| 35 | + } |
| 36 | + |
| 37 | + if err := rdb.Publish(channel, payload); err != nil { |
| 38 | + http.Error(w, err.Error(), http.StatusInternalServerError) |
| 39 | + return |
| 40 | + } |
| 41 | + |
| 42 | + // set redis `mykey` = `myvalue` |
| 43 | + if err := rdb.Set("mykey", []byte("myvalue")); err != nil { |
| 44 | + http.Error(w, err.Error(), http.StatusInternalServerError) |
| 45 | + return |
| 46 | + } |
| 47 | + |
| 48 | + // get redis payload for `mykey` |
| 49 | + if payload, err := rdb.Get("mykey"); err != nil { |
| 50 | + http.Error(w, err.Error(), http.StatusInternalServerError) |
| 51 | + return |
| 52 | + } else { |
| 53 | + w.Write([]byte("mykey value was: ")) |
| 54 | + w.Write(payload) |
| 55 | + w.Write([]byte("\n")) |
| 56 | + } |
| 57 | + |
| 58 | + // incr `spin-go-incr` by 1 |
| 59 | + if payload, err := rdb.Incr("spin-go-incr"); err != nil { |
| 60 | + http.Error(w, err.Error(), http.StatusInternalServerError) |
| 61 | + return |
| 62 | + } else { |
| 63 | + w.Write([]byte("spin-go-incr value: ")) |
| 64 | + w.Write([]byte(strconv.FormatInt(payload, 10))) |
| 65 | + w.Write([]byte("\n")) |
| 66 | + } |
| 67 | + |
| 68 | + // delete `spin-go-incr` and `mykey` |
| 69 | + if payload, err := rdb.Del("spin-go-incr", "mykey", "non-existing-key"); err != nil { |
| 70 | + http.Error(w, err.Error(), http.StatusInternalServerError) |
| 71 | + } else { |
| 72 | + w.Write([]byte("deleted keys num: ")) |
| 73 | + w.Write([]byte(strconv.FormatInt(int64(payload), 10))) |
| 74 | + w.Write([]byte("\n")) |
| 75 | + } |
| 76 | + |
| 77 | + if _, err := rdb.Sadd("myset", "foo", "bar"); err != nil { |
| 78 | + http.Error(w, err.Error(), http.StatusInternalServerError) |
| 79 | + return |
| 80 | + } |
| 81 | + |
| 82 | + { |
| 83 | + expected := []string{"bar", "foo"} |
| 84 | + payload, err := rdb.Smembers("myset") |
| 85 | + if err != nil { |
| 86 | + http.Error(w, err.Error(), http.StatusInternalServerError) |
| 87 | + return |
| 88 | + } |
| 89 | + sort.Strings(payload) |
| 90 | + if !reflect.DeepEqual(payload, expected) { |
| 91 | + http.Error( |
| 92 | + w, |
| 93 | + fmt.Sprintf( |
| 94 | + "unexpected SMEMBERS result: expected %v, got %v", |
| 95 | + expected, |
| 96 | + payload, |
| 97 | + ), |
| 98 | + http.StatusInternalServerError, |
| 99 | + ) |
| 100 | + return |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + if _, err := rdb.Srem("myset", "bar"); err != nil { |
| 105 | + http.Error(w, err.Error(), http.StatusInternalServerError) |
| 106 | + return |
| 107 | + } |
| 108 | + |
| 109 | + { |
| 110 | + expected := []string{"foo"} |
| 111 | + if payload, err := rdb.Smembers("myset"); err != nil { |
| 112 | + http.Error(w, err.Error(), http.StatusInternalServerError) |
| 113 | + return |
| 114 | + } else if !reflect.DeepEqual(payload, expected) { |
| 115 | + http.Error( |
| 116 | + w, |
| 117 | + fmt.Sprintf( |
| 118 | + "unexpected SMEMBERS result: expected %v, got %v", |
| 119 | + expected, |
| 120 | + payload, |
| 121 | + ), |
| 122 | + http.StatusInternalServerError, |
| 123 | + ) |
| 124 | + return |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + if _, err := rdb.Execute("set", "message", "hello"); err != nil { |
| 129 | + http.Error(w, err.Error(), http.StatusInternalServerError) |
| 130 | + return |
| 131 | + } |
| 132 | + |
| 133 | + if _, err := rdb.Execute("append", "message", " world"); err != nil { |
| 134 | + http.Error(w, err.Error(), http.StatusInternalServerError) |
| 135 | + return |
| 136 | + } |
| 137 | + |
| 138 | + if payload, err := rdb.Execute("get", "message"); err != nil { |
| 139 | + http.Error(w, err.Error(), http.StatusInternalServerError) |
| 140 | + return |
| 141 | + } else if !reflect.DeepEqual( |
| 142 | + payload, |
| 143 | + []*redis.Result{{ |
| 144 | + Kind: redis.ResultKindBinary, |
| 145 | + Val: []byte("hello world"), |
| 146 | + }}) { |
| 147 | + |
| 148 | + http.Error(w, "unexpected GET result", http.StatusInternalServerError) |
| 149 | + fmt.Println() |
| 150 | + return |
| 151 | + } |
| 152 | + }) |
| 153 | +} |
| 154 | + |
| 155 | +func main() {} |
0 commit comments