mirror of
https://github.com/syumai/workers.git
synced 2025-03-11 09:49:12 +00:00
41 lines
649 B
Go
41 lines
649 B
Go
package main
|
|
|
|
import (
|
|
"io"
|
|
"net/http"
|
|
|
|
"github.com/syumai/workers"
|
|
"github.com/syumai/workers/cloudflare"
|
|
)
|
|
|
|
func main() {
|
|
workers.Serve(&MyHandler{})
|
|
}
|
|
|
|
type MyHandler struct{}
|
|
|
|
func (_ *MyHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
|
COUNTER, err := cloudflare.NewDurableObjectNamespace("COUNTER")
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
id := COUNTER.IdFromName("A")
|
|
obj, err := COUNTER.Get(id)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
res, err := obj.Fetch(req)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
count, err := io.ReadAll(res.Body)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
w.Write([]byte("Durable object 'A' count: " + string(count)))
|
|
}
|