/
consul.go
157 lines (127 loc) · 2.91 KB
/
consul.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Package consul is a consul implementation of kv
package consul
import (
"fmt"
"log"
"net"
"path/filepath"
"github.com/hashicorp/consul/api"
"github.com/micro/go-micro/v2/store"
)
type ckv struct {
options store.Options
client *api.Client
}
func (c *ckv) Init(opts ...store.Option) error {
for _, o := range opts {
o(&c.options)
}
return c.configure()
}
func (c *ckv) Options() store.Options {
return c.options
}
func (c *ckv) Read(key string, opts ...store.ReadOption) ([]*store.Record, error) {
// TODO: implement read options
records := make([]*store.Record, 0, 1)
keyval, _, err := c.client.KV().Get(key, nil)
if err != nil {
return nil, err
}
if keyval == nil {
return nil, store.ErrNotFound
}
records = append(records, &store.Record{
Key: keyval.Key,
Value: keyval.Value,
})
return records, nil
}
func (c *ckv) Close() error {
// consul does not support close?
return nil
}
func (c *ckv) Delete(key string, opts ...store.DeleteOption) error {
options := store.DeleteOptions{}
options.Table = c.options.Table
for _, o := range opts {
o(&options)
}
_, err := c.client.KV().Delete(filepath.Join(options.Table, key), nil)
return err
}
func (c *ckv) Write(record *store.Record, opts ...store.WriteOption) error {
options := store.WriteOptions{}
options.Table = c.options.Table
for _, o := range opts {
o(&options)
}
_, err := c.client.KV().Put(&api.KVPair{
Key: filepath.Join(options.Table, record.Key),
Value: record.Value,
}, nil)
return err
}
func (c *ckv) List(opts ...store.ListOption) ([]string, error) {
options := store.ListOptions{}
options.Table = c.options.Table
for _, o := range opts {
o(&options)
}
if options.Table == "" {
options.Table = "/"
} else if options.Table[len(options.Table)-1] != '/' {
options.Table += "/"
}
keyval, _, err := c.client.KV().List(options.Table, nil)
if err != nil {
return nil, err
}
if keyval == nil {
return nil, store.ErrNotFound
}
var keys []string
for _, keyv := range keyval {
keys = append(keys, keyv.Key)
}
return keys, nil
}
func (c *ckv) String() string {
return "consul"
}
func NewStore(opts ...store.Option) store.Store {
var options store.Options
for _, o := range opts {
o(&options)
}
// new store
s := new(ckv)
// set the options
s.options = options
// configure the store
if err := s.configure(); err != nil {
log.Fatal(err)
}
// return store
return s
}
func (ckv *ckv) configure() error {
config := api.DefaultConfig()
nodes := ckv.options.Nodes
// set host
if len(nodes) > 0 {
addr, port, err := net.SplitHostPort(nodes[0])
if ae, ok := err.(*net.AddrError); ok && ae.Err == "missing port in address" {
port = "8500"
config.Address = fmt.Sprintf("%s:%s", nodes[0], port)
} else if err == nil {
config.Address = fmt.Sprintf("%s:%s", addr, port)
}
}
client, err := api.NewClient(config)
if err != nil {
return err
}
ckv.client = client
return nil
}