|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "net/http" |
| 6 | + "os" |
| 7 | + "strings" |
| 8 | + |
| 9 | + spinhttp "github.com/spinframework/spin-go-sdk/v3/http" |
| 10 | + "github.com/spinframework/spin-go-sdk/v3/redis" |
| 11 | +) |
| 12 | + |
| 13 | +func init() { |
| 14 | + spinhttp.Handle(func(w http.ResponseWriter, r *http.Request) { |
| 15 | + redisEndpoint := os.Getenv("REDIS_ENDPOINT") |
| 16 | + conn, err := redis.Open(redisEndpoint) |
| 17 | + if err != nil { |
| 18 | + w.WriteHeader(http.StatusInternalServerError) |
| 19 | + w.Write([]byte("failed to open redis connection: " + err.Error())) |
| 20 | + return |
| 21 | + } |
| 22 | + |
| 23 | + // Set command |
| 24 | + if err := conn.Set("key1", []byte("value1")); err != nil { |
| 25 | + w.WriteHeader(http.StatusInternalServerError) |
| 26 | + w.Write([]byte("failed to perform set operation: " + err.Error())) |
| 27 | + return |
| 28 | + } |
| 29 | + |
| 30 | + // Get command |
| 31 | + value, err := conn.Get("key1") |
| 32 | + if err != nil { |
| 33 | + w.WriteHeader(http.StatusInternalServerError) |
| 34 | + w.Write([]byte("failed to perform get operation: " + err.Error())) |
| 35 | + return |
| 36 | + } |
| 37 | + |
| 38 | + fmt.Println("key1: " + string(value)) |
| 39 | + |
| 40 | + // Incr command |
| 41 | + if _, err := conn.Incr("incr1"); err != nil { |
| 42 | + w.WriteHeader(http.StatusInternalServerError) |
| 43 | + w.Write([]byte("failed to perform incr operation: " + err.Error())) |
| 44 | + return |
| 45 | + } |
| 46 | + |
| 47 | + // Incrementing the same key twice |
| 48 | + incrVal, err := conn.Incr("incr1") |
| 49 | + if err != nil { |
| 50 | + w.WriteHeader(http.StatusInternalServerError) |
| 51 | + w.Write([]byte("failed to perform incr operation: " + err.Error())) |
| 52 | + return |
| 53 | + } |
| 54 | + |
| 55 | + fmt.Printf("incr1: %d\n", incrVal) |
| 56 | + |
| 57 | + // Del command |
| 58 | + numKeysDeleted, err := conn.Del([]string{"incr1", "key1"}) |
| 59 | + if err != nil { |
| 60 | + w.WriteHeader(http.StatusInternalServerError) |
| 61 | + w.Write([]byte("failed to perform del operation: " + err.Error())) |
| 62 | + return |
| 63 | + } |
| 64 | + |
| 65 | + fmt.Printf("deleted %d keys\n", numKeysDeleted) |
| 66 | + |
| 67 | + // Sadd command |
| 68 | + languages := []string{"Go", "Rust", "JavaScript", "Python"} |
| 69 | + setName := "programming_languages" |
| 70 | + numAdded, err := conn.Sadd(setName, languages) |
| 71 | + if err != nil { |
| 72 | + w.WriteHeader(http.StatusInternalServerError) |
| 73 | + w.Write([]byte("failed to perform sadd operation: " + err.Error())) |
| 74 | + return |
| 75 | + } |
| 76 | + |
| 77 | + fmt.Printf("added %d items to the %s set\n", numAdded, setName) |
| 78 | + |
| 79 | + // Smembers command |
| 80 | + setEntries, err := conn.Smembers(setName) |
| 81 | + if err != nil { |
| 82 | + w.WriteHeader(http.StatusInternalServerError) |
| 83 | + w.Write([]byte("failed to perform smembers operation: " + err.Error())) |
| 84 | + return |
| 85 | + } |
| 86 | + |
| 87 | + fmt.Println(setName + ": " + strings.Join(setEntries, ", ")) |
| 88 | + |
| 89 | + // Srem command |
| 90 | + numRemoved, err := conn.Srem(setName, []string{"JavaScript"}) |
| 91 | + if err != nil { |
| 92 | + w.WriteHeader(http.StatusInternalServerError) |
| 93 | + w.Write([]byte("failed to perform srem operation: " + err.Error())) |
| 94 | + return |
| 95 | + } |
| 96 | + |
| 97 | + fmt.Printf("deleted %d entries from set %s\n", numRemoved, setName) |
| 98 | + |
| 99 | + // Execute command |
| 100 | + if _, err := conn.Execute("SET", "execKey", "execValue"); err != nil { |
| 101 | + w.WriteHeader(http.StatusInternalServerError) |
| 102 | + w.Write([]byte("failed to perform execute set operation: " + err.Error())) |
| 103 | + return |
| 104 | + } |
| 105 | + |
| 106 | + // Validating initial Execute command |
| 107 | + execResults, err := conn.Execute("GET", "execKey") |
| 108 | + if err != nil { |
| 109 | + w.WriteHeader(http.StatusInternalServerError) |
| 110 | + w.Write([]byte("failed to perform execute get operation: " + err.Error())) |
| 111 | + return |
| 112 | + } |
| 113 | + |
| 114 | + for _, r := range execResults { |
| 115 | + if r.IsBinary() { |
| 116 | + data, ok := r.AsBytes() |
| 117 | + if !ok { |
| 118 | + w.WriteHeader(http.StatusInternalServerError) |
| 119 | + w.Write([]byte("failed to convert result type to bytes")) |
| 120 | + return |
| 121 | + } |
| 122 | + |
| 123 | + fmt.Println("execKey: " + string(data)) |
| 124 | + |
| 125 | + } else { |
| 126 | + w.WriteHeader(http.StatusInternalServerError) |
| 127 | + w.Write([]byte("incorrect result type received")) |
| 128 | + return |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + w.WriteHeader(http.StatusOK) |
| 133 | + w.Write([]byte("Hello, Spin!")) |
| 134 | + }) |
| 135 | +} |
| 136 | + |
| 137 | +func main() {} |
0 commit comments