R: add service bindings example

This commit is contained in:
aki-0421 2023-04-09 09:36:27 +09:00
parent ef831af9d7
commit 05d38172ae
No known key found for this signature in database
GPG Key ID: 64A8CF6D437D166A
8 changed files with 121 additions and 0 deletions

1
examples/service-bindings/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
dist

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

View File

@ -0,0 +1,32 @@
# service-bindings
- Service bindings are an API that facilitate Worker-to-Worker communication via explicit bindings defined in your configuration.
- In this example, invoke [hello](https://github.com/syumai/workers/tree/main/examples/hello) using Service bindings.
## Development
### Requirements
This project requires these tools to be installed globally.
* wrangler
* tinygo
### Deploy Steps
1. Deploy [hello](https://github.com/syumai/workers/tree/main/examples/hello) first.
2. Define service bindings in `wrangler.toml`.
```toml
services = [
{ binding = "hello", service = "hello" }
]
```
3. Deploy this example.
```
make build # build Go Wasm binary
make publish # publish worker
```
## Documents
- https://developers.cloudflare.com/workers/runtime-apis/service-bindings/

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

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=

View File

@ -0,0 +1,36 @@
package main
import (
"fmt"
"io"
"net/http"
"github.com/syumai/workers"
"github.com/syumai/workers/cloudflare"
"github.com/syumai/workers/cloudflare/fetch"
)
func main() {
handler := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
ctx := req.Context()
obj := cloudflare.GetBinding(ctx, "hello")
cli := fetch.NewClient(fetch.WithBinding(obj))
r, err := fetch.NewRequest(ctx, http.MethodGet, req.URL.String(), nil)
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
res, err := cli.Do(r)
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
io.Copy(w, res.Body)
})
workers.Serve(handler)
}

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

View File

@ -0,0 +1,9 @@
name = "service-bindings"
main = "./worker.mjs"
compatibility_date = "2023-02-24"
services = [
{ binding = "hello", service = "hello" }
]
[build]
command = "make build"