Skip to content

Commit b43c767

Browse files
authored
Merge pull request #167 from syumai/add-browser-support
add browser support
2 parents 1b9c7bc + 5368356 commit b43c767

File tree

12 files changed

+1685
-1
lines changed

12 files changed

+1685
-1
lines changed

_examples/browser/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
public/build
2+
node_modules
3+
.wrangler

_examples/browser/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# browser example
2+
3+
* This example demonstrates running a Go HTTP server within the browser.
4+
- It's just a static page.
5+
* It showcases calculating the sum of two numbers, with the result rendered inside an iframe.
6+
7+
## Demo
8+
9+
* https://browser-example.syumai.workers.dev
10+
11+
## Requirements
12+
13+
- Node.js
14+
- Go 1.24.0 or later
15+
16+
## Development
17+
18+
### Commands
19+
20+
```
21+
make dev # run dev server
22+
make build # build Go Wasm binary
23+
make deploy # deploy static assets
24+
```

_examples/browser/go.mod

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module github.com/syumai/workers/_examples/browser
2+
3+
go 1.24.0
4+
5+
require github.com/syumai/workers v0.0.0
6+
7+
replace github.com/syumai/workers => ../../

_examples/browser/go.sum

Whitespace-only changes.

_examples/browser/main.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"net/http"
6+
7+
"github.com/syumai/workers"
8+
)
9+
10+
type AddRequest struct {
11+
A int `json:"a"`
12+
B int `json:"b"`
13+
}
14+
15+
func main() {
16+
http.HandleFunc("POST /add", func(w http.ResponseWriter, req *http.Request) {
17+
var addReq AddRequest
18+
if err := json.NewDecoder(req.Body).Decode(&addReq); err != nil {
19+
http.Error(w, err.Error(), http.StatusBadRequest)
20+
return
21+
}
22+
result := addReq.A + addReq.B
23+
if err := json.NewEncoder(w).Encode(result); err != nil {
24+
http.Error(w, err.Error(), http.StatusInternalServerError)
25+
return
26+
}
27+
})
28+
workers.Serve(nil)
29+
}

0 commit comments

Comments
 (0)