forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhealth.go
58 lines (45 loc) · 1004 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
48
49
50
51
52
53
54
55
56
57
58
//go:build !windows
package cmd
import (
"fmt"
"net/http"
"os"
"time"
"github.com/evcc-io/evcc/server"
"github.com/spf13/cobra"
"github.com/tv42/httpunix"
)
const serviceName = "evcc"
// healthCmd represents the meter command
var healthCmd = &cobra.Command{
Use: "health",
Short: "Check application health",
Run: runHealth,
}
func init() {
rootCmd.AddCommand(healthCmd)
}
func runHealth(cmd *cobra.Command, args []string) {
u := &httpunix.Transport{
DialTimeout: 100 * time.Millisecond,
RequestTimeout: 1 * time.Second,
ResponseHeaderTimeout: 1 * time.Second,
}
u.RegisterLocation(serviceName, server.SocketPath)
client := http.Client{
Transport: u,
}
var ok bool
resp, err := client.Get(fmt.Sprintf("http+unix://%s/health", serviceName))
if err == nil {
resp.Body.Close()
if resp.StatusCode == http.StatusOK {
log.INFO.Printf("health check ok")
ok = true
}
}
if !ok {
log.ERROR.Printf("health check failed")
os.Exit(1)
}
}