fix config reloading

This commit is contained in:
Adphi 2020-11-21 13:21:22 +01:00
parent 280af3fb4c
commit 033f5ef862

39
main.go
View File

@ -13,6 +13,22 @@ var (
modules = Modules{} modules = Modules{}
) )
func loadConfig() error {
conf := &Config{}
if err := viper.Unmarshal(conf); err != nil {
return err
}
mu.Lock()
modules = conf.Modules
mu.Unlock()
go func() {
if err := modules.LoadReadme(); err != nil {
logrus.WithError(err).Error("failed to load all readme")
}
}()
return nil
}
func main() { func main() {
var address string var address string
var level string var level string
@ -30,34 +46,15 @@ func main() {
if err := viper.ReadInConfig(); err != nil { if err := viper.ReadInConfig(); err != nil {
logrus.Fatal(err) logrus.Fatal(err)
} }
conf := &Config{} if err := loadConfig(); err != nil {
if err := viper.Unmarshal(conf); err != nil {
mu.Unlock()
logrus.Fatal(err) logrus.Fatal(err)
} }
mu.Lock()
modules = conf.Modules
mu.Unlock()
go func() {
if err := modules.LoadReadme(); err != nil {
logrus.WithError(err).Error("failed to load all readme")
}
}()
viper.WatchConfig() viper.WatchConfig()
viper.OnConfigChange(func(event fsnotify.Event) { viper.OnConfigChange(func(event fsnotify.Event) {
mu.Lock() if err := loadConfig(); err != nil {
var err error
modules, err = NewModules(event.Name)
if err != nil {
logrus.Fatal(err) logrus.Fatal(err)
} }
mu.Unlock()
logrus.Info("modules reloaded") logrus.Info("modules reloaded")
go func() {
if err := modules.LoadReadme(); err != nil {
logrus.WithError(err).Error("failed to load all readme")
}
}()
}) })
http.HandleFunc("/", modulesHandler) http.HandleFunc("/", modulesHandler)