diff --git a/examples/pages-functions/.gitignore b/examples/pages-functions/.gitignore new file mode 100644 index 0000000..53c37a1 --- /dev/null +++ b/examples/pages-functions/.gitignore @@ -0,0 +1 @@ +dist \ No newline at end of file diff --git a/examples/pages-functions/Makefile b/examples/pages-functions/Makefile new file mode 100644 index 0000000..7eb22fe --- /dev/null +++ b/examples/pages-functions/Makefile @@ -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 diff --git a/examples/pages-functions/README.md b/examples/pages-functions/README.md new file mode 100644 index 0000000..7771724 --- /dev/null +++ b/examples/pages-functions/README.md @@ -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 +``` diff --git a/examples/pages-functions/functions/hello.js b/examples/pages-functions/functions/hello.js new file mode 100644 index 0000000..b115f5d --- /dev/null +++ b/examples/pages-functions/functions/hello.js @@ -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 }); +} \ No newline at end of file diff --git a/examples/pages-functions/go.mod b/examples/pages-functions/go.mod new file mode 100644 index 0000000..451a740 --- /dev/null +++ b/examples/pages-functions/go.mod @@ -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 => ../../ diff --git a/examples/pages-functions/go.sum b/examples/pages-functions/go.sum new file mode 100644 index 0000000..e69de29 diff --git a/examples/pages-functions/main.go b/examples/pages-functions/main.go new file mode 100644 index 0000000..5c53629 --- /dev/null +++ b/examples/pages-functions/main.go @@ -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) +} diff --git a/examples/pages-functions/pages/index.html b/examples/pages-functions/pages/index.html new file mode 100644 index 0000000..64e5d20 --- /dev/null +++ b/examples/pages-functions/pages/index.html @@ -0,0 +1,14 @@ + + +
+ + + ++ /hello +
++ /syumai +
+ + diff --git a/examples/pages-functions/wrangler.toml b/examples/pages-functions/wrangler.toml new file mode 100644 index 0000000..d21b154 --- /dev/null +++ b/examples/pages-functions/wrangler.toml @@ -0,0 +1,4 @@ +name = "pages-functions" + +[build] +command = "make build"