diff --git a/cmd/server.go b/cmd/server.go index ed6183e..af4bbe3 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -1,6 +1,7 @@ package cmd import ( + "context" "encoding/json" "fmt" "github.com/spf13/cobra" @@ -21,7 +22,7 @@ var ( func must(err error) { if err != nil { - fmt.Fprint(os.Stderr, err) + _, _ = fmt.Fprint(os.Stderr, err) os.Exit(1) } } @@ -34,6 +35,9 @@ func NewStartCmd() *cobra.Command { cfg *config.StreamFuncCaptureDBConfig err error ) + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + configFileFlag, err := cmd.Flags().GetString("c") if err != nil { fmt.Println("get local config err: ", err) @@ -42,6 +46,7 @@ func NewStartCmd() *cobra.Command { must(err) cfg, err = config.ParseConfigByFile(configFileFlag) must(err) + logger := LoadLoggerConfig(cfg.Logging) //连接数据库 model.New(cfg.Db.DriveName, cfg.Db.Conn) @@ -61,7 +66,20 @@ func NewStartCmd() *cobra.Command { _ = sf.SetHandler(handler) // start _ = sf.Connect() - select {} + for { + select { + case <-ctx.Done(): + logger.With( + zap.String("web", "exit"), + ).Error(ctx.Err().Error()) + return + case errs := <-exitChannel: + logger.With( + zap.String("web", "服务退出"), + ).Info(errs.String()) + return + } + } }, } cmd.Flags().StringVar(&ConfigFileFlag, "c", "./config/config.yaml", "The configuration file path")