/
config.go
55 lines (51 loc) · 1.07 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
package server
import (
"encoding/json"
"errors"
pb "github.com/PomeloCloud/BFTRaft4go/proto/server"
"github.com/dgraph-io/badger"
"github.com/golang/protobuf/proto"
"io/ioutil"
"log"
)
func GetConfig(kv *badger.DB) (*pb.ServerConfig, error) {
var res *pb.ServerConfig = nil
err := kv.View(func(txn *badger.Txn) error {
if item, err := txn.Get(ComposeKeyPrefix(CONFIG_GROUP, SERVER_CONF)); err != nil {
log.Panic(err)
return err
} else {
data := ItemValue(item)
if data == nil {
log.Panic(err)
return errors.New("no data")
} else {
conf := pb.ServerConfig{}
if err := proto.Unmarshal(*data, &conf); err != nil {
log.Panic(err)
return err
}
res = &conf
return nil
}
}
})
return res, err
}
type FileConfig struct {
Db string
Address string
Bootstraps []string
}
func ReadConfigFile(path string) FileConfig {
data, err := ioutil.ReadFile(path)
if err != nil {
panic(err)
}
log.Println(string(data))
fc := FileConfig{}
if err := json.Unmarshal(data, &fc); err != nil {
panic(err)
}
return fc
}