-
Notifications
You must be signed in to change notification settings - Fork 124
/
client.go
83 lines (69 loc) · 2.04 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
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
//
// Last.Backend LLC CONFIDENTIAL
// __________________
//
// [2014] - [2018] Last.Backend LLC
// All Rights Reserved.
//
// NOTICE: All information contained herein is, and remains
// the property of Last.Backend LLC and its suppliers,
// if any. The intellectual and technical concepts contained
// herein are proprietary to Last.Backend LLC
// and its suppliers and may be covered by Russian Federation and Foreign Patents,
// patents in process, and are protected by trade secret or copyright law.
// Dissemination of this information or reproduction of this material
// is strictly forbidden unless prior written permission is obtained
// from Last.Backend LLC.
//
package v3
import (
"crypto/tls"
"github.com/coreos/etcd/clientv3"
"github.com/coreos/etcd/pkg/transport"
s "github.com/lastbackend/lastbackend/pkg/storage/etcd/store"
"github.com/lastbackend/lastbackend/pkg/util/serializer"
"github.com/lastbackend/lastbackend/pkg/util/serializer/json"
"path"
"time"
)
func GetClient(conf *Config) (s.Store, s.DestroyFunc, error) {
if conf == nil {
panic("need to set the storage configuration")
}
tlsConfig, err := getTLSConfig(conf.TLS.Cert, conf.TLS.Key, conf.TLS.CA)
if err != nil {
return nil, nil, err
}
cfg := clientv3.Config{
Endpoints: conf.Endpoints,
TLS: tlsConfig,
DialTimeout: 5 * time.Second,
}
client, err := clientv3.New(cfg)
if err != nil {
return nil, nil, err
}
destroyFunc := func() {
//client.Close()
}
codec := serializer.NewSerializer(json.Encoder{}, json.Decoder{})
var st = &dbstore{
client: client,
codec: codec,
pathPrefix: path.Join("/", conf.Prefix),
watcher: newWatcher(client),
}
st.opts = append(st.opts, clientv3.WithSerializable())
return st, destroyFunc, nil
}
func getTLSConfig(certFile, keyFile, caFile string) (*tls.Config, error) {
if len(certFile) == 0 || len(keyFile) == 0 || len(caFile) == 0 {
return nil, nil
}
tlsInfo := transport.TLSInfo{
CertFile: certFile,
KeyFile: keyFile,
TrustedCAFile: caFile,
}
return tlsInfo.ClientConfig()
}