-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
87 lines (65 loc) · 1.29 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package main
import (
"net/http"
"os"
"os/signal"
"sync"
"github.com/kak-tus/healthcheck"
"github.com/kak-tus/ruthie/config"
"github.com/kak-tus/ruthie/reader"
"github.com/kak-tus/ruthie/writer"
"go.uber.org/zap"
)
var rdr *reader.Reader
var wrt *writer.Writer
func main() {
logger, err := zap.NewProduction()
if err != nil {
panic(err)
}
log := logger.Sugar()
cnf, err := config.NewConfig()
if err != nil {
log.Panic(err)
}
hlth := healthcheck.NewHandler()
hlth.Add("/healthcheck", func() (healthcheck.State, string) {
return healthcheck.StatePassing, "ok"
})
rdr, err = reader.NewReader(&cnf.Reader, log)
if err != nil {
log.Panic(err)
}
wrt, err = writer.NewWriter(&cnf.Writer, log, rdr)
if err != nil {
log.Panic(err)
}
go wrt.Start()
hlth.Add("/status", status)
go http.ListenAndServe(cnf.Healthcheck.Listen, hlth)
st := make(chan os.Signal, 1)
signal.Notify(st, os.Interrupt)
<-st
log.Info("Stop")
wrt.Stop()
_ = log.Sync()
}
func status() (healthcheck.State, string) {
var wg sync.WaitGroup
wg.Add(2)
var rs bool
go func() {
rs = rdr.IsAccessible()
wg.Done()
}()
var ws bool
go func() {
ws = wrt.IsAccessible()
wg.Done()
}()
wg.Wait()
if rs && ws {
return healthcheck.StatePassing, "ok"
}
return healthcheck.StateWarning, "nok"
}