-
Notifications
You must be signed in to change notification settings - Fork 34
/
node.go
79 lines (66 loc) · 1.86 KB
/
node.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
package teaconfigs
import (
"github.com/TeaWeb/code/teaconfigs/shared"
"github.com/go-yaml/yaml"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/files"
"github.com/iwind/TeaGo/logs"
)
// 节点配置文件名
var nodeConfigFile = "node.conf"
var sharedNodeConfig *NodeConfig = nil
// 节点配置
type NodeConfig struct {
Id string `yaml:"id" json:"id"` // ID
On bool `yaml:"on" json:"on"` // 是否启用
Name string `yaml:"name" json:"name"` // 名称
ClusterId string `yaml:"clusterId" json:"clusterId"` // 集群ID
ClusterSecret string `yaml:"clusterSecret" json:"clusterSecret"` // 集群秘钥
ClusterAddr string `yaml:"clusterAddr" json:"clusterAddr"` // 集群通讯地址
Role NodeRole `yaml:"role" json:"role"` // 角色
}
// 取得当前节点配置
// 如果为nil,表示尚未配置集群
func SharedNodeConfig() *NodeConfig {
shared.Locker.Lock()
defer shared.Locker.ReadUnlock()
if sharedNodeConfig != nil {
return sharedNodeConfig
}
configFile := files.NewFile(Tea.ConfigFile(nodeConfigFile))
if !configFile.Exists() {
return nil
}
data, err := configFile.ReadAll()
if err != nil {
logs.Error(err)
return nil
}
config := &NodeConfig{}
err = yaml.Unmarshal(data, config)
if err != nil {
return nil
}
sharedNodeConfig = config
return config
}
// 保存到文件
func (this *NodeConfig) Save() error {
shared.Locker.Lock()
defer shared.Locker.WriteUnlock()
data, err := yaml.Marshal(this)
if err != nil {
return err
}
configFile := files.NewFile(Tea.ConfigFile(nodeConfigFile))
err = configFile.Write(data)
if err != nil {
return err
}
sharedNodeConfig = nil
return nil
}
// 是否为Master
func (this *NodeConfig) IsMaster() bool {
return this.Role == NodeRoleMaster
}