This repository has been archived by the owner on Sep 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathmain.go
102 lines (88 loc) · 2.55 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package main
import (
"database/sql"
"fmt"
"net"
"net/http"
"os"
"time"
"code.cloudfoundry.org/lager"
_ "github.com/go-sql-driver/mysql"
"github.com/cloudfoundry-incubator/galera-healthcheck/api"
"github.com/cloudfoundry-incubator/galera-healthcheck/config"
"github.com/cloudfoundry-incubator/galera-healthcheck/healthcheck"
"github.com/cloudfoundry-incubator/galera-healthcheck/monit_client"
"github.com/cloudfoundry-incubator/galera-healthcheck/mysqld_cmd"
"github.com/cloudfoundry-incubator/galera-healthcheck/node_manager"
"github.com/cloudfoundry-incubator/galera-healthcheck/sequence_number"
)
func main() {
rootConfig, err := config.NewConfig(os.Args)
logger := rootConfig.Logger
err = rootConfig.Validate()
if err != nil {
logger.Fatal("Failed to validate config", err)
}
db, err := sql.Open("mysql",
fmt.Sprintf("%s:%s@unix(%s)/",
rootConfig.DB.User,
rootConfig.DB.Password,
rootConfig.DB.Socket))
if err != nil {
logger.Fatal("db-initialize", err, lager.Data{
"dbSocket": rootConfig.DB.Socket,
"dbUser": rootConfig.DB.User,
})
} else {
logger.Info("db-initialize", lager.Data{
"dbSocket": rootConfig.DB.Socket,
"dbUser": rootConfig.DB.User,
})
}
mysqldCmd := mysqld_cmd.NewMysqldCmd(logger, *rootConfig)
serviceManager := &node_manager.NodeManager{
ServiceName: rootConfig.Monit.ServiceName,
StateFilePath: rootConfig.Monit.MysqlStateFilePath,
MonitClient: monit_client.NewClient(
net.JoinHostPort(rootConfig.Monit.Host, rootConfig.Monit.Port),
rootConfig.Monit.User,
rootConfig.Monit.Password,
2*time.Minute,
),
GaleraInitAddress: rootConfig.Monit.GaleraInitStatusServerAddress,
Logger: logger,
}
healthchecker := healthcheck.New(db, *rootConfig, logger)
sequenceNumberchecker := sequence_number.New(db, mysqldCmd, *rootConfig, logger)
stateSnapshotter := &healthcheck.DBStateSnapshotter{
DB: db,
Logger: logger,
}
router, err := api.NewRouter(
logger,
rootConfig,
serviceManager,
sequenceNumberchecker,
healthchecker,
healthchecker,
stateSnapshotter,
)
if err != nil {
logger.Fatal("Failed to create router", err)
}
address := fmt.Sprintf("%s:%d", rootConfig.Host, rootConfig.Port)
l, err := net.Listen("tcp", address)
if err != nil {
logger.Fatal("tcp-listen", err, lager.Data{
"address": address,
})
}
url := fmt.Sprintf("http://%s/", address)
logger.Info("Serving healthcheck endpoint", lager.Data{
"url": url,
})
if err := http.Serve(l, router); err != nil {
logger.Fatal("http-server", err)
}
logger.Info("graceful-exit")
}