-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
88 lines (77 loc) · 2.14 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
85
86
87
88
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
)
var logger = log.New(os.Stdout, "covid19-at", 0)
var mp = newMetadataProvider()
var he = newHealthMinistryExporter()
var exporters = []Exporter{
he,
newEcdcExporter(mp),
newMathdroExporter(),
}
var a = newApi(he)
func writeJson(w http.ResponseWriter, f func() (interface{}, error)) {
result, err := f()
if err != nil {
w.WriteHeader(500)
w.Write([]byte(err.Error()))
} else {
bytes, err := json.Marshal(result)
if err != nil {
w.WriteHeader(500)
w.Write([]byte(err.Error()))
} else {
w.Header().Add("Content-type", "application/json; charset=utf-8")
w.Write(bytes)
}
}
}
func handleApiBundesland(w http.ResponseWriter, _ *http.Request) {
writeJson(w, func() (interface{}, error) { return a.GetBundeslandStat() })
}
func handleApiBezirk(w http.ResponseWriter, _ *http.Request) {
writeJson(w, func() (interface{}, error) { return a.GetBezirkStat() })
}
func handleApiTotal(w http.ResponseWriter, _ *http.Request) {
writeJson(w, func() (interface{}, error) { return a.GetOverallStat() })
}
func handleMetrics(w http.ResponseWriter, _ *http.Request) {
for _, e := range exporters {
metrics, err := e.GetMetrics()
if err == nil {
writeMetrics(metrics, w)
}
}
}
func handleHealth(w http.ResponseWriter, _ *http.Request) {
errors := make([]error, 0)
for _, e := range exporters {
errors = append(errors, e.Health()...)
}
if len(errors) > 0 {
w.WriteHeader(http.StatusInternalServerError)
errorResponse := ""
for _, e := range errors {
errorResponse += e.Error() + "\n"
}
fmt.Fprintf(w, `<html><body><img width="500" src="https://spiessknafl.at/fine.jpg"/><pre>%s</pre></body></html>`, errorResponse)
} else {
fmt.Fprintf(w, `<html><body><img width="500" src="https://spiessknafl.at/helth.png"/></body></html>`)
}
}
func main() {
http.HandleFunc("/metrics", handleMetrics)
http.HandleFunc("/health", handleHealth)
http.HandleFunc("/api/bundesland", handleApiBundesland)
http.HandleFunc("/api/bezirk", handleApiBezirk)
http.HandleFunc("/api/total", handleApiTotal)
err := http.ListenAndServe(":8282", nil)
if err != nil {
panic(err)
}
}