Created
October 5, 2016 09:19
-
-
Save emil-palm/a32afeb58701453437a957af2d0c88ac to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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