From 5c55a2ebf7f7a4149dcd88474c71cd0a1c1b371a Mon Sep 17 00:00:00 2001 From: syumai Date: Sun, 29 May 2022 22:27:46 +0900 Subject: [PATCH] add hello example --- examples/hello/.gitignore | 1 + examples/hello/Makefile | 12 ++++++++++++ examples/hello/README.md | 9 +++++++++ examples/hello/go.mod | 7 +++++++ examples/hello/go.sum | 2 ++ examples/hello/main.go | 19 +++++++++++++++++++ examples/hello/worker.mjs | 20 ++++++++++++++++++++ examples/hello/wrangler.toml | 9 +++++++++ 8 files changed, 79 insertions(+) create mode 100644 examples/hello/.gitignore create mode 100644 examples/hello/Makefile create mode 100644 examples/hello/README.md create mode 100644 examples/hello/go.mod create mode 100644 examples/hello/go.sum create mode 100644 examples/hello/main.go create mode 100644 examples/hello/worker.mjs create mode 100644 examples/hello/wrangler.toml diff --git a/examples/hello/.gitignore b/examples/hello/.gitignore new file mode 100644 index 0000000..53c37a1 --- /dev/null +++ b/examples/hello/.gitignore @@ -0,0 +1 @@ +dist \ No newline at end of file diff --git a/examples/hello/Makefile b/examples/hello/Makefile new file mode 100644 index 0000000..320ddc3 --- /dev/null +++ b/examples/hello/Makefile @@ -0,0 +1,12 @@ +.PHONY: dev +dev: + wrangler dev + +.PHONY: build +build: + mkdir -p dist + tinygo build -o ./dist/app.wasm -target wasm ./... + +.PHONY: publish +publish: + wrangler publish diff --git a/examples/hello/README.md b/examples/hello/README.md new file mode 100644 index 0000000..5276432 --- /dev/null +++ b/examples/hello/README.md @@ -0,0 +1,9 @@ +# hello + +* This app just returns a message `Hello, world!`. +* If url param like `?name=syumai`, then a message `Hello, syumai!` will be returned. + +## Demo + +* https://hello.syumai.workers.dev/ +* https://hello.syumai.workers.dev/?name=syumai diff --git a/examples/hello/go.mod b/examples/hello/go.mod new file mode 100644 index 0000000..8a8adaa --- /dev/null +++ b/examples/hello/go.mod @@ -0,0 +1,7 @@ +module github.com/syumai/hello + +go 1.18 + +require github.com/syumai/workers v0.0.0 + +replace github.com/syumai/workers => ../../ \ No newline at end of file diff --git a/examples/hello/go.sum b/examples/hello/go.sum new file mode 100644 index 0000000..8c27871 --- /dev/null +++ b/examples/hello/go.sum @@ -0,0 +1,2 @@ +github.com/syumai/workers v0.1.0 h1:z5QfQR2X+PCKzom7RodpI5J4D5YF7NT7Qwzb9AM9dgY= +github.com/syumai/workers v0.1.0/go.mod h1:alXIDhTyeTwSzh0ZgQ3cb9HQPyyYfIejupE4Z3efr14= diff --git a/examples/hello/main.go b/examples/hello/main.go new file mode 100644 index 0000000..9fbdfbd --- /dev/null +++ b/examples/hello/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 = "world" + } + fmt.Fprintf(w, "Hello, %s!", name) + }) + workers.Serve(handler) +} diff --git a/examples/hello/worker.mjs b/examples/hello/worker.mjs new file mode 100644 index 0000000..1724f16 --- /dev/null +++ b/examples/hello/worker.mjs @@ -0,0 +1,20 @@ +import "../assets/polyfill_performance.js"; +import "../assets/wasm_exec.js"; +import mod from "./dist/app.wasm"; + +const go = new Go(); + +const load = WebAssembly.instantiate(mod, go.importObject).then((instance) => { + go.run(instance); + return instance; +}); + +async function processRequest(event) { + const req = event.request; + await load; + return handleRequest(req); +} + +addEventListener("fetch", (event) => { + event.respondWith(processRequest(event)); +}) diff --git a/examples/hello/wrangler.toml b/examples/hello/wrangler.toml new file mode 100644 index 0000000..32eaddf --- /dev/null +++ b/examples/hello/wrangler.toml @@ -0,0 +1,9 @@ +name = "hello" +main = "./worker.mjs" +compatibility_date = "2022-05-13" +compatibility_flags = [ + "streams_enable_constructors" +] + +[build] +command = "make build"