mirror of
https://github.com/spf13/viper
synced 2024-12-23 03:57:01 +00:00
feat: support callback when remote config changed
This commit is contained in:
parent
0e82215118
commit
aeda052906
1 changed files with 12 additions and 0 deletions
12
viper.go
12
viper.go
|
@ -224,6 +224,8 @@ type Viper struct {
|
||||||
// TODO: should probably be protected with a mutex
|
// TODO: should probably be protected with a mutex
|
||||||
encoderRegistry *encoding.EncoderRegistry
|
encoderRegistry *encoding.EncoderRegistry
|
||||||
decoderRegistry *encoding.DecoderRegistry
|
decoderRegistry *encoding.DecoderRegistry
|
||||||
|
|
||||||
|
onRemoteConfigChange func()
|
||||||
}
|
}
|
||||||
|
|
||||||
// New returns an initialized Viper instance.
|
// New returns an initialized Viper instance.
|
||||||
|
@ -1959,6 +1961,12 @@ func mergeMaps(src, tgt map[string]any, itgt map[any]any) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func OnRemoteConfigChange(run func()) { v.OnRemoteConfigChange(run) }
|
||||||
|
|
||||||
|
func (v *Viper) OnRemoteConfigChange(run func()) {
|
||||||
|
v.onRemoteConfigChange = run
|
||||||
|
}
|
||||||
|
|
||||||
// ReadRemoteConfig attempts to get configuration from a remote source
|
// ReadRemoteConfig attempts to get configuration from a remote source
|
||||||
// and read it in the remote configuration registry.
|
// and read it in the remote configuration registry.
|
||||||
func ReadRemoteConfig() error { return v.ReadRemoteConfig() }
|
func ReadRemoteConfig() error { return v.ReadRemoteConfig() }
|
||||||
|
@ -2024,6 +2032,10 @@ func (v *Viper) watchKeyValueConfigOnChannel() error {
|
||||||
b := <-rc
|
b := <-rc
|
||||||
reader := bytes.NewReader(b.Value)
|
reader := bytes.NewReader(b.Value)
|
||||||
v.unmarshalReader(reader, v.kvstore)
|
v.unmarshalReader(reader, v.kvstore)
|
||||||
|
|
||||||
|
if v.onRemoteConfigChange != nil {
|
||||||
|
v.onRemoteConfigChange()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}(respc)
|
}(respc)
|
||||||
return nil
|
return nil
|
||||||
|
|
Loading…
Reference in a new issue