/
health.go
69 lines (57 loc) · 1.31 KB
/
health.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
package command
import (
"fmt"
"net/http"
"github.com/gopad/gopad-ui/pkg/config"
"github.com/rs/zerolog/log"
"github.com/urfave/cli/v2"
)
// Health provides the sub-command to perform a health check.
func Health(cfg *config.Config) *cli.Command {
return &cli.Command{
Name: "health",
Usage: "Perform health checks",
Flags: HealthFlags(cfg),
Action: HealthAction(cfg),
}
}
// HealthFlags defines health flags.
func HealthFlags(cfg *config.Config) []cli.Flag {
return []cli.Flag{
&cli.StringFlag{
Name: "metrics-addr",
Value: defaultMetricsAddr,
Usage: "Address to bind the metrics",
EnvVars: []string{"GOPAD_UI_METRICS_ADDR"},
Destination: &cfg.Metrics.Addr,
},
}
}
// HealthAction defines health action.
func HealthAction(cfg *config.Config) cli.ActionFunc {
return func(c *cli.Context) error {
resp, err := http.Get(
fmt.Sprintf(
"http://%s/healthz",
cfg.Metrics.Addr,
),
)
if err != nil {
log.Error().
Err(err).
Msg("Failed to request health check")
return err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
log.Error().
Int("code", resp.StatusCode).
Msg("Health seems to be in bad state")
return err
}
log.Debug().
Int("code", resp.StatusCode).
Msg("Health got a good state")
return nil
}
}