data2minio/cmd/server.go

103 lines
2.7 KiB
Go
Raw Permalink Normal View History

2023-04-01 16:43:13 +08:00
package cmd
import (
"context"
2023-04-02 23:35:12 +08:00
"data_minio/minio"
"data_minio/mq"
2023-04-01 16:43:13 +08:00
"fmt"
"github.com/spf13/cobra"
"os"
"os/signal"
"syscall"
"go.uber.org/zap"
"data_minio/config"
"data_minio/global"
"data_minio/model"
)
var (
ConfigFileFlag string = "./config/config.yaml"
ConsulAddress string = "http://localhost:8500"
NodeName string = "data_minio"
Mode string = "dev"
)
func NewStartCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "start",
Short: "Start folder monitor",
Run: func(cmd *cobra.Command, args []string) {
var (
cfg *config.DataMinioConfig
err error
)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
global.Must(err)
configFileFlag, err := cmd.Flags().GetString("c")
if err != nil {
fmt.Println("get local config err: ", err)
return
}
NodeName, err = cmd.Flags().GetString("n")
if err != nil {
fmt.Println("get remote path config err: ", err)
return
}
Mode, err = cmd.Flags().GetString("m")
if err != nil {
fmt.Println("get remote path config err: ", err)
return
}
if len(configFileFlag) > 1 {
cfg, err = config.ParseConfigByFile(configFileFlag)
global.Must(err)
err = config.UpdateRemoteConfig(cfg)
global.Must(err)
ConfigFileFlag = configFileFlag
} else {
//获取consul注册中心的配置文件
cfg, err = config.GetRemoteConfig(ConsulAddress, fmt.Sprintf("hpds-pavement/hpds_web/%s/%s", Mode, NodeName))
global.Must(err)
err = config.UpdateLocalConfig(cfg, ConfigFileFlag)
}
global.Cfg = cfg
global.Logger = config.LoadLoggerConfig(cfg.Logging)
2023-04-02 23:35:12 +08:00
global.MinioClient, err = minio.NewMinioClient(cfg.Minio.Endpoint, cfg.Minio.AccessKeyId, cfg.Minio.SecretAccessKey)
2023-04-01 16:43:13 +08:00
global.Must(err)
//连接数据库
global.DB = model.New(cfg.Db.DriveName, cfg.Db.Conn, cfg.Mode == "dev")
2023-04-02 23:35:12 +08:00
//创建消息连接点
mq.MqList, err = mq.NewMqClient(cfg.Funcs, cfg.Node)
global.Must(err)
2023-04-01 16:43:13 +08:00
// 退出channel
exitChannel := make(chan os.Signal)
defer close(exitChannel)
// 退出信号监听
go func(c chan os.Signal) {
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
}(exitChannel)
select {
case <-ctx.Done():
global.Logger.With(
zap.String(cfg.Name, "exit"),
).Error(ctx.Err().Error())
return
case errs := <-exitChannel:
global.Logger.With(
zap.String(cfg.Name, "服务退出"),
).Info(errs.String())
os.Exit(-1)
return
}
},
}
cmd.Flags().StringVar(&ConfigFileFlag, "c", "./config/config.yaml", "The configuration file path")
cmd.Flags().StringVar(&NodeName, "n", "data_minio", "The configuration name")
cmd.Flags().StringVar(&Mode, "m", "dev", "run mode : dev | test | releases")
return cmd
}