Skip to content

Instantly share code, notes, and snippets.

@emil-palm
Created October 5, 2016 09:19
Show Gist options
  • Save emil-palm/a32afeb58701453437a957af2d0c88ac to your computer and use it in GitHub Desktop.
Save emil-palm/a32afeb58701453437a957af2d0c88ac to your computer and use it in GitHub Desktop.
func setupAndRunViper(path, format string, viperCh) {
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, syscall.SIGHUP)
cnt := 0
for {
v := viper.New()
v.SetConfigFile(path)
v.SetConfigType(format)
var err error
if err = v.ReadInConfig(); err != nil {
goto ERROR
}
if cnt == 0 {
log.WithFields(log.Fields{
"Topic": "Config",
}).Info("Finished reading the config file")
}
cnt++
viperCh <- v
goto NEXT
ERROR:
if cnt == 0 {
log.WithFields(log.Fields{
"Topic": "Config",
"Error": err,
}).Fatalf("Can't read config file %s", path)
} else {
log.WithFields(log.Fields{
"Topic": "Config",
"Error": err,
}).Warningf("Can't read config file %s", path)
}
NEXT:
select {
case <-sigCh:
log.WithFields(log.Fields{
"Topic": "Config",
}).Info("Reload the config file")
}
}
}
func ReadConfigfileServeExtensive(path, format string, configCh chan *BgpConfigSet) {
viperCh := make(chan *viper.Viper)
go setupAndRunViper(path, format, viperCh)
select {
case viper <- viperCh:
c := &BgpConfigSet{}
if err = v.UnmarshalExact(c); err != nil {
log.WithFields(log.Fields{
"Topic": "Config",
"Error": err,
}).Warningf("Can't read config file %s", path)
}
if err = setDefaultConfigValuesWithViper(v, c); err != nil {
goto ERROR
}
configCh <- c
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment