Skip to content

Commit

Permalink
add outbound http to same app tinygo example
Browse files Browse the repository at this point in the history
Signed-off-by: Michelle Dhanani <michelle@fermyon.com>
  • Loading branch information
michelleN committed Sep 1, 2023
1 parent de7aea4 commit 8ba038b
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module outbound-http-to-same-app

go 1.17

require github.com/fermyon/spin/sdk/go v0.0.0

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

replace github.com/fermyon/spin/sdk/go v0.0.0 => ../../../sdk/go/
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package main

import (
"fmt"
"net/http"

spinhttp "github.com/fermyon/spin/sdk/go/http"
)

func init() {
spinhttp.Handle(func(w http.ResponseWriter, r *http.Request) {
// Because we included self in `allowed_http_hosts`, we can make outbound
// HTTP requests to our own app using a relative path.
// This is not yet supported in cloud.
resp, err := spinhttp.Get("/hello")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

fmt.Fprintln(w, resp.Body)
fmt.Fprintln(w, resp.Header.Get("content-type"))
})
}

func main() {}
20 changes: 18 additions & 2 deletions examples/http-tinygo-outbound-http/spin.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,25 @@ version = "1.0.0"

[[component]]
id = "tinygo-hello"
source = "main.wasm"
allowed_http_hosts = ["https://random-data-api.fermyon.app", "https://postman-echo.com"]
source = "tinygo-hello/main.wasm"
allowed_http_hosts = [
"https://random-data-api.fermyon.app",
"https://postman-echo.com",
]
[component.trigger]
route = "/hello"
[component.build]
workdir = "tinygo-hello"
command = "tinygo build -target=wasi -gc=leaking -no-debug -o main.wasm main.go"

[[component]]
id = "outbound-http-to-same-app"
source = "outbound-http-to-same-app/main.wasm"
# Use self to make outbound requests to components in the same Spin application.
# `self` is not yet supported in cloud
allowed_http_hosts = ["self"]
[component.trigger]
route = "/outbound-http-to-same-app"
[component.build]
workdir = "outbound-http-to-same-app"
command = "tinygo build -target=wasi -gc=leaking -no-debug -o main.wasm main.go"
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ require github.com/fermyon/spin/sdk/go v0.0.0

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

replace github.com/fermyon/spin/sdk/go v0.0.0 => ../../sdk/go/
replace github.com/fermyon/spin/sdk/go v0.0.0 => ../../../sdk/go/
2 changes: 2 additions & 0 deletions examples/http-tinygo-outbound-http/tinygo-hello/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U=
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
File renamed without changes.

0 comments on commit 8ba038b

Please sign in to comment.