-
Notifications
You must be signed in to change notification settings - Fork 35
/
health.go
47 lines (39 loc) · 934 Bytes
/
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
package cli
import (
"encoding/json"
"fmt"
"io"
"net/http"
"github.com/pkg/errors"
"github.com/runabol/tork/conf"
ucli "github.com/urfave/cli/v2"
)
func (c *CLI) healthCmd() *ucli.Command {
return &ucli.Command{
Name: "health",
Usage: "Perform a health check",
Action: health,
}
}
func health(_ *ucli.Context) error {
chk, err := http.Get(fmt.Sprintf("%s/health", conf.StringDefault("endpoint", "http://localhost:8000")))
if err != nil {
return err
}
if chk.StatusCode != http.StatusOK {
return errors.Errorf("Health check failed. Status Code: %d", chk.StatusCode)
}
body, err := io.ReadAll(chk.Body)
if err != nil {
return errors.Wrapf(err, "error reading body")
}
type resp struct {
Status string `json:"status"`
}
r := resp{}
if err := json.Unmarshal(body, &r); err != nil {
return errors.Wrapf(err, "error unmarshalling body")
}
fmt.Printf("Status: %s\n", r.Status)
return nil
}