Skip to content

Commit 15dee2f

Browse files
committed
feat(redis): implement redis in wasip2
Signed-off-by: Andrew Steurer <[email protected]>
1 parent 1a3e34e commit 15dee2f

File tree

7 files changed

+442
-1
lines changed

7 files changed

+442
-1
lines changed

v3/examples/mqtt-outbound/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module github.com/http_go
1+
module github.com/spinframework/spin-go-sdk/v3/examples/mqtt-outbound
22

33
go 1.24
44

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Requirements
2+
- Latest version of [TinyGo](https://tinygo.org/getting-started/)
3+
- Latest version of [Docker](https://docs.docker.com/get-started/get-docker/)
4+
5+
# Usage
6+
7+
In one terminal window, run:
8+
```sh
9+
docker run -p 6379:6379 redis:8.2
10+
```
11+
12+
In another terminal, you'll run your Spin app:
13+
```sh
14+
spin up --build
15+
```
16+
17+
In yet another terminal, you'll interact with the Spin app:
18+
```sh
19+
curl localhost:3000
20+
```

v3/examples/redis-outbound/go.mod

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module github.com/spinframework/spin-go-sdk/v3/examples/redis-outbound
2+
3+
go 1.24
4+
5+
require github.com/spinframework/spin-go-sdk/v3 v3.0.0
6+
7+
require (
8+
github.com/julienschmidt/httprouter v1.3.0 // indirect
9+
go.bytecodealliance.org/cm v0.2.2 // indirect
10+
)
11+
12+
replace github.com/spinframework/spin-go-sdk/v3 => ../../

v3/examples/redis-outbound/go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U=
2+
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
3+
go.bytecodealliance.org/cm v0.2.2 h1:M9iHS6qs884mbQbIjtLX1OifgyPG9DuMs2iwz8G4WQA=
4+
go.bytecodealliance.org/cm v0.2.2/go.mod h1:JD5vtVNZv7sBoQQkvBvAAVKJPhR/bqBH7yYXTItMfZI=

v3/examples/redis-outbound/main.go

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
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.IsBytes() {
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() {}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
spin_manifest_version = 2
2+
3+
[application]
4+
name = "go-redis-outbound-example"
5+
version = "0.1.0"
6+
authors = ["Andrew Steurer <[email protected]>"]
7+
description = "Using Spin with Redis"
8+
9+
[[trigger.http]]
10+
route = "/"
11+
component = "redis-outbound"
12+
13+
[component.redis-outbound]
14+
source = "main.wasm"
15+
environment = { REDIS_ENDPOINT = "redis://localhost:6379" }
16+
allowed_outbound_hosts = ["redis://localhost:6379"]
17+
18+
[component.redis-outbound.build]
19+
command = "tinygo build -target=wasip2 --wit-package $(go list -mod=readonly -m -f '{{.Dir}}' github.com/spinframework/spin-go-sdk/v3)/wit --wit-world http-trigger -gc=leaking -o main.wasm main.go"
20+
watch = ["**/*.go", "go.mod"]

0 commit comments

Comments
 (0)