/
readable_health_handler.go
44 lines (35 loc) · 1.24 KB
/
readable_health_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
// Copyright © 2019 by PACE Telematics GmbH. All rights reserved.
// Created at 2019/12/05 by Charlotte Pröller
package servicehealthcheck
import (
"fmt"
"net/http"
"strconv"
"strings"
"github.com/pace/bricks/maintenance/log"
)
// saves length of the longest name for the column width in the table. 20 characters width is the default
var longestCheckName = 20
type readableHealthHandler struct{}
func (h *readableHealthHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
s := log.Sink{Silent: true}
ctx := log.ContextWithSink(r.Context(), &s)
reqChecks := check(ctx, &requiredChecks)
optChecks := check(ctx, &optionalChecks)
status := http.StatusOK
table := "%-" + strconv.Itoa(longestCheckName) + "s %-3s %s\n"
bodyBuilder := &strings.Builder{}
bodyBuilder.WriteString("Required Services: \n")
for name, res := range reqChecks {
bodyBuilder.WriteString(fmt.Sprintf(table, name, res.State, res.Msg))
if res.State == Err {
status = http.StatusServiceUnavailable
}
}
bodyBuilder.WriteString("Optional Services: \n")
for name, res := range optChecks {
bodyBuilder.WriteString(fmt.Sprintf(table, name, res.State, res.Msg))
// do not change status, as this is optional
}
writeResult(w, status, bodyBuilder.String())
}