-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathhttp.go
151 lines (141 loc) · 4.66 KB
/
http.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package healthchecks
import (
"fmt"
"io"
"net/http"
"strconv"
"strings"
)
// Handler returns a http.Handler that responds to status check requests. It should be registered at `/status/...`
func Handler(statusEndpoints []StatusEndpoint, aboutFilePath string, versionFilePath string, customData map[string]interface{}) http.Handler {
return HandlerFunc(statusEndpoints, aboutFilePath, versionFilePath, customData)
}
// HandlerFunc returns a http.HandlerFunc that responds to status check requests. It should be registered at `/status/...`
func HandlerFunc(statusEndpoints []StatusEndpoint, aboutFilePath string, versionFilePath string, customData map[string]interface{}) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
slug := strings.Split(r.URL.Path, "/")
apiVersion := APIV1
if strings.ToLower(slug[2]) == "v2" {
apiVersion = APIV2
}
switch apiVersion {
case APIV1:
handleV1Api(w, r, slug[2], statusEndpoints, aboutFilePath, versionFilePath, customData)
case APIV2:
handleV2Api(w, r, slug[3], statusEndpoints, aboutFilePath, versionFilePath, customData)
}
})
}
func handleV1Api(
w http.ResponseWriter,
r *http.Request,
endpoint string,
statusEndpoints []StatusEndpoint,
aboutFilePath string,
versionFilePath string,
customData map[string]interface{},
) {
switch endpoint {
case "about":
w.Header().Set("Content-Type", "application/json; charset=utf-8")
aboutResp, _ := About(statusEndpoints, ABOUT_PROTOCOL_HTTP, aboutFilePath, versionFilePath, customData, APIV1, true)
io.WriteString(w, aboutResp)
case "aggregate":
typeFilter := r.URL.Query().Get("type")
w.Header().Set("Content-Type", "application/json; charset=utf-8")
io.WriteString(w, Aggregate(statusEndpoints, typeFilter, APIV1))
case "am-i-up":
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
io.WriteString(w, "OK")
case "traverse":
action := r.URL.Query().Get("action")
if action == "" {
action = "about"
}
dependencies := []string{}
queryDependencies := r.URL.Query().Get("dependencies")
if queryDependencies != "" {
dependencies = strings.Split(queryDependencies, ",")
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
io.WriteString(w, Traverse(statusEndpoints, dependencies, action, ABOUT_PROTOCOL_HTTP, aboutFilePath, versionFilePath, customData))
default:
endpoint := FindStatusEndpoint(statusEndpoints, endpoint)
if endpoint == nil {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusNotFound)
io.WriteString(w, SerializeStatusList(StatusList{
StatusList: []Status{
{
Description: "Unknown Status endpoint",
Result: CRITICAL,
Details: fmt.Sprintf("Status endpoint does not exist: %s", r.URL.Path),
},
},
}, APIV1))
return
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
io.WriteString(w, ExecuteStatusCheck(endpoint, APIV1))
}
}
func handleV2Api(
w http.ResponseWriter,
r *http.Request,
endpoint string,
statusEndpoints []StatusEndpoint,
aboutFilePath string,
versionFilePath string,
customData map[string]interface{},
) {
switch endpoint {
case "about":
checkStatusStr := r.URL.Query().Get("checkStatus")
checkStatus := true
if checkStatusStr != "" {
checkStatus, _ = strconv.ParseBool(checkStatusStr)
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
aboutResp, _ := About(statusEndpoints, ABOUT_PROTOCOL_HTTP, aboutFilePath, versionFilePath, customData, APIV2, checkStatus)
io.WriteString(w, aboutResp)
case "aggregate":
typeFilter := r.URL.Query().Get("type")
w.Header().Set("Content-Type", "application/json; charset=utf-8")
io.WriteString(w, Aggregate(statusEndpoints, typeFilter, APIV2))
case "am-i-up":
w.Header().Set("Content-Type", "application/json; charset=utf-8")
io.WriteString(
w,
SerializeStatusList(
StatusList{
StatusList: []Status{
Status{
Description: "Am I Up",
Result: OK,
Details: "The service is running",
},
},
},
APIV2,
),
)
default:
endpoint := FindStatusEndpoint(statusEndpoints, endpoint)
if endpoint == nil {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusNotFound)
io.WriteString(w, SerializeStatusList(StatusList{
StatusList: []Status{
{
Description: "Unknown Status endpoint",
Result: CRITICAL,
Details: fmt.Sprintf("Status endpoint does not exist: %s", r.URL.Path),
},
},
}, APIV2))
return
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
io.WriteString(w, ExecuteStatusCheck(endpoint, APIV2))
}
}