-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
67 lines (55 loc) · 1.34 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
package main
import (
"encoding/json"
"errors"
"io/ioutil"
"log"
"net/rpc"
"sync"
)
// NodeInfo for node config
type NodeInfo struct {
CandidateID string `json:"candidateID"`
Endpoint string `json:"endpoint"`
URL string `json:"url"`
Client *rpc.Client
}
// Config for nodes in cluster
type Config struct {
mu sync.Mutex
Nodes []NodeInfo `json:"nodes"`
}
// LoadConfig reads config file and returns Config object
func LoadConfig() *Config {
config := new(Config)
config.mu.Lock()
defer config.mu.Unlock()
content, err := ioutil.ReadFile("./cluster_config.json")
if err != nil {
log.Fatal("Error when opening file: ", err)
}
err = json.Unmarshal(content, &config)
if err != nil {
log.Fatal("Error during Unmarshal(): ", err)
}
// remove this server from our list of nodes
config.removeNode(server.candidateID)
return config
}
func (config *Config) getNode(candidateID string) (*NodeInfo, error) {
for _, node := range config.Nodes {
if node.CandidateID == candidateID {
return &node, nil
}
}
return nil, errors.New("Node not found")
}
func (config *Config) removeNode(candidateID string) error {
for i, node := range config.Nodes {
if node.CandidateID == server.candidateID {
config.Nodes[i] = config.Nodes[len(config.Nodes)-1]
config.Nodes = config.Nodes[:len(config.Nodes)-1]
}
}
return nil
}