Skip to content

Commit

Permalink
Merge pull request fermyon#921 from etehtsea/upd-examples
Browse files Browse the repository at this point in the history
chore: Update examples
  • Loading branch information
itowlson committed Dec 7, 2022
2 parents 4f22512 + 0f8c025 commit a9a9ada
Show file tree
Hide file tree
Showing 12 changed files with 99 additions and 144 deletions.
7 changes: 0 additions & 7 deletions examples/config-tinygo/Makefile

This file was deleted.

16 changes: 16 additions & 0 deletions examples/config-tinygo/README.md
@@ -0,0 +1,16 @@
# Spin component in TinyGo

```shell
$ RUST_LOG=spin=trace spin build --up
```

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

```shell
$ curl -i localhost:3000
HTTP/1.1 200 OK
content-length: 23
date: Tue, 29 Nov 2022 06:59:24 GMT

message: I'm a teapot
```
4 changes: 3 additions & 1 deletion examples/config-tinygo/spin.toml
Expand Up @@ -14,4 +14,6 @@ source = "main.wasm"
[component.trigger]
route = "/..."
[component.config]
message = "I'm a {{object}}"
message = "I'm a {{object}}"
[component.build]
command = "tinygo build -wasm-abi=generic -target=wasi -gc=leaking -no-debug -o main.wasm main.go"
Expand Up @@ -16,16 +16,17 @@ Creating and sending HTTP requests from Spin components closely follows the Go
r1, err := spin_http.Get("https://some-random-api.ml/facts/dog")
r2, err := spin_http.Post("https://postman-echo.com/post", "text/plain", bytes.NewBufferString("Hello there!"))

req, err := http.NewRequest("PUT", "https://postman-echo.com/put", bytes NewBufferString("General Kenobi!"))
req.Header.Add("foo", "bar")
r3, err := spin_http.Send(req)
req, err := http.NewRequest("PUT", "https://postman-echo.com/put", bytes NewBufferString("General Kenobi!"))
req.Header.Add("foo", "bar")
r3, err := spin_http.Send(req)
```

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

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

The component configuration must contain a list of all hosts allowed to send
Expand All @@ -42,13 +43,14 @@ source = "main.wasm"
allowed_http_hosts = [ "https://some-random-api.ml", "https://postman-echo.com" ]
[component.trigger]
route = "/hello"
[component.build]
command = "tinygo build -wasm-abi=generic -target=wasi -gc=leaking -no-debug -o main.wasm main.go"
```

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

```shell
$ make serve
RUST_LOG=spin=trace,wasi_outbound_http=trace spin up --file spin.toml
$ RUST_LOG=spin=trace,wasi_outbound_http=trace spin up
```

The application can now receive requests on `http://localhost:3000/hello`:
Expand Down
2 changes: 1 addition & 1 deletion examples/http-tinygo-outbound-http/spin.toml
@@ -1,7 +1,7 @@
spin_version = "1"
authors = ["Fermyon Engineering <engineering@fermyon.com>"]
description = "A simple Spin application written in (Tiny)Go that performs outbound HTTP requests."
name = "spin-hello-tinygo"
name = "spin-tinygo-outbound-http"
trigger = { type = "http", base = "/" }
version = "1.0.0"

Expand Down
@@ -1,4 +1,4 @@
# Spin HTTP components in (Tiny)Go
# Spin HTTP components in TinyGo

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

Expand Down Expand Up @@ -30,8 +30,9 @@ func main() {}
Building this as a WebAssembly module can be done using the `tinygo` compiler:

```shell
$ make build
tinygo build -wasm-abi=generic -target=wasi -no-debug -o main.wasm main.go
$ spin build
Executing the build command for component tinygo-hello: tinygo build -wasm-abi=generic -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
Expand All @@ -50,13 +51,14 @@ id = "hello"
source = "main.wasm"
[component.trigger]
route = "/hello"
[component.build]
command = "tinygo build -wasm-abi=generic -target=wasi -gc=leaking -no-debug -o main.wasm main.go"
```

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

```shell
$ make serve
RUST_LOG=spin=trace spin up --file spin.toml
$ RUST_LOG=spin=trace spin up
```

The application can now receive requests on `http://localhost:3000/hello`:
Expand Down
118 changes: 31 additions & 87 deletions examples/spin-wagi-http/http-rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions examples/tinygo-outbound-redis/README.md
@@ -0,0 +1,18 @@
# Spin component in TinyGo

```shell
$ RUST_LOG=spin=trace spin build --up
```

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

```shell
$ curl -i localhost:3000/publish
HTTP/1.1 200 OK
content-length: 67
date: Tue, 29 Nov 2022 07:03:52 GMT

mykey value was: myvalue
spin-go-incr value: 1
deleted keys num: 2
```
11 changes: 11 additions & 0 deletions examples/tinygo-redis/README.md
@@ -0,0 +1,11 @@
# Spin component in TinyGo

```shell
$ RUST_LOG=spin=trace spin build --up
```

```shell
$ redis-cli
127.0.0.1:6379> PUBLISH messages test-message
(integer) 1
```
4 changes: 3 additions & 1 deletion examples/wagi-http-rust/spin.toml
Expand Up @@ -10,4 +10,6 @@ id = "env"
source = "target/wasm32-wasi/release/wagihelloworld.wasm"
[component.trigger]
route = "/env"
executor = { type = "wagi" } # _start (the default entrypoint) is automatically mapped to main()
executor = { type = "wagi" } # _start (the default entrypoint) is automatically mapped to main()
[component.build]
command = "cargo build --target wasm32-wasi --release"
15 changes: 0 additions & 15 deletions wit/ephemeral/wasi-cache.wit

This file was deleted.

0 comments on commit a9a9ada

Please sign in to comment.