mirror of
https://github.com/syumai/workers.git
synced 2025-03-10 17:29:11 +00:00
F: add fetch example (wip)
This commit is contained in:
parent
eb6c550c8b
commit
778456807e
1
examples/fetch/.gitignore
vendored
Normal file
1
examples/fetch/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
dist
|
12
examples/fetch/Makefile
Normal file
12
examples/fetch/Makefile
Normal 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
|
21
examples/fetch/README.md
Normal file
21
examples/fetch/README.md
Normal file
@ -0,0 +1,21 @@
|
||||
# hello
|
||||
|
||||
* This app just returns a message `Hello, world!`.
|
||||
* If a url param like `?name=syumai` given, then a message `Hello, syumai!` will be returned.
|
||||
|
||||
## 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
|
||||
```
|
7
examples/fetch/go.mod
Normal file
7
examples/fetch/go.mod
Normal file
@ -0,0 +1,7 @@
|
||||
module github.com/syumai/fetch
|
||||
|
||||
go 1.18
|
||||
|
||||
require github.com/syumai/workers v0.0.0
|
||||
|
||||
replace github.com/syumai/workers => ../../
|
2
examples/fetch/go.sum
Normal file
2
examples/fetch/go.sum
Normal 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=
|
35
examples/fetch/main.go
Normal file
35
examples/fetch/main.go
Normal file
@ -0,0 +1,35 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"github.com/syumai/workers"
|
||||
"github.com/syumai/workers/cloudflare/fetch"
|
||||
)
|
||||
|
||||
func main() {
|
||||
handler := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
||||
f := fetch.NewClient()
|
||||
|
||||
r, err := fetch.NewRequest(req.Context(), http.MethodGet, "https://api.github.com/repos/syumai/workers/releases", nil)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
r.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:109.0) Gecko/20100101 Firefox/111.0")
|
||||
|
||||
res, err := f.Do(r)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("content-type", "application/json")
|
||||
io.Copy(w, res.Body)
|
||||
})
|
||||
workers.Serve(handler)
|
||||
}
|
22
examples/fetch/worker.mjs
Normal file
22
examples/fetch/worker.mjs
Normal file
@ -0,0 +1,22 @@
|
||||
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 default {
|
||||
async fetch(req, env, ctx) {
|
||||
await load;
|
||||
await readyPromise;
|
||||
return handleRequest(req, { env, ctx });
|
||||
}
|
||||
}
|
6
examples/fetch/wrangler.toml
Normal file
6
examples/fetch/wrangler.toml
Normal file
@ -0,0 +1,6 @@
|
||||
name = "fetch"
|
||||
main = "./worker.mjs"
|
||||
compatibility_date = "2023-02-24"
|
||||
|
||||
[build]
|
||||
command = "make build"
|
Loading…
x
Reference in New Issue
Block a user