Skip to content

Commit

Permalink
fix examples
Browse files Browse the repository at this point in the history
  • Loading branch information
syumai committed Apr 16, 2024
1 parent 52a78e1 commit ca4bfba
Show file tree
Hide file tree
Showing 12 changed files with 24 additions and 30 deletions.
3 changes: 1 addition & 2 deletions _examples/cache/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ func (rw *responseWriter) ToHTTPResponse() *http.Response {
}

func handler(w http.ResponseWriter, req *http.Request) {
ctx := req.Context()
rw := responseWriter{ResponseWriter: w}
c := cache.New()

Expand All @@ -64,7 +63,7 @@ func handler(w http.ResponseWriter, req *http.Request) {
rw.Write([]byte(text))

// Create cache
cloudflare.WaitUntil(ctx, func() {
cloudflare.WaitUntil(func() {
err := c.Put(req, rw.ToHTTPResponse())
if err != nil {
fmt.Println(err)
Expand Down
2 changes: 1 addition & 1 deletion _examples/d1-blog-server/app/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func NewArticleHandler() http.Handler {
func (h *articleHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
// initialize DB.
// D1 connector requires request's context to initialize DB.
c, err := d1.OpenConnector(req.Context(), "BlogDB")
c, err := d1.OpenConnector("BlogDB")
if err != nil {
h.handleErr(w, http.StatusInternalServerError, fmt.Sprintf("failed to initialize DB: %v", err))
}
Expand Down
5 changes: 2 additions & 3 deletions _examples/durable-object-counter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ func main() {
workers.Serve(&MyHandler{})
}

type MyHandler struct {}
type MyHandler struct{}

func (_ *MyHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
COUNTER, err := cloudflare.NewDurableObjectNamespace(req.Context(), "COUNTER")
COUNTER, err := cloudflare.NewDurableObjectNamespace("COUNTER")
if err != nil {
panic(err)
}
Expand All @@ -38,4 +38,3 @@ func (_ *MyHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {

w.Write([]byte("Durable object 'A' count: " + string(count)))
}

2 changes: 1 addition & 1 deletion _examples/env/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

func main() {
handler := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
fmt.Fprintf(w, "MY_ENV: %s", cloudflare.Getenv(req.Context(), "MY_ENV"))
fmt.Fprintf(w, "MY_ENV: %s", cloudflare.Getenv("MY_ENV"))
})
workers.Serve(handler)
}
2 changes: 1 addition & 1 deletion _examples/fetch-event/go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/syumai/workers/_examples/hello
module github.com/syumai/workers/_examples/fetch-event

go 1.21.3

Expand Down
6 changes: 2 additions & 4 deletions _examples/fetch-event/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,10 @@ import (
)

func handler(w http.ResponseWriter, req *http.Request) {
ctx := req.Context()

cloudflare.PassThroughOnException(ctx)
cloudflare.PassThroughOnException()

// logging after responding
cloudflare.WaitUntil(ctx, func() {
cloudflare.WaitUntil(func() {
for i := 0; i < 5; i++ {
time.Sleep(time.Second)
}
Expand Down
2 changes: 1 addition & 1 deletion _examples/kv-counter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func main() {
}

// initialize KV namespace instance
kv, err := cloudflare.NewKVNamespace(req.Context(), counterNamespace)
kv, err := cloudflare.NewKVNamespace(counterNamespace)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to init KV: %v", err)
os.Exit(1)
Expand Down
6 changes: 3 additions & 3 deletions _examples/mysql-blog-server/app/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ func NewArticleHandler() http.Handler {

func (h *articleHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
// initialize DB.
mysql.RegisterDialContext("tcp", func(_ context.Context, addr string) (net.Conn, error) {
return sockets.Connect(req.Context(), addr, &sockets.SocketOptions{
mysql.RegisterDialContext("tcp", func(ctx context.Context, addr string) (net.Conn, error) {
return sockets.Connect(ctx, addr, &sockets.SocketOptions{
SecureTransport: sockets.SecureTransportOff,
})
})
db, err := sql.Open("mysql",
cloudflare.Getenv(req.Context(), "MYSQL_DSN"))
cloudflare.Getenv("MYSQL_DSN"))
if err != nil {
log.Fatalf("failed to connect: %v", err)
}
Expand Down
19 changes: 9 additions & 10 deletions _examples/r2-image-server/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"context"
"fmt"
"io"
"log"
Expand All @@ -24,12 +23,12 @@ func handleErr(w http.ResponseWriter, msg string, err error) {

type server struct{}

func (s *server) bucket(ctx context.Context) (*cloudflare.R2Bucket, error) {
return cloudflare.NewR2Bucket(ctx, bucketName)
func (s *server) bucket() (*cloudflare.R2Bucket, error) {
return cloudflare.NewR2Bucket(bucketName)
}

func (s *server) post(w http.ResponseWriter, req *http.Request, key string) {
bucket, err := s.bucket(req.Context())
bucket, err := s.bucket()
if err != nil {
handleErr(w, "failed to initialize R2Bucket\n", err)
return
Expand Down Expand Up @@ -61,9 +60,9 @@ func (s *server) post(w http.ResponseWriter, req *http.Request, key string) {
w.Write([]byte("successfully uploaded image"))
}

func (s *server) get(w http.ResponseWriter, req *http.Request, key string) {
func (s *server) get(w http.ResponseWriter, key string) {
// get image object from R2
bucket, err := s.bucket(req.Context())
bucket, err := s.bucket()
if err != nil {
handleErr(w, "failed to initialize R2Bucket\n", err)
return
Expand All @@ -88,9 +87,9 @@ func (s *server) get(w http.ResponseWriter, req *http.Request, key string) {
io.Copy(w, imgObj.Body)
}

func (s *server) delete(w http.ResponseWriter, req *http.Request, key string) {
func (s *server) delete(w http.ResponseWriter, key string) {
// delete image object from R2
bucket, err := s.bucket(req.Context())
bucket, err := s.bucket()
if err != nil {
handleErr(w, "failed to initialize R2Bucket\n", err)
return
Expand All @@ -107,10 +106,10 @@ func (s *server) ServeHTTP(w http.ResponseWriter, req *http.Request) {
key := strings.TrimPrefix(req.URL.Path, "/")
switch req.Method {
case "GET":
s.get(w, req, key)
s.get(w, key)
return
case "DELETE":
s.delete(w, req, key)
s.delete(w, key)
return
case "POST":
s.post(w, req, key)
Expand Down
2 changes: 1 addition & 1 deletion _examples/r2-image-viewer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func handleErr(w http.ResponseWriter, msg string, err error) {
// This example is based on implementation in syumai/workers-playground
// - https://github.com/syumai/workers-playground/blob/e32881648ccc055e3690a0d9c750a834261c333e/r2-image-viewer/src/index.ts#L30
func handler(w http.ResponseWriter, req *http.Request) {
bucket, err := cloudflare.NewR2Bucket(req.Context(), bucketName)
bucket, err := cloudflare.NewR2Bucket(bucketName)
if err != nil {
handleErr(w, "failed to get R2Bucket\n", err)
return
Expand Down
2 changes: 1 addition & 1 deletion _examples/service-bindings/go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/syumai/workers/_examples/fetch
module github.com/syumai/workers/_examples/service-bindings

go 1.21.3

Expand Down
3 changes: 1 addition & 2 deletions _examples/service-bindings/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import (

func main() {
handler := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
ctx := req.Context()
bind := cloudflare.GetBinding(ctx, "hello")
bind := cloudflare.GetBinding("hello")
fc := fetch.NewClient(fetch.WithBinding(bind))

hc := fc.HTTPClient(fetch.RedirectModeFollow)
Expand Down

0 comments on commit ca4bfba

Please sign in to comment.