-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add Config.ReloadFiles for reload has been load config files
- add new event: reload.data - add example use fsnotify watch files
- Loading branch information
Showing
5 changed files
with
216 additions
and
41 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/fsnotify/fsnotify" | ||
"github.com/gookit/config/v2" | ||
"github.com/gookit/config/v2/yamlv3" | ||
"github.com/gookit/goutil" | ||
"github.com/gookit/goutil/cliutil" | ||
) | ||
|
||
func main() { | ||
config.AddDriver(yamlv3.Driver) | ||
config.WithOptions( | ||
config.ParseEnv, | ||
config.WithHookFunc(func(event string, c *config.Config) { | ||
if event == config.OnReloadData { | ||
cliutil.Cyanln("config reloaded, you can do something ....") | ||
} | ||
}), | ||
) | ||
|
||
// load app config files | ||
err := config.LoadFiles( | ||
"testdata/json_base.json", | ||
"testdata/yml_base.yml", | ||
"testdata/yml_other.yml", | ||
) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// mock server running | ||
done := make(chan bool) | ||
|
||
// watch loaded config files | ||
err = watchConfigFiles(config.Default()) | ||
goutil.PanicErr(err) | ||
|
||
cliutil.Infoln("loaded config files is watching ...") | ||
<-done | ||
} | ||
|
||
func watchConfigFiles(cfg *config.Config) error { | ||
watcher, err := fsnotify.NewWatcher() | ||
if err != nil { | ||
return err | ||
} | ||
//noinspection GoUnhandledErrorResult | ||
defer watcher.Close() | ||
|
||
// get loaded files | ||
files := cfg.LoadedFiles() | ||
if len(files) == 0 { | ||
return nil | ||
} | ||
|
||
go func() { | ||
for { | ||
select { | ||
case event, ok := <-watcher.Events: | ||
if !ok { // 'Events' channel is closed | ||
cliutil.Infoln("'Events' channel is closed ...", event) | ||
return | ||
} | ||
|
||
// if event.Op > 0 { | ||
cliutil.Infof("file event: %s\n", event) | ||
|
||
if event.Op&fsnotify.Write == fsnotify.Write { | ||
cliutil.Infof("modified file: %s\n", event.Name) | ||
|
||
err := cfg.ReloadFiles() | ||
if err != nil { | ||
cliutil.Errorf("reload config error: %s\n", err.Error()) | ||
} | ||
} | ||
// } | ||
|
||
case err, ok := <-watcher.Errors: | ||
if ok { // 'Errors' channel is not closed | ||
cliutil.Errorf("watch file error: %s\n", err.Error()) | ||
} | ||
if err != nil { | ||
cliutil.Errorf("watch file error2: %s\n", err.Error()) | ||
} | ||
return | ||
} | ||
} | ||
}() | ||
|
||
for _, path := range files { | ||
cliutil.Infof("add watch file: %s\n", path) | ||
if err := watcher.Add(path); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} |
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
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
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
Oops, something went wrong.