forked from cayleygraph/cayley
-
Notifications
You must be signed in to change notification settings - Fork 0
/
health.go
39 lines (35 loc) · 845 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
package command
import (
"fmt"
"log"
"net/http"
"github.com/spf13/cobra"
)
const defaultAddress = "http://localhost:64210/"
func NewHealthCmd() *cobra.Command {
return &cobra.Command{
Use: "health",
Aliases: []string{},
Short: "Health check HTTP server",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) > 1 {
return fmt.Errorf("Too many arguments provided, expected 0 or 1")
}
address := defaultAddress
if len(args) == 1 {
address = args[0]
}
healthAddress := address + "health"
resp, err := http.Get(healthAddress)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != 204 {
return fmt.Errorf("/health responded with status code %d, expected 204", resp.StatusCode)
}
log.Printf("%s ok", healthAddress)
return nil
},
}
}