Commit c69c99a 1 parent ad2e530 commit c69c99a Copy full SHA for c69c99a
File tree 2 files changed +63
-52
lines changed
2 files changed +63
-52
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 1
1
package main
2
2
3
3
import (
4
- "os"
5
-
6
- "encoding/json"
7
-
8
- "fmt"
9
-
10
- "io/ioutil"
11
-
12
4
"flag"
13
-
5
+ "io/ioutil"
6
+ "os"
14
7
"time"
15
8
16
9
"github.com/bhoriuchi/go-bunyan/bunyan"
@@ -63,54 +56,19 @@ func main() {
63
56
// discard default logger
64
57
gin .DefaultWriter = ioutil .Discard
65
58
66
- //get a router
59
+ // get a router
67
60
r := gin .Default ()
68
61
62
+ // add queue to the context
63
+ r .Use (func (c * gin.Context ) {
64
+ c .Set ("Q" , q )
65
+ c .Next ()
66
+ })
67
+
69
68
// use bunyan logger
70
69
r .Use (ginbunyan .Ginbunyan (& blog ))
71
70
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 )
114
72
115
73
blog .Info ("Listening on port %s" , * port )
116
74
// block on server run
You can’t perform that action at this time.
0 commit comments