add hello example

This commit is contained in:
syumai 2022-05-29 22:27:46 +09:00
parent 1cf0183b17
commit 5c55a2ebf7
8 changed files with 79 additions and 0 deletions

1
examples/hello/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
dist

12
examples/hello/Makefile Normal file
View File

@ -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

9
examples/hello/README.md Normal file
View File

@ -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

7
examples/hello/go.mod Normal file
View File

@ -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 => ../../

2
examples/hello/go.sum Normal file
View File

@ -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=

19
examples/hello/main.go Normal 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 = "world"
}
fmt.Fprintf(w, "Hello, %s!", name)
})
workers.Serve(handler)
}

20
examples/hello/worker.mjs Normal file
View File

@ -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));
})

View File

@ -0,0 +1,9 @@
name = "hello"
main = "./worker.mjs"
compatibility_date = "2022-05-13"
compatibility_flags = [
"streams_enable_constructors"
]
[build]
command = "make build"