feat: initial add for (Tiny)Go SDK

Signed-off-by: Radu Matei <radu.matei@fermyon.com>
This commit is contained in:
Radu Matei 2022-03-09 16:52:58 +02:00
parent 8c22c8d0a5
commit d0f0f13a82
No known key found for this signature in database
GPG Key ID: 53A4E7168B7782C2
7 changed files with 66 additions and 0 deletions

1
.gitignore vendored
View File

@ -3,3 +3,4 @@ target
cache
node_modules
ignored-assets
main.wasm

3
sdk/go/go.mod Normal file
View File

@ -0,0 +1,3 @@
module github.com/fermyon/spin-sdk
go 1.17

17
sdk/go/http.go Normal file
View File

@ -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))
}

View File

@ -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

View File

@ -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/
)

View File

@ -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!")
})
}

View File

@ -0,0 +1,13 @@
apiVersion = "0.1.0"
authors = ["Fermyon Engineering <engineering@fermyon.com>"]
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" }