mirror of
https://github.com/syumai/workers.git
synced 2025-03-10 17:29:11 +00:00
F: add cache API example
This commit is contained in:
parent
3fed99fa50
commit
46aea871f2
1
examples/cache/.gitignore
vendored
Normal file
1
examples/cache/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
dist
|
13
examples/cache/Makefile
vendored
Normal file
13
examples/cache/Makefile
vendored
Normal file
@ -0,0 +1,13 @@
|
||||
.PHONY: dev
|
||||
dev:
|
||||
wrangler dev
|
||||
|
||||
.PHONY: build
|
||||
build:
|
||||
mkdir -p dist
|
||||
#tinygo build -o ./dist/app.wasm -target wasm ./...
|
||||
tinygo build -o ./dist/app.wasm -target wasm -no-debug ./...
|
||||
|
||||
.PHONY: publish
|
||||
publish:
|
||||
wrangler publish
|
20
examples/cache/README.md
vendored
Normal file
20
examples/cache/README.md
vendored
Normal file
@ -0,0 +1,20 @@
|
||||
# [Cache](https://developers.cloudflare.com/workers/runtime-apis/cache/)
|
||||
|
||||
The Cache API allows fine grained control of reading and writing from the Cloudflare global network.
|
||||
|
||||
### 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
|
||||
```
|
9
examples/cache/go.mod
vendored
Normal file
9
examples/cache/go.mod
vendored
Normal file
@ -0,0 +1,9 @@
|
||||
module github.com/syumai/cache
|
||||
|
||||
go 1.18
|
||||
|
||||
require github.com/syumai/workers v0.0.0
|
||||
|
||||
require github.com/go-chi/chi/v5 v5.0.8 // indirect
|
||||
|
||||
replace github.com/syumai/workers => ../../
|
4
examples/cache/go.sum
vendored
Normal file
4
examples/cache/go.sum
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
github.com/go-chi/chi/v5 v5.0.8 h1:lD+NLqFcAi1ovnVZpsnObHGW4xb4J8lNmoYVfECH1Y0=
|
||||
github.com/go-chi/chi/v5 v5.0.8/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
|
||||
github.com/syumai/workers v0.1.0 h1:z5QfQR2X+PCKzom7RodpI5J4D5YF7NT7Qwzb9AM9dgY=
|
||||
github.com/syumai/workers v0.1.0/go.mod h1:alXIDhTyeTwSzh0ZgQ3cb9HQPyyYfIejupE4Z3efr14=
|
77
examples/cache/main.go
vendored
Normal file
77
examples/cache/main.go
vendored
Normal file
@ -0,0 +1,77 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/syumai/workers"
|
||||
"github.com/syumai/workers/cloudflare"
|
||||
"github.com/syumai/workers/cloudflare/cache"
|
||||
)
|
||||
|
||||
type responseWriter struct {
|
||||
http.ResponseWriter
|
||||
StatusCode int
|
||||
Body []byte
|
||||
}
|
||||
|
||||
func (rw *responseWriter) WriteHeader(statusCode int) {
|
||||
rw.StatusCode = statusCode
|
||||
rw.ResponseWriter.WriteHeader(statusCode)
|
||||
}
|
||||
|
||||
func (rw *responseWriter) Write(data []byte) (int, error) {
|
||||
rw.Body = append(rw.Body, data...)
|
||||
return rw.ResponseWriter.Write(data)
|
||||
}
|
||||
|
||||
func (rw *responseWriter) ToHTTPResponse() *http.Response {
|
||||
return &http.Response{
|
||||
StatusCode: rw.StatusCode,
|
||||
Header: rw.Header(),
|
||||
Body: io.NopCloser(bytes.NewReader(rw.Body)),
|
||||
}
|
||||
}
|
||||
|
||||
func handler(w http.ResponseWriter, req *http.Request) {
|
||||
ctx := req.Context()
|
||||
rw := responseWriter{ResponseWriter: w}
|
||||
c := cache.New()
|
||||
|
||||
// Find cache
|
||||
res, _ := c.Match(req, nil)
|
||||
if res != nil {
|
||||
// Set the response status code
|
||||
rw.WriteHeader(res.StatusCode)
|
||||
// Set the response headers
|
||||
for key, values := range res.Header {
|
||||
for _, value := range values {
|
||||
rw.Header().Add(key, value)
|
||||
}
|
||||
}
|
||||
rw.Header().Add("X-Message", "cache from worker")
|
||||
// Set the response body
|
||||
io.Copy(rw.ResponseWriter, res.Body)
|
||||
return
|
||||
}
|
||||
|
||||
// Responding
|
||||
text := fmt.Sprintf("time:%v\n", time.Now().UnixMilli())
|
||||
rw.Header().Set("Cache-Control", "max-age=15")
|
||||
rw.Write([]byte(text))
|
||||
|
||||
// Create cache
|
||||
cloudflare.WaitUntil(ctx, func() {
|
||||
err := c.Put(req, rw.ToHTTPResponse())
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func main() {
|
||||
workers.Serve(http.HandlerFunc(handler))
|
||||
}
|
22
examples/cache/worker.mjs
vendored
Normal file
22
examples/cache/worker.mjs
vendored
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/cache/wrangler.toml
vendored
Normal file
6
examples/cache/wrangler.toml
vendored
Normal file
@ -0,0 +1,6 @@
|
||||
name = "cache"
|
||||
main = "./worker.mjs"
|
||||
compatibility_date = "2023-02-24"
|
||||
|
||||
[build]
|
||||
command = "make build"
|
Loading…
x
Reference in New Issue
Block a user