@@ -23,6 +23,7 @@ func main() {
2323 http .HandleFunc ("/" , handleProduce )
2424 workers .Serve (nil )
2525}
26+
2627func 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
9891func 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