Skip to content

Commit 6cb693a

Browse files
committed
add outbound http to same app tinygo example
Signed-off-by: Michelle Dhanani <michelle@fermyon.com>
1 parent ff2bfd4 commit 6cb693a

File tree

7 files changed

+56
-3
lines changed

7 files changed

+56
-3
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module outbound-http-to-same-app
2+
3+
go 1.17
4+
5+
require github.com/fermyon/spin/sdk/go v0.0.0
6+
7+
require github.com/julienschmidt/httprouter v1.3.0 // indirect
8+
9+
replace github.com/fermyon/spin/sdk/go v0.0.0 => ../../../sdk/go/
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
7+
spinhttp "github.com/fermyon/spin/sdk/go/http"
8+
)
9+
10+
func init() {
11+
spinhttp.Handle(func(w http.ResponseWriter, r *http.Request) {
12+
// Because we included self in `allowed_http_hosts`, we can make outbound
13+
// HTTP requests to our own app using a relative path.
14+
// This is not yet supported in cloud.
15+
resp, err := spinhttp.Get("/hello")
16+
if err != nil {
17+
http.Error(w, err.Error(), http.StatusInternalServerError)
18+
return
19+
}
20+
21+
fmt.Fprintln(w, resp.Body)
22+
fmt.Fprintln(w, resp.Header.Get("content-type"))
23+
})
24+
}
25+
26+
func main() {}

examples/http-tinygo-outbound-http/spin.toml

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,25 @@ version = "1.0.0"
77

88
[[component]]
99
id = "tinygo-hello"
10-
source = "main.wasm"
11-
allowed_http_hosts = ["https://random-data-api.fermyon.app", "https://postman-echo.com"]
10+
source = "tinygo-hello/main.wasm"
11+
allowed_http_hosts = [
12+
"https://random-data-api.fermyon.app",
13+
"https://postman-echo.com",
14+
]
1215
[component.trigger]
1316
route = "/hello"
1417
[component.build]
18+
workdir = "tinygo-hello"
19+
command = "tinygo build -target=wasi -gc=leaking -no-debug -o main.wasm main.go"
20+
21+
[[component]]
22+
id = "outbound-http-to-same-app"
23+
source = "outbound-http-to-same-app/main.wasm"
24+
# Use self to make outbound requests to components in the same Spin application.
25+
# `self` is not yet supported in cloud
26+
allowed_http_hosts = ["self"]
27+
[component.trigger]
28+
route = "/outbound-http-to-same-app"
29+
[component.build]
30+
workdir = "outbound-http-to-same-app"
1531
command = "tinygo build -target=wasi -gc=leaking -no-debug -o main.wasm main.go"

examples/http-tinygo-outbound-http/go.mod renamed to examples/http-tinygo-outbound-http/tinygo-hello/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ require github.com/fermyon/spin/sdk/go v0.0.0
66

77
require github.com/julienschmidt/httprouter v1.3.0 // indirect
88

9-
replace github.com/fermyon/spin/sdk/go v0.0.0 => ../../sdk/go/
9+
replace github.com/fermyon/spin/sdk/go v0.0.0 => ../../../sdk/go/
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U=
2+
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=

0 commit comments

Comments
 (0)