-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
72 lines (64 loc) · 2.09 KB
/
handlers.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
package main
import (
"meowtrics/model"
"net/http"
"time"
"github.com/gorilla/mux"
"github.com/unrolled/render"
)
var r render.Render
const (
APPLICATION_PROTOBUF = "application/x-protobuf"
APPLICATION_JSON = "application/json"
APPLICATION_ALL = "*/*"
)
func HeartBeatHandler() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
status := "OK"
timestamp := time.Now().UTC().String()
r.JSON(w, http.StatusOK, model.HeartBeat{Status: &status, Timestamp: ×tamp})
})
}
func NotFoundHandler() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
code := ContentNotFound
errorMesage := "Nothing to see here"
response := model.ErrorResponse{Code: &code, ErrorMessage: &errorMesage}
r.JSON(w, http.StatusNotFound, response)
})
}
func CreateEventHandler() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
contentHeader := req.Header.Get("Content-Type")
var status int
var errResp *model.ErrorResponse
switch contentHeader {
case APPLICATION_JSON:
status, errResp = processJsonPost(req, meowtricsLogger)
case APPLICATION_PROTOBUF:
status, errResp = processProtobufPost(req, meowtricsLogger)
default:
status, errResp = processUnsupportedMediaTypePost(req, meowtricsLogger)
}
//Response body to this post request is always returned as JSON because it is human readable in case of errors
r.JSON(w, status, errResp)
})
}
func RetrieveEventHandler() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
acceptHeader := req.Header.Get("Accept")
id := mux.Vars(req)["id"]
switch acceptHeader {
case APPLICATION_PROTOBUF:
status, data := processProtobufGet(id, meowtricsLogger)
w.Header().Set("Content-Type", APPLICATION_PROTOBUF)
r.Data(w, status, data)
case APPLICATION_JSON, APPLICATION_ALL, "":
status, event := processJsonGet(id, meowtricsLogger)
r.JSON(w, status, event)
default:
status, errResp := processUnsupportedMediaTypeGet(req, meowtricsLogger)
r.JSON(w, status, errResp)
}
})
}