add(examples): pages functions example

This commit is contained in:
zztkm 2023-04-20 23:52:16 +09:00
parent 7afd8d17d1
commit 16692e1f71
9 changed files with 101 additions and 0 deletions

1
examples/pages-functions/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
dist

View File

@ -0,0 +1,12 @@
.PHONY: dev
dev:
wrangler pages dev ./pages
.PHONY: build
build:
mkdir -p dist
tinygo build -o ./dist/app.wasm -target wasm ./...
.PHONY: publish
publish:
wrangler pages publish ./pages

View File

@ -0,0 +1,20 @@
# pages-functions
* This app is the Pages Functions version of hello.
## Development
### Requirements
This project requires these tools to be installed globally.
* wrangler
* tinygo
### Commands
```
make dev # run dev server
make build # build Go Wasm binary
make publish # publish worker
```

View File

@ -0,0 +1,24 @@
import "../../assets/polyfill_performance.js";
import "../../assets/wasm_exec.js";
import mod from "../dist/app.wasm";
const go = new Go();
const readyPromise = new Promise((resolve) => {
globalThis.ready = resolve;
});
const load = WebAssembly.instantiate(mod, go.importObject).then((instance) => {
go.run(instance);
return instance;
});
export const onRequest = async (ctx) => {
await load;
await readyPromise;
const {
request,
env,
} = ctx;
return handleRequest(request, { env, ctx });
}

View File

@ -0,0 +1,7 @@
module github.com/syumai/pages-functions
go 1.18
require github.com/syumai/workers v0.0.0
replace github.com/syumai/workers => ../../

View File

View File

@ -0,0 +1,19 @@
package main
import (
"fmt"
"net/http"
"github.com/syumai/workers"
)
func main() {
handler := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
name := req.URL.Query().Get("name")
if name == "" {
name = "Pages Functions"
}
fmt.Fprintf(w, "Hello, %s!", name)
})
workers.Serve(handler)
}

View File

@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
</head>
<body>
<p>
<a href="/hello">/hello</a>
</p>
<p>
<a href="/hello?name=syumai">/syumai</a>
</p>
</body>
</html>

View File

@ -0,0 +1,4 @@
name = "pages-functions"
[build]
command = "make build"