Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Go Components page - to align with 2.1.0 #1128

Merged
merged 4 commits into from
Jan 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 35 additions & 11 deletions content/spin/v2/go-components.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ Using TinyGo to compile components for Spin is currently required, as the

## Versions

TinyGo `0.29.0` is recommended, which requires Go `v1.18+` or newer.

> TinyGo version `0.29.0` is known to have issues with Spin on some systems.
TinyGo `0.30.0` is recommended, which requires Go `v1.19+`.

## HTTP Components

Expand Down Expand Up @@ -67,6 +65,35 @@ func init() {
func main() {}
```

The Spin HTTP component (written in Go) can be built using the `tingygo` toolchain:

<!-- @selectiveCpy -->

```bash
$ tinygo build -o main.wasm -target=wasi main.go
```

Once built, we can run our Spin HTTP component using the Spin up command:

<!-- @selectiveCpy -->

```bash
$ spin up
```

The Spin HTTP component can now receive and process incoming requests:

<!-- @selectiveCpy -->

```bash
$ curl -i localhost:3000
HTTP/1.1 200 OK
content-type: text/plain
content-length: 15

Hello Fermyon!
```

The important things to note in the implementation above:

- the entry point to the component is the standard `func init()` for Go programs
Expand Down Expand Up @@ -118,12 +145,12 @@ func init() {
func main() {}
```

The component can be built using the `tingygo` toolchain:
The Outbound HTTP Request example above can be built using the `tingygo` toolchain:

<!-- @selectiveCpy -->

```bash
$ tinygo build -wasm-abi=generic -target=wasi -no-debug -o main.wasm main.go
$ tinygo build -o main.wasm -target=wasi main.go
```

Before we can execute this component, we need to add the
Expand Down Expand Up @@ -159,10 +186,7 @@ now receive requests in route `/hello`:
```bash
$ curl -i localhost:3000/hello
HTTP/1.1 200 OK
content-type: text/plain; charset=utf-8
server: spin/0.1.0
content-length: 85
date = "2023-11-04T00:00:01Z"
content-length: 93

{"timestamp":1684299253331,"fact":"Reindeer grow new antlers every year"}
```
Expand Down Expand Up @@ -231,7 +255,7 @@ component = "echo-message"
[component.echo-message]
source = "main.wasm"
[component.echo-message.build]
command = "tinygo build -wasm-abi=generic -target=wasi -gc=leaking -no-debug -o main.wasm main.go"
command = "tinygo build -target=wasi -gc=leaking -no-debug -o main.wasm main.go"
```

The application will connect to `redis://localhost:6379`, and for every new message
Expand Down Expand Up @@ -364,4 +388,4 @@ For more information about using SQLite from TinyGo, see [SQLite storage](sqlite

## AI Inferencing From Go Components

For more information about using Serverless AI from TinyGo, see the [Serverless AI](serverless-ai-api-guide) API guide.
For more information about using Serverless AI from TinyGo, see the [Serverless AI](serverless-ai-api-guide) API guide.
Loading