From 263b6df05507edcc2e4b7557ff162d3be52da6b5 Mon Sep 17 00:00:00 2001 From: yedf2 <120050102@qq.com> Date: Tue, 12 Apr 2022 16:54:38 +0800 Subject: [PATCH] refactor main --- .gitignore | 2 - dtmsvr/entry/main.go | 60 ++++++++++++++++++++++++++++++ dtmsvr/microservices/drivers.go | 9 +++++ main.go | 65 ++------------------------------- 4 files changed, 73 insertions(+), 63 deletions(-) create mode 100644 dtmsvr/entry/main.go create mode 100644 dtmsvr/microservices/drivers.go diff --git a/.gitignore b/.gitignore index 3235773..5dd9d0a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,8 +1,6 @@ conf.yml *.out *.log -*/**/main -main dist .idea/** .vscode diff --git a/dtmsvr/entry/main.go b/dtmsvr/entry/main.go new file mode 100644 index 0000000..b324344 --- /dev/null +++ b/dtmsvr/entry/main.go @@ -0,0 +1,60 @@ +package entry + +import ( + "flag" + "fmt" + "os" + "path/filepath" + + "github.com/dtm-labs/dtm/dtmcli/logger" + "github.com/dtm-labs/dtm/dtmsvr" + "github.com/dtm-labs/dtm/dtmsvr/config" + "github.com/dtm-labs/dtm/dtmsvr/storage/registry" + "go.uber.org/automaxprocs/maxprocs" +) + +func ver(version *string) { + if *version == "" { + *version = "0.0.0-dev" + } + fmt.Printf("dtm version: %s\n", *version) +} + +func usage() { + cmd := filepath.Base(os.Args[0]) + s := "Usage: %s [options]\n\n" + fmt.Fprintf(os.Stderr, s, cmd) + flag.PrintDefaults() +} + +var isVersion = flag.Bool("v", false, "Show the version of dtm.") +var isDebug = flag.Bool("d", false, "Set log level to debug.") +var isHelp = flag.Bool("h", false, "Show the help information about dtm.") +var isReset = flag.Bool("r", false, "Reset dtm server data.") +var confFile = flag.String("c", "", "Path to the server configuration file.") + +func Main(version *string) { + flag.Parse() + if flag.NArg() > 0 || *isHelp { + usage() + return + } else if *isVersion { + ver(version) + return + } + logger.Infof("dtm version is: %s", *version) + config.MustLoadConfig(*confFile) + conf := &config.Config + if *isDebug { + conf.LogLevel = "debug" + } + logger.InitLog2(conf.LogLevel, conf.Log.Outputs, conf.Log.RotationEnable, conf.Log.RotationConfigJSON) + if *isReset { + dtmsvr.PopulateDB(false) + } + _, _ = maxprocs.Set(maxprocs.Logger(logger.Infof)) + registry.WaitStoreUp() + dtmsvr.StartSvr() // start dtmsvr api + go dtmsvr.CronExpiredTrans(-1) // start dtmsvr cron job + select {} +} diff --git a/dtmsvr/microservices/drivers.go b/dtmsvr/microservices/drivers.go new file mode 100644 index 0000000..f74bc72 --- /dev/null +++ b/dtmsvr/microservices/drivers.go @@ -0,0 +1,9 @@ +package microservices + +// load the microserver driver +import ( + _ "github.com/dtm-labs/dtmdriver-gozero" + _ "github.com/dtm-labs/dtmdriver-kratos" + _ "github.com/dtm-labs/dtmdriver-polaris" + _ "github.com/dtm-labs/dtmdriver-protocol1" +) diff --git a/main.go b/main.go index dd5c791..78b539e 100644 --- a/main.go +++ b/main.go @@ -7,70 +7,13 @@ package main import ( - "flag" - "fmt" - "os" - "path/filepath" - - "go.uber.org/automaxprocs/maxprocs" - - "github.com/dtm-labs/dtm/dtmcli/logger" - "github.com/dtm-labs/dtm/dtmsvr" - "github.com/dtm-labs/dtm/dtmsvr/config" - "github.com/dtm-labs/dtm/dtmsvr/storage/registry" - - // load the microserver driver - _ "github.com/dtm-labs/dtmdriver-gozero" - _ "github.com/dtm-labs/dtmdriver-kratos" - _ "github.com/dtm-labs/dtmdriver-polaris" - _ "github.com/dtm-labs/dtmdriver-protocol1" + "github.com/dtm-labs/dtm/dtmsvr/entry" + _ "github.com/dtm-labs/dtm/dtmsvr/microservices" ) -// Version declares version info +// Version defines version info. It is set by -ldflags. var Version string -func version() { - if Version == "" { - Version = "0.0.0-dev" - } - fmt.Printf("dtm version: %s\n", Version) -} - -func usage() { - cmd := filepath.Base(os.Args[0]) - s := "Usage: %s [options]\n\n" - fmt.Fprintf(os.Stderr, s, cmd) - flag.PrintDefaults() -} - -var isVersion = flag.Bool("v", false, "Show the version of dtm.") -var isDebug = flag.Bool("d", false, "Set log level to debug.") -var isHelp = flag.Bool("h", false, "Show the help information about dtm.") -var isReset = flag.Bool("r", false, "Reset dtm server data.") -var confFile = flag.String("c", "", "Path to the server configuration file.") - func main() { - flag.Parse() - if flag.NArg() > 0 || *isHelp { - usage() - return - } else if *isVersion { - version() - return - } - logger.Infof("dtm version is: %s", Version) - config.MustLoadConfig(*confFile) - conf := &config.Config - if *isDebug { - conf.LogLevel = "debug" - } - logger.InitLog2(conf.LogLevel, conf.Log.Outputs, conf.Log.RotationEnable, conf.Log.RotationConfigJSON) - if *isReset { - dtmsvr.PopulateDB(false) - } - _, _ = maxprocs.Set(maxprocs.Logger(logger.Infof)) - registry.WaitStoreUp() - dtmsvr.StartSvr() // start dtmsvr api - go dtmsvr.CronExpiredTrans(-1) // start dtmsvr cron job - select {} + entry.Main(&Version) }