-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.go
44 lines (39 loc) · 884 Bytes
/
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
package chord
import (
"crypto/sha1"
"encoding/json"
"fmt"
"hash"
"io/ioutil"
"log"
)
// Config represents configuration for a Chord node
type Config struct {
Host string `json:"Host"`
HashFunc hash.Hash
HashBits int `json:"NumBits"`
NumNodes int `json:"NumNodes"`
}
// InitConfig initializes configuration from conf file
func InitConfig(confPath string) (*Config, error) {
bytes, err := ioutil.ReadFile(confPath)
log.Println(string(bytes))
if err != nil {
return nil, fmt.Errorf("init config failed: %s", err)
}
config := Config{}
err = json.Unmarshal(bytes, &config)
if err != nil {
return nil, fmt.Errorf("init config failed: %s", err)
}
return &config, nil
}
// DefaultConfig initializes a default configuration
func DefaultConfig(host string) *Config {
return &Config{
Host: host,
HashFunc: sha1.New(),
HashBits: 3,
NumNodes: 8,
}
}