Skip to content

Commit 8ffe6d3

Browse files
authored
Merge pull request #19 from rajatjindal/spin-w2-variables
Add support for wasip2 variables
2 parents 3b8bb70 + 72c0f22 commit 8ffe6d3

File tree

12 files changed

+185
-52
lines changed

12 files changed

+185
-52
lines changed

v2/examples/variables/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/v2/examples/variables
2+
3+
go 1.24.1
4+
5+
require github.com/spinframework/spin-go-sdk/v2 v2.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/v2 => ../../

v2/examples/variables/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=

v2/examples/variables/main.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
7+
spinhttp "github.com/spinframework/spin-go-sdk/v2/http"
8+
"github.com/spinframework/spin-go-sdk/v2/variables"
9+
)
10+
11+
func init() {
12+
spinhttp.Handle(func(w http.ResponseWriter, r *http.Request) {
13+
14+
// Get variable value `message` defined in spin.toml.
15+
val, err := variables.Get("message")
16+
if err != nil {
17+
http.Error(w, err.Error(), http.StatusInternalServerError)
18+
return
19+
}
20+
fmt.Fprintln(w, "message: ", val)
21+
})
22+
}
23+
24+
func main() {}

v2/examples/variables/spin.toml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
spin_manifest_version = 2
2+
3+
[application]
4+
name = "variables-example"
5+
authors = ["Fermyon Engineering <[email protected]>"]
6+
description = "A simple Spin application written in (Tiny)Go."
7+
version = "1.0.0"
8+
9+
[variables]
10+
object = { default = "teapot" }
11+
12+
[[trigger.http]]
13+
route = "/..."
14+
component = "variables"
15+
16+
[component.variables]
17+
source = "main.wasm"
18+
[component.variables.variables]
19+
message = "I'm a {{object}}"
20+
21+
[component.hello.build]
22+
command = "tinygo build -target=wasip2 --wit-package $(go list -mod=readonly -m -f '{{.Dir}}' github.com/spinframework/spin-go-sdk/v2)/wit --wit-world http-trigger -gc=leaking -no-debug -o main.wasm main.go"

v2/go.mod

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,5 @@ go 1.23.2
44

55
require (
66
github.com/julienschmidt/httprouter v1.3.0
7-
go.bytecodealliance.org v0.6.2
87
go.bytecodealliance.org/cm v0.2.2
98
)
10-
11-
require (
12-
github.com/coreos/go-semver v0.3.1 // indirect
13-
github.com/docker/libtrust v0.0.0-20160708172513-aabc10ec26b7 // indirect
14-
github.com/klauspost/compress v1.17.11 // indirect
15-
github.com/opencontainers/go-digest v1.0.0 // indirect
16-
github.com/regclient/regclient v0.8.2 // indirect
17-
github.com/sirupsen/logrus v1.9.3 // indirect
18-
github.com/tetratelabs/wazero v1.9.0 // indirect
19-
github.com/ulikunitz/xz v0.5.12 // indirect
20-
github.com/urfave/cli/v3 v3.0.0-beta1 // indirect
21-
golang.org/x/mod v0.23.0 // indirect
22-
golang.org/x/sys v0.30.0 // indirect
23-
)

v2/go.sum

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,4 @@
1-
github.com/coreos/go-semver v0.3.1 h1:yi21YpKnrx1gt5R+la8n5WgS0kCrsPp33dmEyHReZr4=
2-
github.com/coreos/go-semver v0.3.1/go.mod h1:irMmmIw/7yzSRPWryHsK7EYSg09caPQL03VsM8rvUec=
3-
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4-
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
5-
github.com/docker/libtrust v0.0.0-20160708172513-aabc10ec26b7 h1:UhxFibDNY/bfvqU5CAUmr9zpesgbU6SWc8/B4mflAE4=
6-
github.com/docker/libtrust v0.0.0-20160708172513-aabc10ec26b7/go.mod h1:cyGadeNEkKy96OOhEzfZl+yxihPEzKnqJwvfuSUqbZE=
71
github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U=
82
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
9-
github.com/klauspost/compress v1.17.11 h1:In6xLpyWOi1+C7tXUUWv2ot1QvBjxevKAaI6IXrJmUc=
10-
github.com/klauspost/compress v1.17.11/go.mod h1:pMDklpSncoRMuLFrf1W9Ss9KT+0rH90U12bZKk7uwG0=
11-
github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=
12-
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
13-
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
14-
github.com/regclient/regclient v0.8.2 h1:23BQ3jWgKYHHIXUhp/S9laVJDHDoOQaQCzXMJ4undVE=
15-
github.com/regclient/regclient v0.8.2/go.mod h1:uGyetv0o6VLyRDjtfeBqp/QBwRLJ3Hcn07/+8QbhNcM=
16-
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
17-
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
18-
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
19-
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
20-
github.com/tetratelabs/wazero v1.9.0 h1:IcZ56OuxrtaEz8UYNRHBrUa9bYeX9oVY93KspZZBf/I=
21-
github.com/tetratelabs/wazero v1.9.0/go.mod h1:TSbcXCfFP0L2FGkRPxHphadXPjo1T6W+CseNNY7EkjM=
22-
github.com/ulikunitz/xz v0.5.12 h1:37Nm15o69RwBkXM0J6A5OlE67RZTfzUxTj8fB3dfcsc=
23-
github.com/ulikunitz/xz v0.5.12/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
24-
github.com/urfave/cli/v3 v3.0.0-beta1 h1:6DTaaUarcM0wX7qj5Hcvs+5Dm3dyUTBbEwIWAjcw9Zg=
25-
github.com/urfave/cli/v3 v3.0.0-beta1/go.mod h1:FnIeEMYu+ko8zP1F9Ypr3xkZMIDqW3DR92yUtY39q1Y=
26-
go.bytecodealliance.org v0.4.0 h1:SRwgZIcXR54AmbJg9Y3AMgDlZlvD8dffteBYW+nCD3k=
27-
go.bytecodealliance.org v0.4.0/go.mod h1:hkdjfgQ/bFZYUucnm9cn0Q8/SHO3iT0rzskYlkV4Jy0=
28-
go.bytecodealliance.org v0.6.2 h1:Jy4u5DVmSkXgsnwojBhJ+AD/YsJsR3VzVnxF0xRCqTQ=
29-
go.bytecodealliance.org v0.6.2/go.mod h1:gqjTJm0y9NSksG4py/lSjIQ/SNuIlOQ+hCIEPQwtJgA=
303
go.bytecodealliance.org/cm v0.2.2 h1:M9iHS6qs884mbQbIjtLX1OifgyPG9DuMs2iwz8G4WQA=
314
go.bytecodealliance.org/cm v0.2.2/go.mod h1:JD5vtVNZv7sBoQQkvBvAAVKJPhR/bqBH7yYXTItMfZI=
32-
golang.org/x/mod v0.23.0 h1:Zb7khfcRGKk+kqfxFaP5tZqCnDZMjC5VtUBs87Hr6QM=
33-
golang.org/x/mod v0.23.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY=
34-
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
35-
golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
36-
golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
37-
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
38-
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
39-
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

v2/integration_test.go

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,15 @@ type testSpin struct {
4242
cmd *exec.Cmd
4343
}
4444

45-
func startSpin(t *testing.T, dir string) *testSpin {
45+
func startSpin(t *testing.T, dir string, extraArgs ...string) *testSpin {
4646
buildApp(t, dir)
4747

4848
url := getFreePort(t)
4949

5050
// long timeout because... ci
5151
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute)
5252

53-
cmd := exec.CommandContext(ctx, spinBinary, "up", "--listen", url)
53+
cmd := exec.CommandContext(ctx, spinBinary, append([]string{"up", "--listen", url}, extraArgs...)...)
5454
cmd.Dir = dir
5555
stderr := new(bytes.Buffer)
5656
cmd.Stderr = stderr
@@ -144,6 +144,30 @@ func TestKeyValue(t *testing.T) {
144144
}
145145
}
146146

147+
func TestVariables(t *testing.T) {
148+
spin := startSpin(t, "variables/testdata/variables")
149+
defer spin.cancel()
150+
151+
resp := retryGet(t, spin.url)
152+
spin.cancel()
153+
if resp.Body == nil {
154+
t.Fatal("body is nil")
155+
}
156+
t.Log(resp.Status)
157+
b, err := io.ReadAll(resp.Body)
158+
resp.Body.Close()
159+
if err != nil {
160+
t.Fatal(err)
161+
}
162+
163+
// assert response body
164+
want := "message: I'm a teapot\n"
165+
got := string(b)
166+
if want != got {
167+
t.Fatalf("body is not equal: want = %q got = %q", want, got)
168+
}
169+
}
170+
147171
// TestBuildExamples ensures that the tinygo examples will build successfully.
148172
func TestBuildExamples(t *testing.T) {
149173
examples, err := os.ReadDir("examples")
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/v2/examples/variables
2+
3+
go 1.24.1
4+
5+
require github.com/spinframework/spin-go-sdk/v2 v2.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/v2 => ../../../
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=
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
7+
spinhttp "github.com/spinframework/spin-go-sdk/v2/http"
8+
"github.com/spinframework/spin-go-sdk/v2/variables"
9+
)
10+
11+
func init() {
12+
spinhttp.Handle(func(w http.ResponseWriter, r *http.Request) {
13+
// Get variable value `message` defined in spin.toml.
14+
val, err := variables.Get("message")
15+
if err != nil {
16+
http.Error(w, err.Error(), http.StatusInternalServerError)
17+
return
18+
}
19+
fmt.Fprintln(w, "message: ", val)
20+
})
21+
}
22+
23+
func main() {}

0 commit comments

Comments
 (0)