spin/examples/http-tinygo
Adam Reese 7082744313
ref(sdk/go): use `spin build` for building test fixtures
This commit changes test fixtures to be built using `spin build` rather
than using TinyGo directly.

Signed-off-by: Adam Reese <adam@reese.io>
2023-05-11 08:44:43 -07:00
..
README.md fix: Go SDK integration tests 2023-04-05 15:08:05 +03:00
go.mod fix: Go SDK integration tests 2023-04-05 15:08:05 +03:00
go.sum ref(sdk/go): use `spin build` for building test fixtures 2023-05-11 08:44:43 -07:00
main.go feat(sdk/go): Implement spin-http for TinyGo 2022-04-21 20:47:36 -07:00
spin.toml chore: update examples and templates to spin_manifest_version 2023-03-08 12:06:03 -05:00

README.md

Spin HTTP components in TinyGo

This example showcases how to build Spin HTTP components using TinyGo.

package main

import (
	"fmt"
	"net/http"

	spinhttp "github.com/fermyon/spin/sdk/go/http"
)

func init() {
	spinhttp.Handle(func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Content-Type", "text/plain")
		fmt.Fprintln(w, "Hello Fermyon!")
	})
}

func main() {}

For more information and examples for using TinyGo with WebAssembly, check the official TinyGo documentation and the Wasm examples.

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

$ go mod tidy
$ spin build
Executing the build command for component tinygo-hello: tinygo build -target=wasi -gc=leaking -no-debug -o main.wasm main.go
Successfully ran the build command for the Spin components.

Finally, we can create a Spin application configuration to execute this component:

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"
[component.build]
command = "tinygo build -target=wasi -gc=leaking -no-debug -o main.wasm main.go"

At this point, we can execute the application with the spin CLI:

$ RUST_LOG=spin=trace spin up

The application can now receive requests on http://localhost:3000/hello:

$ curl -i localhost:3000/hello
HTTP/1.1 200 OK
content-type: text/plain; charset=utf-8
content-length: 16
date: Wed, 09 Mar 2022 17:23:08 GMT

Hello, Fermyon!