|
|
|
@ -47,27 +47,33 @@ var (
|
|
|
|
|
cfgPath = flag.String("c", defaultCfgPath, "Path to the configuration file")
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func initCfg() error {
|
|
|
|
|
data, err := os.ReadFile(*cfgPath)
|
|
|
|
|
func initCfg(path string) (*config.GlobalConfig, error) {
|
|
|
|
|
data, err := os.ReadFile(path)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
return nil, errs.Wrap(err, "ReadFile unmarshal failed")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return yaml.Unmarshal(data, &config.Config)
|
|
|
|
|
conf := config.NewGlobalConfig()
|
|
|
|
|
err = yaml.Unmarshal(data, &conf)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, errs.Wrap(err, "InitConfig unmarshal failed")
|
|
|
|
|
}
|
|
|
|
|
return conf, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type checkFunc struct {
|
|
|
|
|
name string
|
|
|
|
|
function func() error
|
|
|
|
|
function func(*config.GlobalConfig) error
|
|
|
|
|
flag bool
|
|
|
|
|
config *config.GlobalConfig
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
|
|
if err := initCfg(); err != nil {
|
|
|
|
|
conf, err := initCfg(defaultCfgPath)
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Printf("Read config failed: %v\n", err)
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -75,11 +81,11 @@ func main() {
|
|
|
|
|
|
|
|
|
|
checks := []checkFunc{
|
|
|
|
|
//{name: "Mysql", function: checkMysql},
|
|
|
|
|
{name: "Mongo", function: checkMongo},
|
|
|
|
|
{name: "Redis", function: checkRedis},
|
|
|
|
|
{name: "Minio", function: checkMinio},
|
|
|
|
|
{name: "Zookeeper", function: checkZookeeper},
|
|
|
|
|
{name: "Kafka", function: checkKafka},
|
|
|
|
|
{name: "Mongo", function: checkMongo, config: conf},
|
|
|
|
|
{name: "Redis", function: checkRedis, config: conf},
|
|
|
|
|
{name: "Minio", function: checkMinio, config: conf},
|
|
|
|
|
{name: "Zookeeper", function: checkZookeeper, config: conf},
|
|
|
|
|
{name: "Kafka", function: checkKafka, config: conf},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for i := 0; i < maxRetry; i++ {
|
|
|
|
@ -92,7 +98,7 @@ func main() {
|
|
|
|
|
allSuccess := true
|
|
|
|
|
for index, check := range checks {
|
|
|
|
|
if !check.flag {
|
|
|
|
|
err = check.function()
|
|
|
|
|
err = check.function(check.config)
|
|
|
|
|
if err != nil {
|
|
|
|
|
component.ErrorPrint(fmt.Sprintf("Starting %s failed:%v.", check.name, err))
|
|
|
|
|
allSuccess = false
|
|
|
|
@ -112,30 +118,30 @@ func main() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// checkMongo checks the MongoDB connection without retries
|
|
|
|
|
func checkMongo() error {
|
|
|
|
|
_, err := unrelation.NewMongo()
|
|
|
|
|
func checkMongo(config *config.GlobalConfig) error {
|
|
|
|
|
_, err := unrelation.NewMongo(config)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// checkRedis checks the Redis connection
|
|
|
|
|
func checkRedis() error {
|
|
|
|
|
_, err := cache.NewRedis()
|
|
|
|
|
func checkRedis(config *config.GlobalConfig) error {
|
|
|
|
|
_, err := cache.NewRedis(config)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// checkMinio checks the MinIO connection
|
|
|
|
|
func checkMinio() error {
|
|
|
|
|
func checkMinio(config *config.GlobalConfig) error {
|
|
|
|
|
|
|
|
|
|
// Check if MinIO is enabled
|
|
|
|
|
if config.Config.Object.Enable != "minio" {
|
|
|
|
|
if config.Object.Enable != "minio" {
|
|
|
|
|
return errs.Wrap(errors.New("minio.Enable is empty"))
|
|
|
|
|
}
|
|
|
|
|
minio := &component.Minio{
|
|
|
|
|
ApiURL: config.Config.Object.ApiURL,
|
|
|
|
|
Endpoint: config.Config.Object.Minio.Endpoint,
|
|
|
|
|
AccessKeyID: config.Config.Object.Minio.AccessKeyID,
|
|
|
|
|
SecretAccessKey: config.Config.Object.Minio.SecretAccessKey,
|
|
|
|
|
SignEndpoint: config.Config.Object.Minio.SignEndpoint,
|
|
|
|
|
ApiURL: config.Object.ApiURL,
|
|
|
|
|
Endpoint: config.Object.Minio.Endpoint,
|
|
|
|
|
AccessKeyID: config.Object.Minio.AccessKeyID,
|
|
|
|
|
SecretAccessKey: config.Object.Minio.SecretAccessKey,
|
|
|
|
|
SignEndpoint: config.Object.Minio.SignEndpoint,
|
|
|
|
|
UseSSL: getEnv("MINIO_USE_SSL", "false"),
|
|
|
|
|
}
|
|
|
|
|
err := component.CheckMinio(minio)
|
|
|
|
@ -143,18 +149,18 @@ func checkMinio() error {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// checkZookeeper checks the Zookeeper connection
|
|
|
|
|
func checkZookeeper() error {
|
|
|
|
|
_, err := zookeeper.NewZookeeperDiscoveryRegister()
|
|
|
|
|
func checkZookeeper(config *config.GlobalConfig) error {
|
|
|
|
|
_, err := zookeeper.NewZookeeperDiscoveryRegister(config)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// checkKafka checks the Kafka connection
|
|
|
|
|
func checkKafka() error {
|
|
|
|
|
func checkKafka(config *config.GlobalConfig) error {
|
|
|
|
|
// Prioritize environment variables
|
|
|
|
|
kafkaStu := &component.Kafka{
|
|
|
|
|
Username: config.Config.Kafka.Username,
|
|
|
|
|
Password: config.Config.Kafka.Password,
|
|
|
|
|
Addr: config.Config.Kafka.Addr,
|
|
|
|
|
Username: config.Kafka.Username,
|
|
|
|
|
Password: config.Kafka.Password,
|
|
|
|
|
Addr: config.Kafka.Addr,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
kafkaClient, err := component.CheckKafka(kafkaStu)
|
|
|
|
@ -170,9 +176,9 @@ func checkKafka() error {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
requiredTopics := []string{
|
|
|
|
|
config.Config.Kafka.MsgToMongo.Topic,
|
|
|
|
|
config.Config.Kafka.MsgToPush.Topic,
|
|
|
|
|
config.Config.Kafka.LatestMsgToRedis.Topic,
|
|
|
|
|
config.Kafka.MsgToMongo.Topic,
|
|
|
|
|
config.Kafka.MsgToPush.Topic,
|
|
|
|
|
config.Kafka.LatestMsgToRedis.Topic,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, requiredTopic := range requiredTopics {
|
|
|
|
@ -181,11 +187,22 @@ func checkKafka() error {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tlsConfig := &kafka.TLSConfig{
|
|
|
|
|
CACrt: config.Kafka.TLS.CACrt,
|
|
|
|
|
ClientCrt: config.Kafka.TLS.ClientCrt,
|
|
|
|
|
ClientKey: config.Kafka.TLS.ClientKey,
|
|
|
|
|
ClientKeyPwd: config.Kafka.TLS.ClientKeyPwd,
|
|
|
|
|
InsecureSkipVerify: config.Kafka.TLS.InsecureSkipVerify,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_, err = kafka.NewMConsumerGroup(&kafka.MConsumerGroupConfig{
|
|
|
|
|
KafkaVersion: sarama.V2_0_0_0,
|
|
|
|
|
OffsetsInitial: sarama.OffsetNewest, IsReturnErr: false,
|
|
|
|
|
}, []string{config.Config.Kafka.LatestMsgToRedis.Topic},
|
|
|
|
|
config.Config.Kafka.Addr, config.Config.Kafka.ConsumerGroupID.MsgToRedis)
|
|
|
|
|
OffsetsInitial: sarama.OffsetNewest,
|
|
|
|
|
IsReturnErr: false,
|
|
|
|
|
UserName: config.Kafka.Username,
|
|
|
|
|
Password: config.Kafka.Password,
|
|
|
|
|
}, []string{config.Kafka.LatestMsgToRedis.Topic},
|
|
|
|
|
config.Kafka.Addr, config.Kafka.ConsumerGroupID.MsgToRedis, tlsConfig)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
@ -193,8 +210,8 @@ func checkKafka() error {
|
|
|
|
|
_, err = kafka.NewMConsumerGroup(&kafka.MConsumerGroupConfig{
|
|
|
|
|
KafkaVersion: sarama.V2_0_0_0,
|
|
|
|
|
OffsetsInitial: sarama.OffsetNewest, IsReturnErr: false,
|
|
|
|
|
}, []string{config.Config.Kafka.MsgToPush.Topic},
|
|
|
|
|
config.Config.Kafka.Addr, config.Config.Kafka.ConsumerGroupID.MsgToMongo)
|
|
|
|
|
}, []string{config.Kafka.MsgToPush.Topic},
|
|
|
|
|
config.Kafka.Addr, config.Kafka.ConsumerGroupID.MsgToMongo, tlsConfig)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
@ -202,8 +219,8 @@ func checkKafka() error {
|
|
|
|
|
kafka.NewMConsumerGroup(&kafka.MConsumerGroupConfig{
|
|
|
|
|
KafkaVersion: sarama.V2_0_0_0,
|
|
|
|
|
OffsetsInitial: sarama.OffsetNewest, IsReturnErr: false,
|
|
|
|
|
}, []string{config.Config.Kafka.MsgToPush.Topic}, config.Config.Kafka.Addr,
|
|
|
|
|
config.Config.Kafka.ConsumerGroupID.MsgToPush)
|
|
|
|
|
}, []string{config.Kafka.MsgToPush.Topic}, config.Kafka.Addr,
|
|
|
|
|
config.Kafka.ConsumerGroupID.MsgToPush, tlsConfig)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|