This repository has been archived by the owner on Dec 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
84 lines (69 loc) · 1.78 KB
/
main.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
package main
import (
"encoding/json"
"net/http"
"os"
"strings"
"time"
"github.com/apex/log"
"github.com/apex/log/handlers/text"
)
func main() {
log.SetHandler(text.New(os.Stdout))
configureEmon()
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
log.WithFields(log.Fields{
"method": r.Method,
"path": r.RequestURI,
"direction": "incoming",
}).Infof("HTTP %s %s", r.Method, r.RequestURI)
if strings.HasPrefix(r.RequestURI, "/favicon.ico") {
w.WriteHeader(http.StatusNotFound)
return
}
status := "healthy"
statusCode := http.StatusOK
checkSets, code := runHealthchecks()
switch code {
case 1:
status = "warn"
case 2:
status = "alert"
statusCode = http.StatusFailedDependency
}
resp := apiResponse{
Status: status,
Checks: make(map[string]apiChecks),
}
for _, cs := range checkSets {
if resp.Checks[cs.source] == nil {
resp.Checks[cs.source] = apiChecks{}
}
for _, c := range cs.checks {
resp.Checks[cs.source][c.Name] = c
}
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(statusCode)
js, err := json.Marshal(resp)
if err == nil {
w.Write(js)
}
log.WithFields(log.Fields{
"status": statusCode,
"direction": "outgoing",
}).Infof("HTTP %s %s - %s", r.Method, r.RequestURI, http.StatusText(statusCode))
})
log.WithFields(log.Fields{
"EmonHTTPBindAddress": config.EmonHTTPBindAddress,
"EmonSlowCheckThreshold": config.EmonSlowCheckThreshold,
"ClusterHTTPEndpoint": cleanURL(config.ClusterHTTPEndpoint),
"ClusterSize": config.ClusterSize,
}).Infof("emon started")
srv := &http.Server{
Addr: config.EmonHTTPBindAddress,
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
}
srv.ListenAndServe()
}