-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.go
46 lines (38 loc) · 1001 Bytes
/
handler.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
package main
import (
"encoding/json"
"io/ioutil"
"net/http"
"strings"
)
// requestHandler handles the HTTP request
func requestHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-type", "application/json")
token := strings.TrimSpace(r.Header.Get("token"))
if secret != token {
logger.Printf("Invalid token: " + token)
writeResp(w, http.StatusForbidden, "Invalid Token")
return
}
data, err := ioutil.ReadAll(r.Body)
if err != nil {
logger.Printf("Error getting data: " + err.Error())
writeResp(w, http.StatusBadRequest, "Invalid Content")
return
}
if len(data) > 0 {
logger.Println(string(data))
err = que.push(r.Context(), data)
if err != nil {
logger.Printf("Error storing data: " + err.Error())
writeResp(w, http.StatusBadRequest, "Internal Error")
return
}
}
writeResp(w, http.StatusOK, "OK")
return
}
func writeResp(w http.ResponseWriter, status int, msg string) {
w.WriteHeader(status)
json.NewEncoder(w).Encode(msg)
}