You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/v3/go-components.md
+4-13Lines changed: 4 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -30,7 +30,7 @@ Using TinyGo to compile components for Spin is currently required, as the
30
30
31
31
## Versions
32
32
33
-
TinyGo `0.30.0` is recommended, which requires Go `v1.19+`.
33
+
TinyGo `0.35.0` is recommended, which requires Go `v1.22+`. Older versions of TinyGo may not support the command-line flags used when building Spin applications.
34
34
35
35
## HTTP Components
36
36
@@ -61,16 +61,14 @@ func init() {
61
61
fmt.Fprintln(w, "Hello Fermyon!")
62
62
})
63
63
}
64
-
65
-
funcmain() {}
66
64
```
67
65
68
66
The Spin HTTP component (written in Go) can be built using the `tingygo` toolchain:
-[Making HTTP Requests Within an Application](#making-http-requests-within-an-application)
@@ -232,10 +233,14 @@ However, the wildcard implies that the component requires _all other_ components
232
233
233
234
To make an HTTP request to another route with your application, you can pass just the route as the URL. For example, if you make an outbound HTTP request to `/api/customers/`, Spin prepends the route with whatever host the application is running on. It also replaces the URL scheme (`http` or `https`) with the scheme of the current HTTP request. For example, if the application is running in the cloud, Spin changes `/api` to `https://.../api`.
234
235
236
+
> You can also use the special host `self.alt` to perform self-requests by route. This is important for the JavaScript `fetch` wrapper, which handles relative requests in a way that doesn't work with `allowed_outbound_hosts`. For example, you would write `fetch('http://self.alt/api')`.
237
+
>
235
238
In this way of doing self-requests, the request undergoes normal HTTP processing once Spin has prepended the host. For example, in a cloud deployment, the request passes through the network, and potentially back in through a load balancer or other gateway. The benefit of this is that it allows load to be distributed across the environment, but it may count against your use of bandwidth.
236
239
237
-
You must still grant permission by including `self` in `allowed_outbound_hosts`:
240
+
You must still grant permission by including `self`or `self.alt`in `allowed_outbound_hosts`:
Copy file name to clipboardExpand all lines: content/v3/http-trigger.md
-2Lines changed: 0 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -300,8 +300,6 @@ class IncomingHandler(http.IncomingHandler):
300
300
301
301
In Go, you register the handler as a callback in your program's `init` function. Call `spinhttp.Handle`, passing your handler as the sole argument. Your handler takes a `http.Request` record, from the standard `net/http` package, and a `ResponseWriter` to construct the response.
302
302
303
-
> The do-nothing `main` function is required by TinyGo but is not used; the action happens in the `init` function and handler callback.
If allowed, Spin components can send outbound HTTP requests.
159
+
If allowed, Spin components can send outbound HTTP requests using the `fetch` function.
159
160
Let's see an example of a component that makes a request to [an API that returns random animal facts](https://random-data-api.fermyon.app/animals/json)
160
161
161
162
```javascript
@@ -238,6 +239,19 @@ This can be the basis for building components that communicate with external
238
239
databases or storage accounts, or even more specialized components like HTTP
239
240
proxies or URL shorteners.
240
241
242
+
### Intra-Application Requests in JavaScript
243
+
244
+
JavaScript's `fetch` function handles relative URLs in a way that doesn't work well with Spin's fine-grained outbound HTTP permissions.
245
+
Therefore, when [making a request to another route within the same application](./http-outbound#intra-application-http-requests-by-route),
246
+
you must use the special pseudo-host `self.alt` rather than a relative route. For example:
247
+
248
+
```javascript
249
+
awaitfetch('/api'); // Avoid!
250
+
awaitfetch('http://self.alt/api'); // Prefer!
251
+
```
252
+
253
+
You must [add `http://self` or `http://self.alt` to the component's `allowed_outbound_hosts`](./http-outbound#intra-application-http-requests-by-route).
0 commit comments