This repository has been archived by the owner on Dec 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
client.go
56 lines (48 loc) · 1.85 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package config
import (
"errors"
"fmt"
"github.com/go-mesh/openlogging"
)
var configClientPlugins = make(map[string]func(options Options) (Client, error))
//const
const (
LabelService = "serviceName"
LabelVersion = "version"
LabelEnvironment = "environment"
LabelApp = "app"
)
//DefaultClient is config server's client
var DefaultClient Client
//InstallConfigClientPlugin install a config client plugin
func InstallConfigClientPlugin(name string, f func(options Options) (Client, error)) {
configClientPlugins[name] = f
openlogging.GetLogger().Infof("Installed %s Plugin", name)
}
//Client is the interface of config server client, it has basic func to interact with config server
type Client interface {
//PullConfigs pull all configs from remote
PullConfigs(labels ...map[string]string) (map[string]interface{}, error)
//PullConfig pull one config from remote
PullConfig(key, contentType string, labels map[string]string) (interface{}, error)
// PushConfigs push config to cc
PushConfigs(data map[string]interface{}, labels map[string]string) (map[string]interface{}, error)
// DeleteConfigsByKeys delete config for cc by keys
DeleteConfigsByKeys(keys []string, labels map[string]string) (map[string]interface{}, error)
//Watch get kv change results, you can compare them with local kv cache and refresh local cache
Watch(f func(map[string]interface{}), errHandler func(err error), labels map[string]string) error
Options() Options
}
//NewClient create config client implementation
func NewClient(name string, options Options) (Client, error) {
plugins := configClientPlugins[name]
if plugins == nil {
return nil, errors.New(fmt.Sprintf("plugin [%s] not found", name))
}
DefaultClient, err := plugins(options)
if err != nil {
return nil, err
}
openlogging.GetLogger().Infof("%s plugin is enabled", name)
return DefaultClient, nil
}