-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathconfig_storage.go
37 lines (31 loc) · 992 Bytes
/
config_storage.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
package service_decorators
import "github.com/hashicorp/consul/api"
// ConfigStorage is to store the configuration
type ConfigStorage interface {
Get(name string) ([]byte, error)
}
// ConsulConfigStorage the configuration storage with Consul KV
type ConsulConfigStorage struct {
client *api.KV
}
// CreateConsulConfigStorage is to create a ConsulConfigStorage
func CreateConsulConfigStorage(consulConfig *api.Config) (*ConsulConfigStorage, error) {
client, err := api.NewClient(consulConfig)
if err != nil {
return nil, err
}
return &ConsulConfigStorage{client.KV()}, nil
}
// Get is to get the configuration
func (storage *ConsulConfigStorage) Get(name string) ([]byte, error) {
pair, _, err := storage.client.Get(name, nil)
if err != nil {
return nil, err
}
return pair.Value, nil
}
// Set is to set the configuration
func (storage *ConsulConfigStorage) Set(name string, value []byte) error {
storage.client.Put(&api.KVPair{Key: name, Value: value}, nil)
return nil
}