Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
d7c7c4f
feat(sdk): add support for http trigger with wasip2 support
rajatjindal Sep 1, 2024
383ffc5
Merge pull request #8 from rajatjindal/wasip2-http
adamreese Feb 12, 2025
2757940
update go-modules and cm package. regen bindings
rajatjindal Apr 2, 2025
6c3e074
Merge pull request #16 from rajatjindal/switch-org
rajatjindal Apr 2, 2025
3cd22af
feat(wasip2/kv): add support for wasip2 kv
rajatjindal Apr 2, 2025
5b03b6d
install wasm-tools and v3.2.0 of spin
rajatjindal Apr 2, 2025
9f90e21
Merge pull request #17 from rajatjindal/spin-w2-kv
rajatjindal Apr 3, 2025
8c008a4
feat(wasip2/llm): add support for llm
rajatjindal Apr 2, 2025
3b8bb70
Merge pull request #18 from rajatjindal/spin-w2-llm
rajatjindal Apr 11, 2025
72c0f22
add variables support for wasip2 sdk
rajatjindal Apr 3, 2025
8ffe6d3
Merge pull request #19 from rajatjindal/spin-w2-variables
rajatjindal Apr 22, 2025
df221c7
Move v2 to v3
adamreese Jun 11, 2025
a65f13a
Merge pull request #23 from adamreese/ref/wasip2-v3
adamreese Jul 10, 2025
ea81e64
feat(mqtt): implement mqtt in wasip2
asteurer Aug 6, 2025
1a3e34e
Merge pull request #32 from asteurer/wasip2-mqtt
adamreese Aug 6, 2025
52313d0
feat(redis): implement redis in wasip2
asteurer Aug 25, 2025
aaeb45a
Merge pull request #35 from asteurer/wasip2-redis
adamreese Aug 27, 2025
b616203
feat(mysql): implement mysql in wasip2
asteurer Aug 27, 2025
a52a86a
Merge pull request #31 from asteurer/wasip2-mysql
adamreese Aug 28, 2025
f407c8f
feat(wasip2): regenerate bindings with componentize-go
asteurer Jan 20, 2026
51d5378
Merge pull request #40 from asteurer/migrate-wasip2
dicej Jan 20, 2026
af71a11
fix: update componentize-go endpoint
asteurer Jan 27, 2026
b8cb553
Merge pull request #43 from asteurer/migrate-wasip2
adamreese Jan 28, 2026
12995fb
Merge remote-tracking branch 'origin/main' into wasip2
dicej Mar 24, 2026
d172d4b
revert mysql.go changes which caused test regression
dicej Mar 24, 2026
593ea45
move v3 into root, fix broken stuff, etc.
dicej Mar 25, 2026
9c9da3b
update componentize-go for CI
dicej Mar 25, 2026
419156b
remove unused file
dicej Mar 25, 2026
d881ce5
move redis_internal to inbound_redis/internal
dicej Mar 25, 2026
4e50c99
s/tinygo/Go/ in redis-outbound example
dicej Mar 25, 2026
e4867dc
update `componentize-go` and make use of new features
dicej Mar 25, 2026
5b1210e
upgrade `componentize-go` again to further simplify examples and tests
dicej Mar 26, 2026
65425aa
remove unnecessary WriteHeader statements
dicej Mar 26, 2026
aca9fe2
add note to readme about temporary `componentize-go` fork
dicej Mar 27, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
28 changes: 12 additions & 16 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,24 @@ jobs:
steps:
- uses: actions/checkout@v4

- name: Setup Go
- name: Install Go
uses: actions/setup-go@v5
with:
go-version: '1.23'
go-version: '1.25'

- name: Setup TinyGo
uses: acifani/setup-tinygo@v2
with:
tinygo-version: '0.35.0'
- name: Install latest Rust stable toolchain
uses: dtolnay/rust-toolchain@stable

- name: Setup Spin
uses: fermyon/actions/spin/setup@v1
with:
version: "v3.1.2"
- name: Install componentize-go
# TODO: Switch to an upstream (pre-built) release once new one is
# available and includes
# https://github.com/bytecodealliance/componentize-go/pull/35
run: cargo install --locked --git https://github.com/dicej/componentize-go --rev fad2c00f

- name: Setup Wasmtime
uses: bytecodealliance/actions/wasmtime/setup@v1
- name: Install Spin
uses: fermyon/actions/spin/setup@v1
with:
version: "28.0.0"

- name: Run unit tests
run: make test
version: "v3.6.2"

- name: Run integration tests
run: make test-integration
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
.spin
main.wasm
*.wasm
*/sdk-version-go.c
14 changes: 14 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
## Generating the WIT bindings

Whenever WIT files are changed, added to, or removed from the `wit` directory, the bindings in `internal` should be regenerated.

### Prerequisites

- Make
- BASH or compatible shell
- [**componentize-go**](https://github.com/bytecodealliance/componentize-go) - Latest version

### Run
```sh
make regenerate-bindings
```
113 changes: 23 additions & 90 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,94 +1,27 @@
VERSION = 2.3.0-pre0

# ----------------------------------------------------------------------
# Test
# ----------------------------------------------------------------------
.PHONY: test
test:
tinygo test -target=wasip1 -gc=leaking -buildmode=c-shared -v ./http
tinygo test -target=wasip1 -gc=leaking -buildmode=c-shared -v ./redis

.PHONY: test-integration
test-integration:
go test -v -count=1 .

# ----------------------------------------------------------------------
# Generate C bindings
# ----------------------------------------------------------------------
GENERATED_SPIN_VARIABLES = variables/spin-config.c variables/spin-config.h
GENERATED_OUTBOUND_HTTP = http/wasi-outbound-http.c http/wasi-outbound-http.h
GENERATED_SPIN_HTTP = http/spin-http.c http/spin-http.h
GENERATED_OUTBOUND_REDIS = redis/outbound-redis.c redis/outbound-redis.h
GENERATED_SPIN_REDIS = redis/spin-redis.c redis/spin-redis.h
GENERATED_KEY_VALUE = kv/key-value.c kv/key-value.h
GENERATED_SQLITE = sqlite/sqlite.c sqlite/sqlite.h
GENERATED_LLM = llm/llm.c llm/llm.h
GENERATED_OUTBOUND_MYSQL = mysql/outbound-mysql.c mysql/outbound-mysql.h
GENERATED_OUTBOUND_PG = pg/outbound-pg.c pg/outbound-pg.h

SDK_VERSION_SOURCE_FILE = sdk_version/sdk-version-go-template.c

# NOTE: Please update this list if you add a new directory to the SDK:
SDK_VERSION_DEST_FILES = variables/sdk-version-go.c http/sdk-version-go.c \
kv/sdk-version-go.c redis/sdk-version-go.c \
sqlite/sdk-version-go.c llm/sdk-version-go.c

# NOTE: To generate the C bindings you need to install a forked version of wit-bindgen.
#
# cargo install wit-bindgen-cli --git https://github.com/fermyon/wit-bindgen-backport --rev "b89d5079ba5b07b319631a1b191d2139f126c976"
#
.PHONY: generate
generate: $(GENERATED_OUTBOUND_HTTP) $(GENERATED_SPIN_HTTP)
generate: $(GENERATED_OUTBOUND_REDIS) $(GENERATED_SPIN_REDIS)
generate: $(GENERATED_SPIN_VARIABLES) $(GENERATED_KEY_VALUE)
generate: $(GENERATED_SQLITE) $(GENERATED_LLM)
generate: $(GENERATED_OUTBOUND_MYSQL) $(GENERATED_OUTBOUND_PG)
generate: $(SDK_VERSION_DEST_FILES)

$(SDK_VERSION_DEST_FILES): $(SDK_VERSION_SOURCE_FILE)
export commit="$$(git rev-parse HEAD)"; \
sed -e "s/{{VERSION}}/${VERSION}/" -e "s/{{COMMIT}}/$${commit}/" < $< > $@

$(GENERATED_SPIN_VARIABLES):
wit-bindgen c --import wit/spin-config.wit --out-dir ./variables

$(GENERATED_OUTBOUND_HTTP):
wit-bindgen c --import wit/wasi-outbound-http.wit --out-dir ./http

$(GENERATED_SPIN_HTTP):
wit-bindgen c --export wit/spin-http.wit --out-dir ./http

$(GENERATED_OUTBOUND_REDIS):
wit-bindgen c --import wit/outbound-redis.wit --out-dir ./redis

$(GENERATED_SPIN_REDIS):
wit-bindgen c --export wit/spin-redis.wit --out-dir ./redis

$(GENERATED_KEY_VALUE):
wit-bindgen c --import wit/key-value.wit --out-dir ./kv

$(GENERATED_SQLITE):
wit-bindgen c --import wit/sqlite.wit --out-dir ./sqlite

$(GENERATED_LLM):
wit-bindgen c --import wit/llm.wit --out-dir ./llm

$(GENERATED_OUTBOUND_MYSQL):
wit-bindgen c --import wit/outbound-mysql.wit --out-dir ./mysql

$(GENERATED_OUTBOUND_PG):
wit-bindgen c --import wit/outbound-pg.wit --out-dir ./pg

# ----------------------------------------------------------------------
# Cleanup
# ----------------------------------------------------------------------
.PHONY: clean
clean:
rm -f $(GENERATED_SPIN_CONFIG)
rm -f $(GENERATED_OUTBOUND_HTTP) $(GENERATED_SPIN_HTTP)
rm -f $(GENERATED_OUTBOUND_REDIS) $(GENERATED_SPIN_REDIS)
rm -f $(GENERATED_KEY_VALUE) $(GENERATED_SQLITE)
rm -f $(GENERATED_LLM)
rm -f $(GENERATED_OUTBOUND_MYSQL)
rm -f $(GENERATED_SDK_VERSION)
rm -f $(SDK_VERSION_DEST_FILES)
.PHONY: build-examples
build-examples:
for x in examples/*; do echo $$x && (cd $$x && spin build) || exit 1; done

.PHONY: regenerate-bindings
regenerate-bindings:
find $$(pwd)/internal/ \
-mindepth 1 \
-maxdepth 1 \
-type d \
! -name 'db' \
! -name 'export_fermyon_spin_inbound_redis' \
! -name 'export_wasi_http_0_2_0_incoming_handler' \
-exec rm -rf {} +
componentize-go \
--ignore-toml-files \
-w "fermyon:spin/http-trigger@3.0.0" \
-w "fermyon:spin/redis-trigger" \
-d ./wit \
bindings \
--format \
-o internal \
--pkg-name github.com/spinframework/spin-go-sdk/v3/internal
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
## The Go SDK for Spin

This is an SDK for developing [Spin](https://github.com/spinframework/spin) applications using the Go programming language.

> Note: This SDK temporarily relies on [a fork](https://github.com/dicej/componentize-go) of [componentize-go](https://github.com/bytecodealliance/componentize-go) until [this PR](https://github.com/bytecodealliance/componentize-go/pull/35) has been accepted. For the time being, please install [this build](https://github.com/dicej/componentize-go/releases/tag/canary).

## Example

```go
package main

import (
"fmt"
"net/http"

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

func init() {
spinhttp.Handle(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Hello, Spin!")
})
}

func main() {}
```

See the [examples](./examples) directory for more examples.
2 changes: 2 additions & 0 deletions componentize-go.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
worlds = [ "fermyon:spin/http-trigger@3.0.0" ]
wit_paths = [ "wit" ]
34 changes: 14 additions & 20 deletions examples/http-outbound/README.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
# Making outbound HTTP requests from TinyGo Spin components
# Making outbound HTTP requests from Go Spin components

The TinyGo SDK for building Spin components allows us to granularly allow
components to send HTTP requests to certain hosts. This is configured in
`spin.toml`.

> For more information and examples for using TinyGo with WebAssembly, check
> [the official TinyGo documentation](https://tinygo.org/docs/guides/webassembly/)
> and
> [the Wasm examples](https://github.com/tinygo-org/tinygo/tree/release/src/examples/wasm).
The Go SDK for building Spin components allows us to granularly allow components
to send HTTP requests to certain hosts. This is configured in `spin.toml`.

Creating and sending HTTP requests from Spin components closely follows the Go
`net/http` API. See [tinygo-hello/main.go](./tinygo-hello/main.go).
`net/http` API. See [hello/main.go](./hello/main.go).

Building this as a WebAssembly module can be done using the `tinygo` compiler:
Building this as a WebAssembly module can be done using `spin build`:

```shell
$ spin build
Building component outbound-http-to-same-app with `tinygo build -target=wasip1 -gc=leaking -buildmode=c-shared -no-debug -o main.wasm .`
Working directory: "./outbound-http-to-same-app"
Building component tinygo-hello with `tinygo build -target=wasip1 -gc=leaking -buildmode=c-shared -no-debug -o main.wasm .`
Working directory: "./tinygo-hello"
Building component http-to-same-app with `componentize-go --world http-trigger build`
Working directory: "./http-to-same-app"
Building component hello with `componentize-go --world http-trigger build`
Working directory: "./hello"
Finished building all Spin components
```

Expand All @@ -30,11 +24,11 @@ HTTP requests to, otherwise sending the request results in an error:
Cannot send HTTP request: Destination not allowed: <URL>
```

The `tinygo-hello` component has the following allowed hosts set:
The `hello` component has the following allowed hosts set:

```toml
[component.tinygo-hello]
source = "tinygo-hello/main.wasm"
[component.hello]
source = "hello/main.wasm"
allowed_outbound_hosts = [
"https://random-data-api.fermyon.app",
"https://postman-echo.com",
Expand All @@ -43,7 +37,7 @@ allowed_outbound_hosts = [

And the `outbound-http-to-same-app` uses the dedicated `self` keyword to enable making
a request to another component in this same app, via a relative path (in this case, the component
is `tinygo-hello` at `/hello`):
is `hello` at `/hello`):

```toml
[component.outbound-http-to-same-app]
Expand All @@ -70,7 +64,7 @@ date: Thu, 26 Oct 2023 18:26:17 GMT
...
```

As well as via the `/outbound-http-to-same-app` path to verify outbound http to the `tinygo-hello` component:
As well as via the `/outbound-http-to-same-app` path to verify outbound http to the `hello` component:

```shell
$ curl -i localhost:3000/outbound-http-to-same-app
Expand Down
14 changes: 8 additions & 6 deletions examples/http-outbound/hello/go.mod
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
module examples/http-outbound/hello
module github.com/spinframework/spin-go-sdk/v3/examples/http-outbound/hello

go 1.20
go 1.25.5

require github.com/spinframework/spin-go-sdk/v2 v2.2.1
require (
github.com/spinframework/spin-go-sdk/v3 v3.0.0
github.com/julienschmidt/httprouter v1.3.0 // indirect
go.bytecodealliance.org/pkg v0.2.1
)

require github.com/julienschmidt/httprouter v1.3.0 // indirect

replace github.com/spinframework/spin-go-sdk/v2 v2.2.1 => ../../../
replace github.com/spinframework/spin-go-sdk/v3 => ../../../
2 changes: 2 additions & 0 deletions examples/http-outbound/hello/go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +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/pkg v0.2.1 h1:TdRagooIcCW3UmlKqVO4cDR3GNDyfDnbiBzGI6TOvyg=
go.bytecodealliance.org/pkg v0.2.1/go.mod h1:OjA+V8g3uUFixeCKFfamm6sYhTJdg8fvwEdJ2GO0GSk=
2 changes: 1 addition & 1 deletion examples/http-outbound/hello/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"net/http"
"os"

spinhttp "github.com/spinframework/spin-go-sdk/v2/http"
spinhttp "github.com/spinframework/spin-go-sdk/v3/http"
)

func init() {
Expand Down
14 changes: 8 additions & 6 deletions examples/http-outbound/http-to-same-app/go.mod
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
module examples/http-outbound/http-to-same-app
module github.com/spinframework/spin-go-sdk/v3/examples/http-outbound/hello

go 1.20
go 1.25.5

require github.com/spinframework/spin-go-sdk/v2 v2.2.1
require (
github.com/spinframework/spin-go-sdk/v3 v3.0.0
github.com/julienschmidt/httprouter v1.3.0 // indirect
go.bytecodealliance.org/pkg v0.2.1
)

require github.com/julienschmidt/httprouter v1.3.0 // indirect

replace github.com/spinframework/spin-go-sdk/v2 v2.2.1 => ../../../
replace github.com/spinframework/spin-go-sdk/v3 => ../../../
2 changes: 2 additions & 0 deletions examples/http-outbound/http-to-same-app/go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +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/pkg v0.2.1 h1:TdRagooIcCW3UmlKqVO4cDR3GNDyfDnbiBzGI6TOvyg=
go.bytecodealliance.org/pkg v0.2.1/go.mod h1:OjA+V8g3uUFixeCKFfamm6sYhTJdg8fvwEdJ2GO0GSk=
2 changes: 1 addition & 1 deletion examples/http-outbound/http-to-same-app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"net/http"

spinhttp "github.com/spinframework/spin-go-sdk/v2/http"
spinhttp "github.com/spinframework/spin-go-sdk/v3/http"
)

func init() {
Expand Down
7 changes: 4 additions & 3 deletions examples/http-outbound/spin.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ spin_manifest_version = 2

[application]
authors = ["Fermyon Engineering <engineering@fermyon.com>"]
description = "A simple Spin application written in (Tiny)Go that performs outbound HTTP requests."
description = "A simple Spin application written in Go that performs outbound HTTP requests."
name = "http-outbound-example"
version = "1.0.0"

Expand All @@ -22,12 +22,13 @@ allowed_outbound_hosts = [
]
[component.hello.build]
workdir = "hello"
command = "tinygo build -target=wasip1 -gc=leaking -buildmode=c-shared -no-debug -o main.wasm ."
command = "componentize-go build"

[component.http-to-same-app]
source = "http-to-same-app/main.wasm"
# Use self to make outbound requests to components in the same Spin application.
allowed_outbound_hosts = ["http://self"]
[component.http-to-same-app.build]
workdir = "http-to-same-app"
command = "tinygo build -target=wasip1 -gc=leaking -buildmode=c-shared -no-debug -o main.wasm ."
command = "componentize-go build"

5 changes: 2 additions & 3 deletions examples/http-router/README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# Spin component in TinyGo using the Spin router
# Spin component in Go using the Spin router

```shell
$ go mod tidy
$ RUST_LOG=spin=trace spin build --up
$ spin build --up
```

The application can now receive requests on `http://localhost:3000`:
Expand Down
14 changes: 8 additions & 6 deletions examples/http-router/go.mod
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
module examples/http-router
module github.com/spinframework/spin-go-sdk/v3/examples/http-router

go 1.20
go 1.25.5

require github.com/spinframework/spin-go-sdk/v2 v2.2.1
require (
github.com/spinframework/spin-go-sdk/v3 v3.0.0
github.com/julienschmidt/httprouter v1.3.0 // indirect
go.bytecodealliance.org/pkg v0.2.1
)

require github.com/julienschmidt/httprouter v1.3.0 // indirect

replace github.com/spinframework/spin-go-sdk/v2 v2.2.1 => ../../
replace github.com/spinframework/spin-go-sdk/v3 => ../../
Loading
Loading