From a7a908bd83ca48dec79690b555c8c781437bd15a Mon Sep 17 00:00:00 2001 From: Doflatango Date: Fri, 23 Feb 2018 15:31:58 +0800 Subject: [PATCH] Move confFile flag from conf to bin --- bin/node/server.go | 5 +++-- bin/web/server.go | 5 +++-- common.go | 4 ++-- conf/conf.go | 27 +++++++++++---------------- 4 files changed, 19 insertions(+), 22 deletions(-) diff --git a/bin/node/server.go b/bin/node/server.go index 37f0129..2ca3080 100644 --- a/bin/node/server.go +++ b/bin/node/server.go @@ -17,7 +17,8 @@ import ( ) var ( - level = flag.Int("l", 0, "log level, -1:debug, 0:info, 1:warn, 2:error") + level = flag.Int("l", 0, "log level, -1:debug, 0:info, 1:warn, 2:error") + confFile = flag.String("conf", "conf/files/base.json", "config file path") ) func main() { @@ -32,7 +33,7 @@ func main() { } log.SetLogger(logger.Sugar()) - if err := cronsun.Init(); err != nil { + if err = cronsun.Init(*confFile); err != nil { log.Errorf(err.Error()) return } diff --git a/bin/web/server.go b/bin/web/server.go index 794d555..754ec30 100644 --- a/bin/web/server.go +++ b/bin/web/server.go @@ -18,7 +18,8 @@ import ( ) var ( - level = flag.Int("l", 0, "log level, -1:debug, 0:info, 1:warn, 2:error") + level = flag.Int("l", 0, "log level, -1:debug, 0:info, 1:warn, 2:error") + confFile = flag.String("conf", "conf/files/base.json", "config file path") ) func main() { @@ -33,7 +34,7 @@ func main() { } log.SetLogger(logger.Sugar()) - if err := cronsun.Init(); err != nil { + if err = cronsun.Init(*confFile); err != nil { log.Errorf(err.Error()) return } diff --git a/common.go b/common.go index 5156872..9ac7c23 100644 --- a/common.go +++ b/common.go @@ -14,7 +14,7 @@ var ( _Uid int ) -func Init() (err error) { +func Init(baseConfFile string) (err error) { if initialized { return } @@ -25,7 +25,7 @@ func Init() (err error) { } // init config - if err = conf.Init(); err != nil { + if err = conf.Init(baseConfFile); err != nil { return fmt.Errorf("Init Config failed: %s", err) } diff --git a/conf/conf.go b/conf/conf.go index 06db5a7..ede3a9c 100644 --- a/conf/conf.go +++ b/conf/conf.go @@ -1,7 +1,6 @@ package conf import ( - "flag" "path" "time" @@ -16,9 +15,6 @@ import ( ) var ( - confFile = flag.String("conf", - "conf/files/base.json", "config file path") - Config = new(Conf) initialized bool @@ -26,16 +22,15 @@ var ( exitChan = make(chan struct{}) ) -func Init() error { +func Init(confFile string) error { if initialized { return nil } - flag.Parse() - if err := Config.parse(); err != nil { + if err := Config.parse(confFile); err != nil { return err } - if err := Config.watch(); err != nil { + if err := Config.watch(confFile); err != nil { return err } initialized = true @@ -124,8 +119,8 @@ func cleanKeyPrefix(p string) string { return p } -func (c *Conf) parse() error { - err := utils.LoadExtendConf(*confFile, c) +func (c *Conf) parse(confFile string) error { + err := utils.LoadExtendConf(confFile, c) if err != nil { return err } @@ -168,7 +163,7 @@ func (c *Conf) parse() error { return nil } -func (c *Conf) watch() error { +func (c *Conf) watch(confFile string) error { var err error watcher, err = fsnotify.NewWatcher() if err != nil { @@ -190,7 +185,7 @@ func (c *Conf) watch() error { timer.Reset(duration) case <-timer.C: if update { - c.reload() + c.reload(confFile) event.Emit(event.WAIT, nil) update = false } @@ -201,7 +196,7 @@ func (c *Conf) watch() error { } }() - return watcher.Add(*confFile) + return watcher.Add(confFile) } // 重新加载配置项 @@ -209,9 +204,9 @@ func (c *Conf) watch() error { // Etcd // Mgo // Web -func (c *Conf) reload() { +func (c *Conf) reload(confFile string) { cf := new(Conf) - if err := cf.parse(); err != nil { + if err := cf.parse(confFile); err != nil { log.Warnf("config file reload err: %s", err.Error()) return } @@ -220,7 +215,7 @@ func (c *Conf) reload() { cf.Node, cf.Proc, cf.Cmd, cf.Once, cf.Lock, cf.Group, cf.Noticer = c.Node, c.Proc, c.Cmd, c.Once, c.Lock, c.Group, c.Noticer *c = *cf - log.Infof("config file[%s] reload success", *confFile) + log.Infof("config file[%s] reload success", confFile) return }