Skip to content

Commit 97e338c

Browse files
authored
Merge pull request #29 from itowlson/spin-3.2-backports
Spin 3.2 backports
2 parents e648d27 + 53ea70d commit 97e338c

12 files changed

+33
-41
lines changed

content/v3/ai-sentiment-analysis-api-tutorial.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -736,9 +736,6 @@ func performSentimentAnalysis(w http.ResponseWriter, r *http.Request, ps spinhtt
736736
return
737737
}
738738
}
739-
740-
func main() {}
741-
742739
```
743740

744741
{{ blockEnd }}

content/v3/build.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ The build command calls TinyGo with the WASI backend and appropriate options:
135135

136136
```toml
137137
[component.hello.build]
138-
command = "tinygo build -target=wasi -gc=leaking -no-debug -o main.wasm main.go"
138+
command = "tinygo build -target=wasip1 -gc=leaking -buildmode=c-shared -no-debug -o main.wasm ."
139139
```
140140

141141
{{ blockEnd }}

content/v3/go-components.md

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Using TinyGo to compile components for Spin is currently required, as the
3030

3131
## Versions
3232

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.
3434

3535
## HTTP Components
3636

@@ -61,16 +61,14 @@ func init() {
6161
fmt.Fprintln(w, "Hello Fermyon!")
6262
})
6363
}
64-
65-
func main() {}
6664
```
6765

6866
The Spin HTTP component (written in Go) can be built using the `tingygo` toolchain:
6967

7068
<!-- @selectiveCpy -->
7169

7270
```bash
73-
$ tinygo build -o main.wasm -target=wasi main.go
71+
$ tinygo build -target=wasip1 -gc=leaking -buildmode=c-shared -no-debug -o main.wasm .
7472
```
7573

7674
Once built, we can run our Spin HTTP component using the Spin up command:
@@ -141,16 +139,14 @@ func init() {
141139
}
142140
})
143141
}
144-
145-
func main() {}
146142
```
147143

148144
The Outbound HTTP Request example above can be built using the `tingygo` toolchain:
149145

150146
<!-- @selectiveCpy -->
151147

152148
```bash
153-
$ tinygo build -o main.wasm -target=wasi main.go
149+
$ tinygo build -target=wasip1 -gc=leaking -buildmode=c-shared -no-debug -o main.wasm .
154150
```
155151

156152
Before we can execute this component, we need to add the
@@ -228,9 +224,6 @@ func init() {
228224
return nil
229225
})
230226
}
231-
232-
// main function must be included for the compiler but is not executed.
233-
func main() {}
234227
```
235228

236229
The manifest for a Redis application must contain the address of the Redis instance. This is set at the application level:
@@ -255,7 +248,7 @@ component = "echo-message"
255248
[component.echo-message]
256249
source = "main.wasm"
257250
[component.echo-message.build]
258-
command = "tinygo build -target=wasi -gc=leaking -no-debug -o main.wasm main.go"
251+
command = "tinygo build -target=wasip1 -gc=leaking -buildmode=c-shared -no-debug -o main.wasm ."
259252
```
260253

261254
The application will connect to `redis://localhost:6379`, and for every new message
@@ -351,8 +344,6 @@ func init() {
351344
}
352345
})
353346
}
354-
355-
func main() {}
356347
```
357348

358349
As with all networking APIs, you must grant access to Redis hosts via the `allowed_outbound_hosts` field in the application manifest:

content/v3/http-outbound.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ url = "https://github.com/spinframework/spin-docs/blob/main/content/spin/v3/http
77

88
---
99
- [Using HTTP From Applications](#using-http-from-applications)
10+
- [Restrictions](#restrictions)
1011
- [Granting HTTP Permissions to Components](#granting-http-permissions-to-components)
1112
- [Configuration-Based Permissions](#configuration-based-permissions)
1213
- [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
232233

233234
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`.
234235

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+
>
235238
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.
236239

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`:
238241

239242
```toml
240-
allowed_outbound_hosts = ["http://self", "https://self"]
243+
allowed_outbound_hosts = ["http://self", "https://self.alt"]
241244
```
245+
246+
> It doesn't matter which you use - either 'allow' form enables both relative and `self.alt` URLs.

content/v3/http-trigger.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,8 +300,6 @@ class IncomingHandler(http.IncomingHandler):
300300
301301
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.
302302

303-
> The do-nothing `main` function is required by TinyGo but is not used; the action happens in the `init` function and handler callback.
304-
305303
```go
306304
package main
307305

content/v3/javascript-components.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ url = "https://github.com/spinframework/spin-docs/blob/main/content/spin/v3/java
1111
- [Building and Running the Template](#building-and-running-the-template)
1212
- [HTTP Components](#http-components)
1313
- [Sending Outbound HTTP Requests](#sending-outbound-http-requests)
14+
- [Intra-Application Requests in JavaScript](#intra-application-requests-in-javascript)
1415
- [Storing Data in Redis From JS/TS Components](#storing-data-in-redis-from-jsts-components)
1516
- [Storing Data in the Spin Key-Value Store](#storing-data-in-the-spin-key-value-store)
1617
- [Storing Data in SQLite](#storing-data-in-sqlite)
@@ -155,7 +156,7 @@ addEventListener('fetch', async (event: FetchEvent) => {
155156

156157
## Sending Outbound HTTP Requests
157158

158-
If allowed, Spin components can send outbound HTTP requests.
159+
If allowed, Spin components can send outbound HTTP requests using the `fetch` function.
159160
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)
160161

161162
```javascript
@@ -238,6 +239,19 @@ This can be the basis for building components that communicate with external
238239
databases or storage accounts, or even more specialized components like HTTP
239240
proxies or URL shorteners.
240241

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+
await fetch('/api'); // Avoid!
250+
await fetch('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).
254+
241255
---
242256

243257
## Storing Data in Redis From JS/TS Components

content/v3/key-value-store-tutorial.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -399,9 +399,6 @@ func init() {
399399
}
400400
})
401401
}
402-
403-
func main() {}
404-
405402
```
406403

407404
{{ blockEnd }}

content/v3/quickstart.md

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,7 @@ component = "hello-go"
628628
source = "main.wasm"
629629
allowed_outbound_hosts = []
630630
[component.hello-go.build]
631-
command = "tinygo build -target=wasi -gc=leaking -no-debug -o main.wasm main.go"
631+
command = "tinygo build -target=wasip1 -gc=leaking -buildmode=c-shared -no-debug -o main.wasm ."
632632
```
633633

634634
This represents a simple Spin HTTP application (triggered by an HTTP request). It has:
@@ -639,8 +639,8 @@ This represents a simple Spin HTTP application (triggered by an HTTP request).
639639
[Learn more about the manifest here.](./writing-apps)
640640

641641
Now let's have a look at the code. Below is the complete source
642-
code for a Spin HTTP component written in Go. Notice where the work is done. The
643-
`main` function is empty (and Spin never calls it). Instead, the `init` function
642+
code for a Spin HTTP component written in Go. Notice where the work is done. Because this is a component
643+
rather than an application, there is no `main` function. Instead, the `init` function
644644
sets up a callback, and passes that callback to `spinhttp.Handle` to register it as
645645
the handler for HTTP requests. You can learn more about this structure
646646
in the [Go language guide](go-components).
@@ -661,8 +661,6 @@ func init() {
661661
fmt.Fprintln(w, "Hello Fermyon!")
662662
})
663663
}
664-
665-
func main() {}
666664
```
667665

668666
{{ blockEnd }}
@@ -817,7 +815,7 @@ You can always run this command manually; `spin build` is a shortcut.
817815

818816
```bash
819817
$ spin build
820-
Executing the build command for component hello-go: tinygo build -target=wasi -gc=leaking -no-debug -o main.wasm main.go
818+
Executing the build command for component hello-go: tinygo build -target=wasip1 -gc=leaking -buildmode=c-shared -no-debug -o main.wasm .
821819
go: downloading github.com/fermyon/spin/sdk/go v0.10.0
822820
Finished building all Spin components
823821
```
@@ -826,14 +824,14 @@ If the build fails, check:
826824

827825
* Are you in the `hello_go` directory?
828826
* Did you successfully [install TinyGo](#install-the-tools)?
829-
* Are your versions of Go and TinyGo up to date? The Spin SDK needs TinyGo 0.27 or above.
827+
* Are your versions of Go and TinyGo up to date? The Spin SDK needs TinyGo 0.35 or above and Go 1.22 or above.
830828
* Set Environment Variable `CGO_ENABLED=1`. (Since the Go SDK is built using CGO, it requires the CGO_ENABLED=1 environment variable to be set.)
831829

832830
If you would like to know what build command Spin runs for a component, you can find it in the manifest, in the `component.(id).build` section:
833831

834832
```toml
835833
[component.hello-go.build]
836-
command = "tinygo build -target=wasi -gc=leaking -no-debug -o main.wasm main.go"
834+
command = "tinygo build -target=wasip1 -gc=leaking -buildmode=c-shared -no-debug -o main.wasm ."
837835
```
838836

839837
You can always run this command manually; `spin build` is a shortcut to save you having to remember it.

content/v3/rdbms-storage.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,6 @@ func init() {
201201
json.NewEncoder(w).Encode(pets)
202202
})
203203
}
204-
205-
func main() {}
206204
```
207205

208206
{{ blockEnd }}

content/v3/redis-trigger.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,6 @@ func init() {
137137
return nil
138138
})
139139
}
140-
141-
func main() {}
142140
```
143141

144142
{{ blockEnd }}

content/v3/sqlite-api-guide.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,6 @@ func init() {
233233
json.NewEncoder(w).Encode(todos)
234234
})
235235
}
236-
237-
func main() {}
238236
```
239237

240238
**General Notes**

content/v3/variables.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,6 @@ func init() {
242242
fmt.Fprintln(w, "Used an API")
243243
})
244244
}
245-
246-
func main() {}
247245
```
248246

249247
{{ blockEnd }}

0 commit comments

Comments
 (0)