Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion v3/examples/mqtt-outbound/go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/http_go
module github.com/spinframework/spin-go-sdk/v3/examples/mqtt-outbound

go 1.24

Expand Down
20 changes: 20 additions & 0 deletions v3/examples/redis-outbound/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Requirements
- Latest version of [TinyGo](https://tinygo.org/getting-started/)
- Latest version of [Docker](https://docs.docker.com/get-started/get-docker/)

# Usage

In one terminal window, you'll run a Redis container:
```sh
docker run -p 6379:6379 redis:8.2
```

In another terminal, you'll run your Spin app:
```sh
spin up --build
```

In yet another terminal, you'll interact with the Spin app:
```sh
curl localhost:3000
```
12 changes: 12 additions & 0 deletions v3/examples/redis-outbound/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module github.com/spinframework/spin-go-sdk/v3/examples/redis-outbound

go 1.24

require github.com/spinframework/spin-go-sdk/v3 v3.0.0

require (
github.com/julienschmidt/httprouter v1.3.0 // indirect
go.bytecodealliance.org/cm v0.2.2 // indirect
)

replace github.com/spinframework/spin-go-sdk/v3 => ../../
4 changes: 4 additions & 0 deletions v3/examples/redis-outbound/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U=
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
go.bytecodealliance.org/cm v0.2.2 h1:M9iHS6qs884mbQbIjtLX1OifgyPG9DuMs2iwz8G4WQA=
go.bytecodealliance.org/cm v0.2.2/go.mod h1:JD5vtVNZv7sBoQQkvBvAAVKJPhR/bqBH7yYXTItMfZI=
155 changes: 155 additions & 0 deletions v3/examples/redis-outbound/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
package main

import (
"fmt"
"net/http"
"os"
"reflect"
"sort"
"strconv"

spin_http "github.com/spinframework/spin-go-sdk/v3/http"
"github.com/spinframework/spin-go-sdk/v3/redis"
)

func init() {

// handler for the http trigger
spin_http.Handle(func(w http.ResponseWriter, _ *http.Request) {

// addr is the environment variable set in `spin.toml` that points to the
// address of the Redis server.
addr := os.Getenv("REDIS_ADDRESS")

// channel is the environment variable set in `spin.toml` that specifies
// the Redis channel that the component will publish to.
channel := os.Getenv("REDIS_CHANNEL")

// payload is the data publish to the redis channel.
payload := []byte(`Hello redis from tinygo!`)

rdb, err := redis.NewClient(addr)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

if err := rdb.Publish(channel, payload); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

// set redis `mykey` = `myvalue`
if err := rdb.Set("mykey", []byte("myvalue")); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

// get redis payload for `mykey`
if payload, err := rdb.Get("mykey"); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
} else {
w.Write([]byte("mykey value was: "))
w.Write(payload)
w.Write([]byte("\n"))
}

// incr `spin-go-incr` by 1
if payload, err := rdb.Incr("spin-go-incr"); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
} else {
w.Write([]byte("spin-go-incr value: "))
w.Write([]byte(strconv.FormatInt(payload, 10)))
w.Write([]byte("\n"))
}

// delete `spin-go-incr` and `mykey`
if payload, err := rdb.Del("spin-go-incr", "mykey", "non-existing-key"); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
} else {
w.Write([]byte("deleted keys num: "))
w.Write([]byte(strconv.FormatInt(int64(payload), 10)))
w.Write([]byte("\n"))
}

if _, err := rdb.Sadd("myset", "foo", "bar"); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

{
expected := []string{"bar", "foo"}
payload, err := rdb.Smembers("myset")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
sort.Strings(payload)
if !reflect.DeepEqual(payload, expected) {
http.Error(
w,
fmt.Sprintf(
"unexpected SMEMBERS result: expected %v, got %v",
expected,
payload,
),
http.StatusInternalServerError,
)
return
}
}

if _, err := rdb.Srem("myset", "bar"); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

{
expected := []string{"foo"}
if payload, err := rdb.Smembers("myset"); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
} else if !reflect.DeepEqual(payload, expected) {
http.Error(
w,
fmt.Sprintf(
"unexpected SMEMBERS result: expected %v, got %v",
expected,
payload,
),
http.StatusInternalServerError,
)
return
}
}

if _, err := rdb.Execute("set", "message", "hello"); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

if _, err := rdb.Execute("append", "message", " world"); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

if payload, err := rdb.Execute("get", "message"); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
} else if !reflect.DeepEqual(
payload,
[]*redis.Result{{
Kind: redis.ResultKindBinary,
Val: []byte("hello world"),
}}) {

http.Error(w, "unexpected GET result", http.StatusInternalServerError)
fmt.Println()
return
}
})
}

func main() {}
20 changes: 20 additions & 0 deletions v3/examples/redis-outbound/spin.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
spin_manifest_version = 2

[application]
name = "go-redis-outbound-example"
version = "0.1.0"
authors = ["Andrew Steurer <[email protected]>"]
description = "Using Spin with Redis"

[[trigger.http]]
route = "/"
component = "redis-outbound"

[component.redis-outbound]
source = "main.wasm"
environment = { REDIS_ADDRESS = "redis://localhost:6379", REDIS_CHANNEL = "messages" }
allowed_outbound_hosts = ["redis://localhost:6379"]

[component.redis-outbound.build]
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"
watch = ["**/*.go", "go.mod"]
Loading