-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
94 lines (83 loc) · 1.87 KB
/
util.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
package cron
import (
"encoding/json"
"io/ioutil"
"os"
"time"
"github.com/go-redis/redis/v8"
"github.com/sirupsen/logrus"
)
var Logger *logrus.Logger
func init() {
formatter := new(logrus.TextFormatter)
formatter.TimestampFormat = time.RFC3339
formatter.FullTimestamp = true
Logger = &logrus.Logger{
Out: os.Stdout,
Formatter: formatter,
Level: logrus.InfoLevel,
}
}
type Conf struct {
Base struct {
HttpAddr string `json:"http_addr"`
RedisOptions redis.Options `json:"redis"`
} `json:"base"`
Gossip struct {
Network string `json:"network"`
NodeName string `json:"node_name"`
BindAddr string `json:"bind_addr"`
BindPort int `json:"bind_port"`
} `json:"gossip"`
Custom struct {
KeyTimeline string `json:"key_timeline"`
KeyEntry string `json:"key_entry"`
KeyExecutor string `json:"key_executor"`
MaxHistoryNum int64 `json:"max_history_num"`
} `json:"custom"`
}
func ReadConfig(conf *Conf, file string) {
defer conf.WithDefault()
data, err := ioutil.ReadFile(file)
if err != nil {
Logger.Warn("ReadConfig failed: ", err.Error())
return
}
if err = json.Unmarshal(data, conf); err != nil {
Logger.Warn("ReadConfig failed: ", err.Error())
return
}
}
func (c *Conf) WithDefault() {
// base
if c.Base.HttpAddr == "" {
c.Base.HttpAddr = ":8080"
}
// gossip
if c.Gossip.Network == "" {
c.Gossip.Network = "LAN"
}
if c.Gossip.BindAddr == "" {
c.Gossip.BindAddr = "0.0.0.0"
}
if c.Gossip.BindPort == 0 {
c.Gossip.BindPort = 7946
}
if c.Gossip.NodeName == "" {
hostname, _ := os.Hostname()
c.Gossip.NodeName = hostname
}
// custom
if c.Custom.KeyEntry == "" {
c.Custom.KeyEntry = "_entry"
}
if c.Custom.KeyTimeline == "" {
c.Custom.KeyTimeline = "_timeline"
}
if c.Custom.KeyExecutor == "" {
c.Custom.KeyExecutor = "_exe"
}
if c.Custom.MaxHistoryNum == 0 {
c.Custom.MaxHistoryNum = 5
}
}