mirror of
https://github.com/syumai/workers.git
synced 2025-03-11 01:39:11 +00:00
add(examples): pages functions example
This commit is contained in:
parent
7afd8d17d1
commit
16692e1f71
1
examples/pages-functions/.gitignore
vendored
Normal file
1
examples/pages-functions/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
dist
|
12
examples/pages-functions/Makefile
Normal file
12
examples/pages-functions/Makefile
Normal 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
|
20
examples/pages-functions/README.md
Normal file
20
examples/pages-functions/README.md
Normal 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
|
||||||
|
```
|
24
examples/pages-functions/functions/hello.js
Normal file
24
examples/pages-functions/functions/hello.js
Normal 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 });
|
||||||
|
}
|
7
examples/pages-functions/go.mod
Normal file
7
examples/pages-functions/go.mod
Normal 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 => ../../
|
0
examples/pages-functions/go.sum
Normal file
0
examples/pages-functions/go.sum
Normal file
19
examples/pages-functions/main.go
Normal file
19
examples/pages-functions/main.go
Normal 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)
|
||||||
|
}
|
14
examples/pages-functions/pages/index.html
Normal file
14
examples/pages-functions/pages/index.html
Normal 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>
|
4
examples/pages-functions/wrangler.toml
Normal file
4
examples/pages-functions/wrangler.toml
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
name = "pages-functions"
|
||||||
|
|
||||||
|
[build]
|
||||||
|
command = "make build"
|
Loading…
x
Reference in New Issue
Block a user