Skip to content

Commit c69c99a

Browse files
committed
housekeeping code cleanup
1 parent ad2e530 commit c69c99a

File tree

2 files changed

+63
-52
lines changed

2 files changed

+63
-52
lines changed

rtq/routes.go

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package rtq
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
7+
"github.com/gin-gonic/gin"
8+
)
9+
10+
// RxRouteHandler handles the http route for inbound data
11+
func RxRouteHandler(c *gin.Context) {
12+
//var json map[string]interface{}
13+
producer := c.Param("producer")
14+
key := c.Param("key")
15+
label := c.Param("label")
16+
17+
rawData, _ := c.GetRawData()
18+
19+
// all data is json
20+
payload := make(map[string]interface{})
21+
err := json.Unmarshal(rawData, &payload)
22+
if err != nil {
23+
c.JSON(500, gin.H{
24+
"status": "FAIL",
25+
"message": fmt.Sprintf("could not unmarshal json: %s", rawData),
26+
})
27+
return
28+
}
29+
30+
// build the message
31+
msg := Message{
32+
Producer: producer,
33+
Label: label,
34+
Key: key,
35+
Payload: payload,
36+
}
37+
38+
// write the message
39+
q := c.MustGet("Q").(*rtQ)
40+
err = q.QWrite(msg)
41+
if err != nil {
42+
c.JSON(500, gin.H{
43+
"status": "FAIL",
44+
"message": fmt.Sprintf("failed to write message: %s", err.Error()),
45+
})
46+
return
47+
}
48+
49+
c.JSON(200, gin.H{
50+
"status": "OK",
51+
})
52+
53+
}

rxtx.go

+10-52
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,9 @@
11
package main
22

33
import (
4-
"os"
5-
6-
"encoding/json"
7-
8-
"fmt"
9-
10-
"io/ioutil"
11-
124
"flag"
13-
5+
"io/ioutil"
6+
"os"
147
"time"
158

169
"github.com/bhoriuchi/go-bunyan/bunyan"
@@ -63,54 +56,19 @@ func main() {
6356
// discard default logger
6457
gin.DefaultWriter = ioutil.Discard
6558

66-
//get a router
59+
// get a router
6760
r := gin.Default()
6861

62+
// add queue to the context
63+
r.Use(func(c *gin.Context) {
64+
c.Set("Q", q)
65+
c.Next()
66+
})
67+
6968
// use bunyan logger
7069
r.Use(ginbunyan.Ginbunyan(&blog))
7170

72-
r.POST("/rx/:producer/:key/*label", func(c *gin.Context) {
73-
//var json map[string]interface{}
74-
producer := c.Param("producer")
75-
key := c.Param("key")
76-
label := c.Param("label")
77-
78-
rawData, _ := c.GetRawData()
79-
80-
// all data is json
81-
payload := make(map[string]interface{})
82-
err := json.Unmarshal(rawData, &payload)
83-
if err != nil {
84-
c.JSON(500, gin.H{
85-
"status": "FAIL",
86-
"message": fmt.Sprintf("could not unmarshal json: %s", rawData),
87-
})
88-
return
89-
}
90-
91-
// build the message
92-
msg := rtq.Message{
93-
Producer: producer,
94-
Label: label,
95-
Key: key,
96-
Payload: payload,
97-
}
98-
99-
// write the message
100-
err = q.QWrite(msg)
101-
if err != nil {
102-
c.JSON(500, gin.H{
103-
"status": "FAIL",
104-
"message": fmt.Sprintf("failed to write message: %s", err.Error()),
105-
})
106-
return
107-
}
108-
109-
c.JSON(200, gin.H{
110-
"status": "OK",
111-
})
112-
113-
})
71+
r.POST("/rx/:producer/:key/*label", rtq.RxRouteHandler)
11472

11573
blog.Info("Listening on port %s", *port)
11674
// block on server run

0 commit comments

Comments
 (0)