-
Notifications
You must be signed in to change notification settings - Fork 0
/
resthandlers.go
116 lines (102 loc) · 2.56 KB
/
resthandlers.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"time"
)
type errorMessage struct {
Message string `json:"message"`
}
func jsonError(w http.ResponseWriter, err interface{}, code int) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Header().Set("X-Content-Type-Options", "nosniff")
w.WriteHeader(code)
j, _ := json.Marshal(err)
w.Write(j)
// json.NewEncoder(w).Encode(err)
}
func middlewareContentType(nextHandler func(http.ResponseWriter, *http.Request)) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
apikey := r.Header["X-Api-Key"]
if len(apikey) > 0 && apikey[0] != config.APIKey {
jsonError(w, errorMessage{Message: "unauthorised"}, http.StatusUnauthorized)
return
}
nextHandler(w, r)
}
}
func catchAllHandler(w http.ResponseWriter, r *http.Request) {
buf := new(bytes.Buffer)
buf.ReadFrom(r.Body)
body := buf.String()
rsp := catchAllResponse{
Method: r.Method,
RequestURI: r.RequestURI,
URL: r.URL,
ContentLength: r.ContentLength,
Host: r.Host,
Proto: r.Proto,
RemoteAddr: r.RemoteAddr,
Body: body,
}
j, _ := json.Marshal(rsp)
w.Write(j)
}
// GET /api/health
func healthHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
jsonError(
w,
unsupportedResponse{SupportedMethods: []string{"GET"}},
http.StatusMethodNotAllowed,
)
return
}
status := "ok"
var info string
d, err := db.Ping()
if err != nil {
status = "degraded"
info = err.Error()
}
rsp := healthResponse{
Status: status,
Version: buildNumber,
Info: info,
DBResponse: d.String(),
LiveTime: liveTime,
DebugLevel: config.DebugLevel,
}
j, _ := json.Marshal(rsp)
w.Write(j)
}
// GET /api/events?guildID={Guild_ID}
func listEventsHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
jsonError(
w,
unsupportedResponse{SupportedMethods: []string{"GET"}},
http.StatusMethodNotAllowed,
)
return
}
guilID := r.URL.Query().Get("guildId")
if guilID == "" {
jsonError(w, errorMessage{Message: "guildId not provided"}, http.StatusBadRequest)
return
}
events, err := db.GetEvents(guilID, "all", time.Time{})
if err != nil {
jsonError(w, errorMessage{Message: err.Error()}, http.StatusInternalServerError)
return
}
rsp, err := json.Marshal(events)
if err != nil {
jsonError(w, errorMessage{Message: err.Error()}, http.StatusInternalServerError)
return
}
fmt.Fprint(w, string(rsp))
}