-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
65 lines (50 loc) · 1.17 KB
/
main.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
package main
import (
"flag"
"fmt"
"github.com/go-redis/redis/v7"
"net/http"
"strings"
"time"
)
func main() {
h := flag.String("h", "127.0.0.1", "KeyDB host")
p := flag.String("p", "6379", "KeyDB port")
sp := flag.String("sp", "8080", "Server listener port")
flag.Parse()
client := redis.NewClient(&redis.Options{
Addr: fmt.Sprintf("%s:%s", *h, *p),
DialTimeout: time.Second,
ReadTimeout: time.Second,
})
http.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
result, err := client.Info().Result()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
info := parseInfo(result)
if info["loading"] != "0" || info["master_sync_in_progress"] != "0" {
w.WriteHeader(http.StatusInternalServerError)
return
}
})
if err := http.ListenAndServe(":"+*sp, nil); err != nil {
panic(err)
}
}
func parseInfo(str string) map[string]string {
info := make(map[string]string)
lines := strings.Split(str, "\r\n")
for _, line := range lines {
if strings.HasPrefix(line, "#") {
continue
}
pair := strings.Split(line, ":")
if len(pair) != 2 {
continue
}
info[pair[0]] = pair[1]
}
return info
}