hpds_control_center/cmd/server.go

155 lines
4.1 KiB
Go

package cmd
import (
"context"
"fmt"
"hpds_control_center/model"
"hpds_control_center/mq"
"os"
"os/signal"
"syscall"
"git.hpds.cc/Component/logging"
"github.com/spf13/cobra"
"go.uber.org/zap"
"hpds_control_center/config"
discover "hpds_control_center/pkg/discover/consul"
)
var (
ConfigFileFlag string = "./config/config.yaml"
ConsulAddress string = "http://localhost:8500"
NodeName string = "main-node"
Mode string = "dev"
)
func must(err error) {
if err != nil {
_, _ = fmt.Fprint(os.Stderr, err)
os.Exit(1)
}
}
func NewStartCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "start",
Short: "Start hpds_web application",
Run: func(cmd *cobra.Command, args []string) {
var (
cfg *config.WebConfig
err error
)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
must(err)
configFileFlag, err := cmd.Flags().GetString("c")
if err != nil {
fmt.Println("get local config err: ", err)
return
}
ConsulAddress, err = cmd.Flags().GetString("r")
if err != nil {
fmt.Println("get remote 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)
must(err)
err = config.UpdateRemoteConfig(cfg)
must(err)
ConfigFileFlag = configFileFlag
} else {
//获取consul注册中心的配置文件
cfg, err = config.GetRemoteConfig(ConsulAddress, fmt.Sprintf("hpds-pavement/hpds_control/%s/%s", Mode, NodeName))
must(err)
err = config.UpdateLocalConfig(cfg, ConfigFileFlag)
}
//创建注册对象
tags := make([]string, 1)
tags[0] = "control"
consulCfg, err := discover.NewConsulConfig(cfg.Consul.Host, cfg.Name, cfg.Name, cfg.Consul.Host, cfg.Consul.Port,
tags, 300, 300, 300)
must(err)
//连接数据库
model.New(cfg.Db.DriveName, cfg.Db.Conn, cfg.Mode == "dev")
logger := LoadLoggerConfig(cfg.Logging)
//创建消息连接点
mq.MqList, err = mq.NewMqClient(cfg.Funcs, cfg.Node, logger)
must(err)
// 退出channel
exitChannel := make(chan os.Signal)
defer close(exitChannel)
// 退出信号监听
go func(c chan os.Signal) {
_ = consulCfg.ServiceDeregister()
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
}(exitChannel)
// start http service
go func() {
//fmt.Printf("Http Server start at port %d \n", cfg.Port)
//启动前执行注册
err = consulCfg.ServiceRegister()
must(err)
}()
//服务退出取消注册
//err = consulCfg.ServiceDeregister()
//
//must(err)
//zap.L().Error("发生错误", zap.Error(err))
for {
select {
case <-ctx.Done():
_ = consulCfg.ServiceDeregister()
logger.With(
zap.String("web", "exit"),
).Error(ctx.Err().Error())
return
case errs := <-exitChannel:
_ = consulCfg.ServiceDeregister()
logger.With(
zap.String("web", "服务退出"),
).Info(errs.String())
return
}
}
},
}
cmd.Flags().StringVar(&ConfigFileFlag, "c", "./config/config.yaml", "The configuration file path")
cmd.Flags().StringVar(&ConsulAddress, "r", "http://consul.hpds.cc", "The configuration remote consul address")
cmd.Flags().StringVar(&NodeName, "n", "main-node", "The configuration name")
cmd.Flags().StringVar(&Mode, "m", "dev", "run mode : dev | test | releases")
return cmd
}
func LoadLoggerConfig(opt config.LogOptions) *logging.Logger {
return logging.NewLogger(
logging.SetPath(opt.Path),
logging.SetPrefix(opt.Prefix),
logging.SetDevelopment(opt.Development),
logging.SetDebugFileSuffix(opt.DebugFileSuffix),
logging.SetWarnFileSuffix(opt.WarnFileSuffix),
logging.SetErrorFileSuffix(opt.ErrorFileSuffix),
logging.SetInfoFileSuffix(opt.InfoFileSuffix),
logging.SetMaxAge(opt.MaxAge),
logging.SetMaxBackups(opt.MaxBackups),
logging.SetMaxSize(opt.MaxSize),
logging.SetLevel(logging.LogLevel["debug"]),
)
}