nacos-viper-remote/config_remote_provider.go

45 lines
1009 B
Go
Raw Permalink Normal View History

2021-05-12 13:34:53 +08:00
package nacos_viper_remote
import (
"fmt"
"github.com/spf13/viper"
)
type ViperRemoteProvider struct {
2021-05-12 13:56:58 +08:00
configType string
configSet string
2021-05-12 13:34:53 +08:00
}
2021-05-12 13:56:58 +08:00
func NewRemoteProvider(configType string) *ViperRemoteProvider {
2021-05-12 13:34:53 +08:00
return &ViperRemoteProvider{
2021-05-12 13:56:58 +08:00
configType: configType,
configSet: "yoyogo.cloud.discovery.metadata"}
2021-05-12 13:34:53 +08:00
}
func (provider *ViperRemoteProvider) GetProvider(runtime_viper *viper.Viper) *viper.Viper {
var option *Option
2021-05-12 13:56:58 +08:00
err := runtime_viper.Sub(provider.configSet).Unmarshal(&option)
2021-05-12 13:34:53 +08:00
if err != nil {
panic(err)
return nil
}
SetOptions(option)
remote_viper := viper.New()
err = remote_viper.AddRemoteProvider("nacos", "localhost", "")
2021-05-12 13:56:58 +08:00
if provider.configType == "" {
provider.configType = "yaml"
}
remote_viper.SetConfigType(provider.configType)
2021-05-12 13:34:53 +08:00
err = remote_viper.ReadRemoteConfig()
if err == nil {
err = remote_viper.WatchRemoteConfigOnChannel()
if err == nil {
fmt.Println("used remote viper")
return remote_viper
}
} else {
panic(err)
}
return runtime_viper
}