-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
60 lines (51 loc) · 1.23 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
package config
import (
"crypto/tls"
"crypto/x509"
"io/ioutil"
"log"
"github.com/go-ini/ini"
consulapi "github.com/hashicorp/consul/api"
)
// CliConfig 服务配置
type CliConfig struct {
ServerName string
ConsulConf *consulapi.Config
TlsConf *tls.Config
}
// CliInit 服务配置初始化
// section 服务实例节点名称
func CliInit(section string) *CliConfig {
cli := &CliConfig{}
cfg, err := ini.Load("./conf/config.ini")
if err != nil {
log.Fatalln(err)
}
if len(section) == 0 {
section = "client"
}
cfgSec := cfg.Section(section)
certFile := cfgSec.Key("grpc_tls_certfile").String()
if certFile != "" {
b, err := ioutil.ReadFile(certFile)
if err != nil {
log.Fatalln(err)
}
cp := x509.NewCertPool()
if !cp.AppendCertsFromPEM(b) {
log.Fatalln("credentials: failed to append certificates")
}
cli.TlsConf = &tls.Config{
ServerName: cfgSec.Key("srv_name").String(),
RootCAs: cp,
}
}
consulCfg := consulapi.DefaultConfig()
consulCfg.Address = cfgSec.Key("consul_addrs").String()
if cfgSec.Key("consul_acl_token").String() != "" {
consulCfg.Token = cfgSec.Key("consul_acl_token").String()
}
cli.ConsulConf = consulCfg
cli.ServerName = cfgSec.Key("srv_name").String()
return cli
}