-
Notifications
You must be signed in to change notification settings - Fork 11
/
init.go
55 lines (48 loc) · 1.08 KB
/
init.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
package service
import (
"log"
"os"
"strings"
"github.com/coreos/go-etcd/etcd"
)
var (
logger *log.Logger
clientSingleton *etcd.Client
hostname string
)
func Client() *etcd.Client {
if clientSingleton == nil {
host := "http://localhost:4001"
if len(os.Getenv("ETCD_HOST")) != 0 {
host = os.Getenv("ETCD_HOST")
}
cacert := os.Getenv("ETCD_CACERT")
tlskey := os.Getenv("ETCD_TLS_KEY")
tlscert := os.Getenv("ETCD_TLS_CERT")
if len(cacert) != 0 && len(tlskey) != 0 && len(tlscert) != 0 {
if !strings.Contains(host, "https://") {
host = strings.Replace(host, "http", "https", 1)
}
c, err := etcd.NewTLSClient([]string{host}, tlscert, tlskey, cacert)
if err != nil {
panic(err)
}
clientSingleton = c
} else {
clientSingleton = etcd.NewClient([]string{host})
}
}
return clientSingleton
}
func init() {
if len(os.Getenv("HOSTNAME")) != 0 {
hostname = os.Getenv("HOSTNAME")
} else {
h, err := os.Hostname()
if err != nil {
panic(err)
}
hostname = h
}
logger = log.New(os.Stderr, "[etcd-discovery]", log.LstdFlags)
}