Skip to content

Commit 37e31d0

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

File tree

7 files changed

+53
-3
lines changed

7 files changed

+53
-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: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
resp, err := spinhttp.Get("/hello")
15+
if err != nil {
16+
http.Error(w, err.Error(), http.StatusInternalServerError)
17+
return
18+
}
19+
20+
fmt.Fprintln(w, resp.Body)
21+
fmt.Fprintln(w, resp.Header.Get("content-type"))
22+
})
23+
}
24+
25+
func main() {}

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,23 @@ 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+
allowed_http_hosts = ["self"]
25+
[component.trigger]
26+
route = "/outbound-http-to-same-app"
27+
[component.build]
28+
workdir = "outbound-http-to-same-app"
1529
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)