Skip to content

Commit 4a4fdb3

Browse files
authored
feat(sdk/go): implement mysql SDK for TinyGo (#1794)
Signed-off-by: Patrick <[email protected]>
1 parent 503917f commit 4a4fdb3

File tree

11 files changed

+1008
-0
lines changed

11 files changed

+1008
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
main.wasm
2+
.spin/
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
CREATE TABLE pets (id INT PRIMARY KEY, name VARCHAR(100) NOT NULL, prey VARCHAR(100), is_finicky BOOL NOT NULL);
2+
INSERT INTO pets VALUES (1, 'Splodge', NULL, false);
3+
INSERT INTO pets VALUES (2, 'Kiki', 'Cicadas', false);
4+
INSERT INTO pets VALUES (3, 'Slats', 'Temptations', true);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module github.com/tinygo_mysql
2+
3+
go 1.20
4+
5+
require github.com/fermyon/spin/sdk/go v0.0.0
6+
7+
require github.com/julienschmidt/httprouter v1.3.0 // indirect
8+
9+
replace github.com/fermyon/spin/sdk/go v0.0.0 => ../../sdk/go/
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
github.com/fermyon/spin/sdk/go v1.4.1 h1:n8KTYTnkErTJdyMBBEtPmJe8dXrvMT6R7iVWbLRjq5E=
2+
github.com/fermyon/spin/sdk/go v1.4.1/go.mod h1:yb8lGesopgj/GwPzLPATxcOeqWZT/HjrzEFfwbztAXE=
3+
github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U=
4+
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"net/http"
7+
"os"
8+
9+
spinhttp "github.com/fermyon/spin/sdk/go/http"
10+
"github.com/fermyon/spin/sdk/go/mysql"
11+
)
12+
13+
type Pet struct {
14+
ID int64
15+
Name string
16+
Prey *string // nullable field must be a pointer
17+
IsFinicky bool
18+
}
19+
20+
func init() {
21+
spinhttp.Handle(func(w http.ResponseWriter, r *http.Request) {
22+
23+
// addr is the environment variable set in `spin.toml` that points to the
24+
// address of the Mysql server.
25+
addr := os.Getenv("DB_URL")
26+
27+
db := mysql.Open(addr)
28+
defer db.Close()
29+
30+
_, err := db.Query("REPLACE INTO pets VALUES (?, 'Maya', ?, ?);", 4, "bananas", true)
31+
if err != nil {
32+
http.Error(w, err.Error(), http.StatusInternalServerError)
33+
return
34+
}
35+
36+
rows, err := db.Query("SELECT * FROM pets")
37+
if err != nil {
38+
http.Error(w, err.Error(), http.StatusInternalServerError)
39+
return
40+
}
41+
42+
var pets []*Pet
43+
for rows.Next() {
44+
var pet Pet
45+
if err := rows.Scan(&pet.ID, &pet.Name, &pet.Prey, &pet.IsFinicky); err != nil {
46+
fmt.Println(err)
47+
}
48+
pets = append(pets, &pet)
49+
}
50+
json.NewEncoder(w).Encode(pets)
51+
})
52+
}
53+
54+
func main() {}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
spin_manifest_version = "1"
2+
authors = ["Patrick Jiang <[email protected]>"]
3+
description = ""
4+
name = "tinygo-mysql"
5+
trigger = { type = "http", base = "/" }
6+
version = "0.1.0"
7+
8+
[[component]]
9+
environment = { DB_URL = "mysql://spin:[email protected]/spin_dev" }
10+
id = "tinygo-mysql"
11+
source = "main.wasm"
12+
[component.trigger]
13+
route = "/..."
14+
[component.build]
15+
command = "tinygo build -target=wasi -gc=leaking -o main.wasm main.go"
16+
watch = ["**/*.go", "go.mod"]

sdk/go/Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ build-examples: $(EXAMPLES_DIR)/tinygo-redis/main.wasm
2929
build-examples: $(EXAMPLES_DIR)/tinygo-key-value/main.wasm
3030
build-examples: $(EXAMPLES_DIR)/tinygo-sqlite/main.wasm
3131
build-examples: $(EXAMPLES_DIR)/tinygo-llm/main.wasm
32+
build-examples: $(EXAMPLES_DIR)/tinygo-mysql/main.wasm
3233

3334
$(EXAMPLES_DIR)/%/main.wasm: $(EXAMPLES_DIR)/%/main.go
3435
tinygo build -target=wasi -gc=leaking -no-debug -o $@ $<
@@ -44,6 +45,7 @@ GENERATED_SPIN_REDIS = redis/spin-redis.c redis/spin-redis.h
4445
GENERATED_KEY_VALUE = key_value/key-value.c key_value/key-value.h
4546
GENERATED_SQLITE = sqlite/sqlite.c sqlite/sqlite.h
4647
GENERATED_LLM = llm/llm.c llm/llm.h
48+
GENERATED_OUTBOUND_MYSQL = mysql/outbound-mysql.c mysql/outbound-mysql.h
4749

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

@@ -61,6 +63,7 @@ generate: $(GENERATED_OUTBOUND_HTTP) $(GENERATED_SPIN_HTTP)
6163
generate: $(GENERATED_OUTBOUND_REDIS) $(GENERATED_SPIN_REDIS)
6264
generate: $(GENERATED_SPIN_CONFIG) $(GENERATED_KEY_VALUE)
6365
generate: $(GENERATED_SQLITE) $(GENERATED_LLM)
66+
generate: $(GENERATED_OUTBOUND_MYSQL)
6467
generate: $(SDK_VERSION_DEST_FILES)
6568

6669
$(SDK_VERSION_DEST_FILES): $(SDK_VERSION_SOURCE_FILE)
@@ -92,6 +95,9 @@ $(GENERATED_SQLITE):
9295
$(GENERATED_LLM):
9396
wit-bindgen c --import ../../wit/ephemeral/llm.wit --out-dir ./llm
9497

98+
$(GENERATED_OUTBOUND_MYSQL):
99+
wit-bindgen c --import ../../wit/ephemeral/outbound-mysql.wit --out-dir ./mysql
100+
95101
# ----------------------------------------------------------------------
96102
# Cleanup
97103
# ----------------------------------------------------------------------
@@ -102,6 +108,7 @@ clean:
102108
rm -f $(GENERATED_OUTBOUND_REDIS) $(GENERATED_SPIN_REDIS)
103109
rm -f $(GENERATED_KEY_VALUE) $(GENERATED_SQLITE)
104110
rm -f $(GENERATED_LLM)
111+
rm -f $(GENERATED_OUTBOUND_MYSQL)
105112
rm -f $(GENERATED_SDK_VERSION)
106113
rm -f http/testdata/http-tinygo/main.wasm
107114
rm -f $(EXAMPLES_DIR)/http-tinygo/main.wasm
@@ -111,4 +118,5 @@ clean:
111118
rm -f $(EXAMPLES_DIR)/tinygo-key-value/main.wasm
112119
rm -f $(EXAMPLES_DIR)/tinygo-sqlite/main.wasm
113120
rm -f $(EXAMPLES_DIR)/tinygo-llm/main.wasm
121+
rm -f $(EXAMPLES_DIR)/tinygo-outbound-mysql/main.wasm
114122
rm -f $(SDK_VERSION_DEST_FILES)

0 commit comments

Comments
 (0)