This repository has been archived by the owner on Nov 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
handlers.go
64 lines (56 loc) · 1.66 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
package main
import (
"github.com/datatogether/api/apiutil"
"github.com/datatogether/coverage/tree"
"io"
"net/http"
)
func RootNodeHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "OPTIONS":
EmptyOkHandler(w, r)
case "GET":
apiutil.WriteResponse(w, tree.CopyToDepth(t, 1))
default:
NotFoundHandler(w, r)
}
}
func NodeHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "OPTIONS":
EmptyOkHandler(w, r)
case "GET":
id := r.URL.Path[len("/tree/"):]
node := t.Find(id)
if node == nil {
apiutil.WriteErrResponse(w, http.StatusNotFound, ErrNotFound)
return
}
apiutil.WriteResponse(w, tree.CopyToDepth(node, 1))
default:
NotFoundHandler(w, r)
}
}
func FullTreeHandler(w http.ResponseWriter, r *http.Request) {
apiutil.WriteResponse(w, t)
}
// HealthCheckHandler is a basic "hey I'm fine" for load balancers & co
// TODO - add Database connection & proper configuration checks here for more accurate
// health reporting
func HealthCheckHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{ "status" : 200 }`))
}
// EmptyOkHandler is an empty 200 response, often used
// for OPTIONS requests that responds with headers set in addCorsHeaders
func EmptyOkHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}
// CertbotHandler pipes the certbot response for manual certificate generation
func CertbotHandler(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, cfg.CertbotResponse)
}
func NotFoundHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
w.Write([]byte(`{ "status" : "not found" }`))
}