-
Notifications
You must be signed in to change notification settings - Fork 3
/
messages.go
47 lines (39 loc) · 1023 Bytes
/
messages.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
package main
import (
"encoding/json"
"io/ioutil"
"net/http"
)
type Message struct {
Type string `json:"type"`
DestinationDeviceId int64 `json:"destinationDeviceId"`
DestinationRegistrationId int64 `json:"destinationRegistrationId"`
Body string `json:"body"`
timestamp string `json:"timestamp"`
}
type Messages struct {
Relay string `json:"relay"`
Messages []Message `json:"messages"`
}
func submitMessages(w http.ResponseWriter, req *http.Request) {
uname, _, ok := req.BasicAuth()
if !ok {
http.Error(w, "auth fail", 401)
return
}
body, err := ioutil.ReadAll(req.Body)
if err != nil {
http.Error(w, "server error", 500)
return
}
var messages Messages
err = json.Unmarshal(body, &messages)
if err != nil {
http.Error(w, "server error", 500)
return
}
log.Infof("number: %s, messages_count: %d", uname, len(messages.Messages))
id := encodeNumber(uname)
writeDB(id[:], []byte("m"), body)
w.WriteHeader(200)
}