This repository has been archived by the owner on May 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.go
75 lines (60 loc) · 2.01 KB
/
index.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
// Package health contains an HTTP server that renders a basic HTML UI displaying the results of health
// checks of kubernetes pods that have special annotations.
package health
import (
"context"
"net/http"
"sort"
"text/template"
"github.com/gorilla/mux"
"pkg.dsb.dev/transport"
"github.com/davidsbond/homelab/internal/health/assets"
"github.com/davidsbond/homelab/internal/health/scrape"
)
//go:generate go-bindata -pkg assets -prefix assets -nometadata -ignore bindata -o ./assets/bindata.go ./assets
type (
// The HTTP type is responsible for handling inbound HTTP requests to display the health check user-interface.
HTTP struct {
transport.HTTP
scraper Scraper
}
// The Scraper interface describes types that scrape kubernetes pods for their current health.
Scraper interface {
Scrape(ctx context.Context) ([]*scrape.PodHealth, error)
}
)
// NewHTTP returns a new instance of the HTTP type that will scrape pod health using the provided Scraper
// implementation.
func NewHTTP(scraper Scraper) *HTTP {
return &HTTP{scraper: scraper}
}
// Register HTTP handlers on the provided mux.Router.
func (t *HTTP) Register(r *mux.Router) {
r.HandleFunc("/", t.Get).Methods(http.MethodGet)
}
// Get handles an inbound HTTP GET request that performs a health scrape and renders the health check UI.
func (t *HTTP) Get(w http.ResponseWriter, r *http.Request) {
const file = "index.gohtml"
data, err := assets.Asset(file)
if err != nil {
t.Error(r.Context(), w, http.StatusInternalServerError, err.Error())
return
}
tpl, err := template.New("").Parse(string(data))
if err != nil {
t.Error(r.Context(), w, http.StatusInternalServerError, err.Error())
return
}
apps, err := t.scraper.Scrape(r.Context())
if err != nil {
t.Error(r.Context(), w, http.StatusInternalServerError, err.Error())
return
}
sort.Slice(apps, func(i, j int) bool {
return apps[i].Name < apps[j].Name
})
if err = tpl.Execute(w, apps); err != nil {
t.Error(r.Context(), w, http.StatusInternalServerError, err.Error())
return
}
}