Add file to storeconfig
This commit is contained in:
parent
0f3e42d463
commit
7ddefcef72
4 changed files with 119 additions and 36 deletions
|
@ -28,7 +28,7 @@ type Provider struct {
|
|||
// Provide allows the file provider to provide configurations to traefik
|
||||
// using the given configuration channel.
|
||||
func (p *Provider) Provide(configurationChan chan<- types.ConfigMessage, pool *safe.Pool, constraints types.Constraints) error {
|
||||
configuration, err := p.loadConfig()
|
||||
configuration, err := p.LoadConfig()
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -52,6 +52,15 @@ func (p *Provider) Provide(configurationChan chan<- types.ConfigMessage, pool *s
|
|||
return nil
|
||||
}
|
||||
|
||||
// LoadConfig loads configuration either from file or a directory specified by 'Filename'/'Directory'
|
||||
// and returns a 'Configuration' object
|
||||
func (p *Provider) LoadConfig() (*types.Configuration, error) {
|
||||
if p.Directory != "" {
|
||||
return loadFileConfigFromDirectory(p.Directory, nil)
|
||||
}
|
||||
return loadFileConfig(p.Filename)
|
||||
}
|
||||
|
||||
func (p *Provider) addWatcher(pool *safe.Pool, directory string, configurationChan chan<- types.ConfigMessage, callback func(chan<- types.ConfigMessage, fsnotify.Event)) error {
|
||||
watcher, err := fsnotify.NewWatcher()
|
||||
if err != nil {
|
||||
|
@ -88,6 +97,27 @@ func (p *Provider) addWatcher(pool *safe.Pool, directory string, configurationCh
|
|||
return nil
|
||||
}
|
||||
|
||||
func (p *Provider) watcherCallback(configurationChan chan<- types.ConfigMessage, event fsnotify.Event) {
|
||||
watchItem := p.Filename
|
||||
if p.Directory != "" {
|
||||
watchItem = p.Directory
|
||||
}
|
||||
|
||||
if _, err := os.Stat(watchItem); err != nil {
|
||||
log.Debugf("Unable to watch %s : %v", watchItem, err)
|
||||
return
|
||||
}
|
||||
|
||||
configuration, err := p.LoadConfig()
|
||||
|
||||
if err != nil {
|
||||
log.Errorf("Error occurred during watcher callback: %s", err)
|
||||
return
|
||||
}
|
||||
|
||||
sendConfigToChannel(configurationChan, configuration)
|
||||
}
|
||||
|
||||
func sendConfigToChannel(configurationChan chan<- types.ConfigMessage, configuration *types.Configuration) {
|
||||
configurationChan <- types.ConfigMessage{
|
||||
ProviderName: "file",
|
||||
|
@ -168,32 +198,3 @@ func loadFileConfigFromDirectory(directory string, configuration *types.Configur
|
|||
}
|
||||
return configuration, nil
|
||||
}
|
||||
|
||||
func (p *Provider) watcherCallback(configurationChan chan<- types.ConfigMessage, event fsnotify.Event) {
|
||||
watchItem := p.Filename
|
||||
if p.Directory != "" {
|
||||
watchItem = p.Directory
|
||||
}
|
||||
|
||||
if _, err := os.Stat(watchItem); err != nil {
|
||||
log.Debugf("Unable to watch %s : %v", watchItem, err)
|
||||
return
|
||||
}
|
||||
|
||||
configuration, err := p.loadConfig()
|
||||
|
||||
if err != nil {
|
||||
log.Errorf("Error occurred during watcher callback: %s", err)
|
||||
return
|
||||
}
|
||||
|
||||
sendConfigToChannel(configurationChan, configuration)
|
||||
}
|
||||
|
||||
func (p *Provider) loadConfig() (*types.Configuration, error) {
|
||||
if p.Directory != "" {
|
||||
return loadFileConfigFromDirectory(p.Directory, nil)
|
||||
}
|
||||
|
||||
return loadFileConfig(p.Filename)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue