2023-02-21 18:46:22 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/syumai/workers"
|
|
|
|
"github.com/syumai/workers/cloudflare"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
workers.Serve(&MyHandler{})
|
|
|
|
}
|
|
|
|
|
2024-04-17 00:56:47 +09:00
|
|
|
type MyHandler struct{}
|
2023-02-21 18:46:22 +01:00
|
|
|
|
|
|
|
func (_ *MyHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
2024-04-17 00:56:47 +09:00
|
|
|
COUNTER, err := cloudflare.NewDurableObjectNamespace("COUNTER")
|
2023-02-21 18:46:22 +01:00
|
|
|
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)))
|
|
|
|
}
|