Skip to content

Commit 15f2b81

Browse files
committed
update queues example
1 parent d0919c3 commit 15f2b81

File tree

2 files changed

+14
-18
lines changed

2 files changed

+14
-18
lines changed

_examples/queues/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
.PHONY: dev
22
dev:
3-
npx wrangler dev --port 8787
3+
wrangler dev
44

55
.PHONY: build
66
build:
@@ -9,4 +9,4 @@ build:
99

1010
.PHONY: deploy
1111
deploy:
12-
npx wrangler deploy
12+
wrangler deploy

_examples/queues/main.go

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ func main() {
2323
http.HandleFunc("/", handleProduce)
2424
workers.Serve(nil)
2525
}
26+
2627
func handleProduce(w http.ResponseWriter, req *http.Request) {
2728
if req.URL.Path != "/" {
2829
w.WriteHeader(http.StatusNotFound)
@@ -48,7 +49,7 @@ func handleProduce(w http.ResponseWriter, req *http.Request) {
4849
err = produceText(q, req)
4950
case "application/json":
5051
log.Println("Handling json content type")
51-
err = produceJson(q, req)
52+
err = produceJSON(q, req)
5253
default:
5354
log.Println("Handling bytes content type")
5455
err = produceBytes(q, req)
@@ -68,38 +69,33 @@ func produceText(q *queues.Producer, req *http.Request) error {
6869
if err != nil {
6970
return fmt.Errorf("failed to read request body: %w", err)
7071
}
71-
if len(content) == 0 {
72-
return fmt.Errorf("empty request body")
73-
}
74-
75-
// text content type supports string and []byte messages
76-
if err := q.Send(content, queues.WithContentType(queues.QueueContentTypeText)); err != nil {
72+
// text content type supports string
73+
if err := q.SendText(string(content)); err != nil {
7774
return fmt.Errorf("failed to send message: %w", err)
7875
}
79-
8076
return nil
8177
}
8278

83-
func produceJson(q *queues.Producer, req *http.Request) error {
79+
func produceJSON(q *queues.Producer, req *http.Request) error {
8480
var data any
8581
if err := json.NewDecoder(req.Body).Decode(&data); err != nil {
8682
return fmt.Errorf("failed to read request body: %w", err)
8783
}
88-
89-
// json content type is default and therefore can be omitted
9084
// json content type supports messages of types that can be serialized to json
91-
if err := q.Send(data); err != nil {
85+
if err := q.SendJSON(data); err != nil {
9286
return fmt.Errorf("failed to send message: %w", err)
9387
}
94-
9588
return nil
9689
}
9790

9891
func produceBytes(q *queues.Producer, req *http.Request) error {
99-
// bytes content type support messages of type []byte, string, and io.Reader
100-
if err := q.Send(req.Body, queues.WithContentType(queues.QueueContentTypeBytes)); err != nil {
92+
content, err := io.ReadAll(req.Body)
93+
if err != nil {
94+
return fmt.Errorf("failed to read request body: %w", err)
95+
}
96+
// bytes content type support messages of type []byte
97+
if err := q.SendBytes(content); err != nil {
10198
return fmt.Errorf("failed to send message: %w", err)
10299
}
103-
104100
return nil
105101
}

0 commit comments

Comments
 (0)