-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
158 lines (123 loc) · 3.42 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package main
import (
"bytes"
"encoding/json"
"io/ioutil"
"log"
"net/http"
"text/template"
"time"
"github.com/bmizerany/pat"
)
var (
templates *template.Template
)
type SMS struct {
ID string
To string
From string
Body string
Timestamp string
}
func nexmo(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
log.Println(err)
return
}
if r.Form.Get("type") != "text" {
log.Println("Invalid message type")
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("ERROR"))
return
}
// YYYY-MM-DD HH:MM:SS
timestamp, err := time.Parse("2006-01-02 15:04:05", r.Form.Get("message-timestamp"))
if err != nil {
log.Println(err)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("ERROR"))
return
}
var jsonStr = []byte(`{"to":"` + r.Form.Get("to") + `","from":"` + r.Form.Get("msisdn") + `","body":"` + r.Form.Get("text") + `","timestamp":"` + timestamp.UTC().String() + `"}`)
req, err := http.NewRequest("POST", "http://127.0.0.1:9200/messages/"+r.Form.Get("msisdn"), bytes.NewBuffer(jsonStr))
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
res, err := client.Do(req)
if err != nil {
panic(err)
}
defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)
log.Println(string(body))
w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
}
func dashboard(w http.ResponseWriter, r *http.Request) {
var jsonStr = []byte(`{"query":{"match_all":{}},"aggs":{"top-types":{"terms":{"field":"_type"},"aggs":{"top_docs":{"top_hits":{"sort":[{"_score":{"order":"desc"}}],"size":1}}}}}}`)
req, err := http.NewRequest("POST", "http://127.0.0.1:9200/messages/_search?search_type=count", bytes.NewBuffer(jsonStr))
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
res, err := client.Do(req)
if err != nil {
panic(err)
}
defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)
log.Println(string(body))
data := struct {
Aggregations struct {
Top struct {
Buckets []struct {
Top struct {
Hits struct {
Hits []struct {
Index string `json:"_index"`
Type string `json:"_type"`
ID string `json:"_id"`
Source struct {
To string `json:"to"`
From string `json:"from"`
Body string `json:"body"`
Timestamp string `json:"timestamp"`
} `json:"_source"`
} `json:"hits"`
} `json:"hits"`
} `json:"top_docs"`
} `json:"buckets"`
} `json:"top-types"`
} `json:"aggregations"`
}{}
err = json.Unmarshal(body, &data)
if err != nil {
panic(err)
}
messages := []SMS{}
for _, bucket := range data.Aggregations.Top.Buckets {
messages = append(messages, SMS{
bucket.Top.Hits.Hits[0].ID,
bucket.Top.Hits.Hits[0].Source.To,
bucket.Top.Hits.Hits[0].Source.From,
bucket.Top.Hits.Hits[0].Source.Body,
bucket.Top.Hits.Hits[0].Source.Timestamp,
})
}
log.Println(messages)
err = templates.ExecuteTemplate(w, "index.html", struct{ Messages []SMS }{messages})
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("ERROR"))
return
}
}
func main() {
var err error
templates, err = template.ParseGlob("templates/*")
if err != nil {
panic(err)
}
router := pat.New()
router.Get("/hooks/nexmo", http.HandlerFunc(nexmo))
router.Get("/", http.HandlerFunc(dashboard))
log.Fatal(http.ListenAndServe(":8080", router))
}