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: README.md
+23-1
Original file line number
Diff line number
Diff line change
@@ -20,6 +20,7 @@
20
20
-[Hello example with state and keepalive](https://nlepage.github.io/go-wasm-http-server/hello-state-keepalive) ([sources](https://github.com/nlepage/go-wasm-http-server/tree/master/docs/hello-state-keepalive))
-[Random password generator web server](https://nlepage.github.io/random-password-please/) ([sources](https://github.com/nlepage/random-password-please) forked from [jbarham/random-password-please](https://github.com/jbarham/random-password-please))
23
+
-[Server fallbacks, and compiling with TinyGo](https://github.com/nlepage/go-wasm-http-server/tree/master/docs/tinygo) (runs locally; see [sources & readme](https://github.com/nlepage/go-wasm-http-server/tree/master/docs/tinygo#README) for how to run this example)
23
24
24
25
25
26
## How?
@@ -39,6 +40,7 @@ The slides are available [here](https://nlepage.github.io/go-wasm-http-talk/).
39
40
`go-wasm-http-server` requires you to build your Go application to WebAssembly, so you need to make sure your code is compatible:
40
41
- no C bindings
41
42
- no System dependencies such as file system or network (database server for example)
43
+
- For smaller WASM blobs, your code may also benefit from being compatible with, and compiled by, [TinyGo](https://tinygo.org/docs/reference/lang-support/stdlib/). See the TinyGo specific details below.
42
44
43
45
## Usage
44
46
@@ -87,16 +89,36 @@ You may want to use build tags as shown above (or file name suffixes) in order t
87
89
Then build your WebAssembly binary:
88
90
89
91
```sh
92
+
# To compile with Go
90
93
GOOS=js GOARCH=wasm go build -o server.wasm .
94
+
95
+
# To compile with TinyGo, if your code is compatible
96
+
GOOS=js GOARCH=wasm tinygo build -o server.wasm .
91
97
```
92
98
93
99
### Step 2: Create ServiceWorker file
94
100
101
+
First, check the version of Go/TinyGo you compiled your wasm with:
102
+
103
+
```sh
104
+
$ go version
105
+
go version go1.23.4 darwin/arm64
106
+
# ^------^
107
+
108
+
$ tinygo version
109
+
tinygo version 0.35.0 darwin/arm64 (using go version go1.23.4 and LLVM version 18.1.2)
110
+
# ^----^
111
+
```
112
+
95
113
Create a ServiceWorker file with the following code:
This example demonstrates that go-wasm-http-server can also be compiled with [TinyGo](https://www.tinygo.org), producing significantly smaller WASM blobs, though at the expense of [at least one known bug](https://github.com/tinygo-org/tinygo/issues/1140) and a [reduced standard library](https://tinygo.org/docs/reference/lang-support/stdlib/).
4
+
5
+
This example also demonstrates how the same code can be used for both server-side execution, and client-side execution in WASM (providing support for clients that cannot interpret WASM).
6
+
7
+
## Prerequisites
8
+
9
+
You'll need a version of [TinyGo installed](https://tinygo.org/getting-started/install/). (eg. `brew install tinygo-org/tools/tinygo`)
10
+
11
+
You'll need to make sure the first line of `sw.js` here has the same tinygo version number as your TinyGo version (this was v0.35.0 at time of writing).
12
+
13
+
## Build & run
14
+
15
+
Compile the WASM blob with TinyGo (this has been done for you for this example):
16
+
17
+
```bash
18
+
GOOS=js GOARCH=wasm tinygo build -o api.wasm .
19
+
```
20
+
21
+
Run the server (with Go, not TinyGo):
22
+
23
+
```bash
24
+
$ go run .
25
+
Server starting on http://127.0.0.1:<port>
26
+
```
27
+
28
+
## Important notes
29
+
30
+
You **must** use the TinyGo `wasm_exec.js`, specific to the version of TinyGo used to compile the WASM, in your `sw.js`. For example, if using the JSDelivr CDN:
<p>This example demonstrates that go-wasm-http-server can be compiled with <ahref="https://www.tinygo.org">TinyGo</em>, producing significantly smaller WASM blobs, at the expense of <ahref="https://github.com/tinygo-org/tinygo/issues/1140">at least one known bug</a>, and a <ahref="https://tinygo.org/docs/reference/lang-support/stdlib/">reduced standard library</a>.</p>
33
+
<dl><dt>WASM HTTP Service Worker:</dt><ddid="wasm-status">☁️ Not loaded — will call server</dd></dl>
// Note that this needs to be mounted at /api/tiny, rather than just /tiny (like in wasm.go)
22
+
// because the service worker mounts the WASM server at /api (at the end of sw.js)
23
+
http.HandleFunc("/api/tiny", goRuntimeHandler)
24
+
25
+
// Pick any available port. Note that ServiceWorkers _require_ localhost for non-SSL serving (so other LAN/WAN IPs will prevent the service worker from loading)
26
+
listener, err:=net.Listen("tcp", ":0")
27
+
iferr!=nil {
28
+
log.Fatalf("Unable to claim a port to start server on: %v", err)
29
+
}
30
+
31
+
// Share the port being used & start
32
+
fmt.Printf("Server starting on http://127.0.0.1:%d\n", listener.Addr().(*net.TCPAddr).Port)
0 commit comments