From d0f0f13a827e8d1af4fad589557cd22f0bd6febf Mon Sep 17 00:00:00 2001 From: Radu Matei Date: Wed, 9 Mar 2022 16:52:58 +0200 Subject: [PATCH] feat: initial add for (Tiny)Go SDK Signed-off-by: Radu Matei --- .gitignore | 1 + sdk/go/go.mod | 3 +++ sdk/go/http.go | 17 +++++++++++++++++ templates/spin-http-tinygo/Makefile | 7 +++++++ templates/spin-http-tinygo/go.mod | 11 +++++++++++ templates/spin-http-tinygo/main.go | 14 ++++++++++++++ templates/spin-http-tinygo/spin.toml | 13 +++++++++++++ 7 files changed, 66 insertions(+) create mode 100644 sdk/go/go.mod create mode 100644 sdk/go/http.go create mode 100644 templates/spin-http-tinygo/Makefile create mode 100644 templates/spin-http-tinygo/go.mod create mode 100644 templates/spin-http-tinygo/main.go create mode 100644 templates/spin-http-tinygo/spin.toml diff --git a/.gitignore b/.gitignore index 67c06653..0ec4bcaa 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ target cache node_modules ignored-assets +main.wasm diff --git a/sdk/go/go.mod b/sdk/go/go.mod new file mode 100644 index 00000000..7567384c --- /dev/null +++ b/sdk/go/go.mod @@ -0,0 +1,3 @@ +module github.com/fermyon/spin-sdk + +go 1.17 diff --git a/sdk/go/http.go b/sdk/go/http.go new file mode 100644 index 00000000..d700819e --- /dev/null +++ b/sdk/go/http.go @@ -0,0 +1,17 @@ +package spin_http + +import ( + "net/http" + "net/http/cgi" +) + +// The entrypoint handler for a Spin HTTP component. +// +// This is currently handled using CGI to form the request and reponse, +// but as Go implements support for the component model, the underlying +// implementation of this function can change, but the exported signature +// can continue to always be +// `func Handler(h func(w http.ResponseWriter, r *http.Request)) error`. +func Handler(h func(w http.ResponseWriter, r *http.Request)) error { + return cgi.Serve(http.HandlerFunc(h)) +} diff --git a/templates/spin-http-tinygo/Makefile b/templates/spin-http-tinygo/Makefile new file mode 100644 index 00000000..2da94ae2 --- /dev/null +++ b/templates/spin-http-tinygo/Makefile @@ -0,0 +1,7 @@ +.PHONY: build +build: + tinygo build -wasm-abi=generic -target=wasi -o main.wasm main.go + +. PHONY: serve +serve: + RUST_LOG=spin=trace spin up --file spin.toml diff --git a/templates/spin-http-tinygo/go.mod b/templates/spin-http-tinygo/go.mod new file mode 100644 index 00000000..ebd77e29 --- /dev/null +++ b/templates/spin-http-tinygo/go.mod @@ -0,0 +1,11 @@ +module github.com/fermyon/spin/templates/spin-http-tinygo + +go 1.17 + +require ( + github.com/fermyon/spin-sdk v0.0.0 +) + +replace ( + github.com/fermyon/spin-sdk v0.0.0 => ../../sdk/go/ +) diff --git a/templates/spin-http-tinygo/main.go b/templates/spin-http-tinygo/main.go new file mode 100644 index 00000000..536ce4c0 --- /dev/null +++ b/templates/spin-http-tinygo/main.go @@ -0,0 +1,14 @@ +package main + +import ( + "fmt" + "net/http" + + spin_http "github.com/fermyon/spin-sdk" +) + +func main() { + spin_http.Handler(func(w http.ResponseWriter, r *http.Request) { + fmt.Fprintln(w, "Hello, Fermyon!") + }) +} diff --git a/templates/spin-http-tinygo/spin.toml b/templates/spin-http-tinygo/spin.toml new file mode 100644 index 00000000..a890aff3 --- /dev/null +++ b/templates/spin-http-tinygo/spin.toml @@ -0,0 +1,13 @@ +apiVersion = "0.1.0" +authors = ["Fermyon Engineering "] +description = "A simple Spin application written in (Tiny)Go." +name = "spin-hello-world" +trigger = { type = "http", base = "/" } +version = "1.0.0" + +[[component]] +id = "hello" +source = "main.wasm" +[component.trigger] +route = "/hello" +executor = { type = "wagi" }