Skip to content

Commit

Permalink
Merge pull request #1128 from fermyon/tpmccallum-patch-24
Browse files Browse the repository at this point in the history
Update Go Components page - to align with 2.1.0
  • Loading branch information
Timothy McCallum authored Jan 8, 2024
2 parents bfa7990 + 35d9935 commit 70c4c38
Showing 1 changed file with 35 additions and 11 deletions.
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.

0 comments on commit 70c4c38

Please sign in to comment.