/
config.go
115 lines (84 loc) · 2.28 KB
/
config.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
package eventcar
import (
"fmt"
"github.com/1-bi/log-api"
"github.com/coreos/etcd/clientv3"
"io/ioutil"
)
// Config
type Config struct {
_etcdConfig clientv3.Config
// nats server connect
_natsHost []string
_natsClusterId string
_natsClientId string
// --- define default value --
nodeIdPath string
nodeRoles []string
nodeNum int64
_agentNodeId string
}
func NewConfig() *Config {
var conf = new(Config)
// --- set default value
conf.nodeIdPath = "/etc/servicebus/agent/node_id"
conf._natsHost = []string{"nats://localhost:4222"}
conf._natsClusterId = "test-cluster"
conf._natsClientId = "test-client"
conf.nodeNum = 1
return conf
}
func (myself *Config) SetNatsHost(hosts []string) {
myself._natsHost = hosts
}
func (myself *Config) SetNatsClusterId(newClusterId string) {
myself._natsClusterId = newClusterId
}
func (myself *Config) SetNatsClientId(newClientId string) {
myself._natsClientId = newClientId
}
func (myself *Config) SetNodeRoles(newRoles []string) {
myself.nodeRoles = newRoles
}
func (myself *Config) CheckBeforeStart() error {
var confErr = myself.checkNodeId()
if confErr != nil {
return confErr
}
return nil
}
func (myself *Config) checkNodeId() error {
// --- check file exist
var existed, err = pathExists(myself.nodeIdPath)
if err != nil {
fmt.Println(err)
}
var nodeId = ""
if !existed {
nodeId = createNodeIdFile(myself.nodeIdPath, myself.nodeNum)
} else {
// --- read node id to file ---
b, err := ioutil.ReadFile(myself.nodeIdPath)
if err != nil {
structBean := logapi.NewStructBean()
structBean.LogString("fileName", myself.nodeIdPath)
logapi.GetLogger("serviebus.Agent.checkNodeId").Debug("Get node id from file. ", structBean)
}
nodeId = string(b)
}
// create the agentNodeId
structBean := logapi.NewStructBean()
structBean.LogString("nodeId", nodeId)
logapi.GetLogger("serviebus.Agent.checkNodeId").Debug("Display the current node id. ", structBean)
myself._agentNodeId = nodeId
return nil
}
// SetRegisterServer define restier servers handle
func (myself *Config) SetRegisterServer(config clientv3.Config) {
myself._etcdConfig = config
}
func (myself *Config) SetNodeIdFilePath(nodeIdFilePath string) {
myself.nodeIdPath = nodeIdFilePath
}
func (myself *Config) SetSettingDir(paths ...string) {
}