-
Notifications
You must be signed in to change notification settings - Fork 42
/
config.go
215 lines (190 loc) · 7.08 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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package config
import (
"flag"
"fmt"
"os"
"time"
"github.com/lbryio/chainquery/daemon/processing"
"github.com/lbryio/chainquery/auth"
"github.com/lbryio/chainquery/daemon"
"github.com/lbryio/chainquery/global"
"github.com/lbryio/chainquery/lbrycrd"
"github.com/lbryio/chainquery/twilio"
"github.com/lbryio/lbry.go/extras/errors"
"github.com/fsnotify/fsnotify"
"github.com/go-ini/ini"
"github.com/johntdyer/slackrus"
"github.com/sirupsen/logrus"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"github.com/volatiletech/sqlboiler/boil"
)
const ( // config setting keys
debugmode = "debugmode"
debugquerymode = "debugquerymode"
mysqldsn = "mysqldsn"
apimysqldsn = "apimysqldsn"
lbrycrdurl = "lbrycrdurl"
profilemode = "profilemode"
daemonmode = "daemonmode"
processingdelay = "processingdelay"
daemondelay = "daemondelay"
defaultclienttimeout = "defaultclienttimeout"
daemonprofile = "daemonprofile"
lbrycrdprofile = "lbrycrdprofile"
mysqlprofile = "mysqlprofile"
apihostport = "apihostport"
slackhookurl = "slackhookurl"
slackchannel = "slackchannel"
slackloglevel = "slackloglevel"
autoupdatecommand = "autoupdatecommand"
twiliosid = "twiliosid"
twilioauthtoken = "twilioauthtoken"
smsrecipients = "smsrecipients"
smsfromphonenumber = "smsfromphonenumber"
apikeys = "apikeys"
maxfailures = "maxfailures"
blockchainname = "blockchainname"
)
const (
//Chainquery Flags
configpathflag = "configpath"
reindexflag = "reindex"
reindexwipeflag = "reindexwipe"
)
// InitializeConfiguration is the main entry point from outside the package. This initializes the configuration and watcher.
func InitializeConfiguration() {
initDefaults()
initFlags()
readConfig()
processConfiguration()
viper.WatchConfig()
viper.OnConfigChange(func(e fsnotify.Event) {
fmt.Println("Config file changed:", e.Name)
readConfig()
processConfiguration()
})
}
// InitSlack initializes the slack connection and posts info level or greater to the set channel.
func InitSlack() {
slackURL := viper.GetString(slackhookurl)
slackChannel := viper.GetString(slackchannel)
slackLogLevel := viper.GetInt(slackloglevel)
if slackURL != "" && slackChannel != "" {
logrus.AddHook(&slackrus.SlackrusHook{
HookURL: slackURL,
AcceptedLevels: slackrus.LevelThreshold(logrus.Level(slackLogLevel)),
Channel: slackChannel,
IconEmoji: ":golang:",
Username: "Chainquery",
})
}
}
func initFlags() {
// using standard library "flag" package
pflag.BoolP(reindexflag, "r", false, "Rebuilds the database from the 1st block. Does not wipe the database")
pflag.BoolP(reindexwipeflag, "w", false, "Drops all tables and rebuilds the database from the 1st block.")
pflag.StringP(configpathflag, "c", "", "Specify non-default location of the configuration of chainquery. The precedence is $HOME, working directory, and lastly the branch path to the default configuration 'path/to/chainquery/config/default/'")
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
pflag.Parse()
if err := viper.BindPFlags(pflag.CommandLine); err != nil {
logrus.Panic(err)
}
}
func readConfig() {
viper.SetConfigName("chainqueryconfig") // name of config file (without extension)
viper.AddConfigPath(viper.GetString(configpathflag)) // 1 - commandline config path
viper.AddConfigPath("$HOME/") // 2 - check $HOME
viper.AddConfigPath(".") // 3 - optionally look for config in the working directory
viper.AddConfigPath("./config/default/") // 4 - use default that comes with the branch
err := viper.ReadInConfig() // Find and read the config file
if err != nil { // Handle errors reading the config file
logrus.Warning("Error reading config file...defaults will be used: ", err)
}
twilio.RecipientList = viper.GetStringSlice(smsrecipients)
twilio.FromNumber = viper.GetString(smsfromphonenumber)
twilio.TwilioAuthToken = viper.GetString(twilioauthtoken)
twilio.TwilioSID = viper.GetString(twiliosid)
}
func initDefaults() {
//Setting viper defaults in the event there are not settings set in the config file.
viper.SetDefault(debugmode, false)
viper.SetDefault(debugquerymode, false)
viper.SetDefault(mysqldsn, "lbry:lbry@tcp(localhost:3306)/chainquery")
viper.SetDefault(apimysqldsn, "lbry:lbry@tcp(localhost:3306)/chainquery")
viper.SetDefault(lbrycrdurl, "rpc://lbry:lbry@localhost:9245")
viper.SetDefault(profilemode, false)
viper.SetDefault(daemonmode, 0) //BEASTMODE
viper.SetDefault(defaultclienttimeout, 20*time.Second)
viper.SetDefault(daemondelay, 1) //Seconds
viper.SetDefault(processingdelay, 100) //Milliseconds
viper.SetDefault(daemonprofile, false)
viper.SetDefault(lbrycrdprofile, false)
viper.SetDefault(mysqlprofile, false)
viper.SetDefault("codeprofile", false)
viper.SetDefault(apihostport, "0.0.0.0:6300")
viper.SetDefault(slackloglevel, 0)
viper.SetDefault(smsrecipients, []string{})
viper.SetDefault(smsfromphonenumber, "")
viper.SetDefault(maxfailures, 1000)
viper.SetDefault(blockchainname, "lbrycrd_main")
}
func processConfiguration() {
// Things that listen live for setting changes that need to be applied. Settings that are retrieved do not need
// to be set here.
isdebug := viper.GetBool(debugmode)
if isdebug {
logrus.Info("SETTINGS: debug mode turned on")
logrus.SetLevel(logrus.DebugLevel)
} else {
logrus.SetLevel(logrus.InfoLevel)
}
if boil.DebugMode != viper.GetBool(debugquerymode) {
logrus.Info("SETTINGS: changing query debug mode to ", viper.GetBool(debugquerymode))
boil.DebugMode = viper.GetBool(debugquerymode)
}
settings := global.DaemonSettings{
DaemonMode: GetDaemonMode(),
ProcessingDelay: GetProcessingDelay(),
DaemonDelay: GetDaemonDelay(),
IsReIndex: viper.GetBool(reindexflag)}
daemon.ApplySettings(settings)
lbrycrd.LBRYcrdURL = GetLBRYcrdURL()
auth.APIKeys = viper.GetStringSlice(apikeys)
processing.MaxFailures = viper.GetInt(maxfailures)
global.BlockChainName = viper.GetString(blockchainname)
}
func getLbrycrdURLFromConfFile() (string, error) {
if os.Getenv("HOME") == "" {
return "", errors.Err("$HOME env var not set")
}
defaultConfFile := os.Getenv("HOME") + "/.lbrycrd/lbrycrd.conf"
if _, err := os.Stat(defaultConfFile); os.IsNotExist(err) {
return "", errors.Err("lbrycrd conf file not found")
}
cfg, err := ini.Load(defaultConfFile)
if err != nil {
return "", errors.Err(err)
}
section, err := cfg.GetSection("")
if err != nil {
return "", errors.Err(err)
}
username := section.Key("rpcuser").String()
password := section.Key("rpcpassword").String()
host := section.Key("rpchost").String()
if host == "" {
host = "localhost"
}
port := section.Key("rpcport").String()
if port == "" {
port = ":9245"
} else {
port = ":" + port
}
userpass := ""
if username != "" || password != "" {
userpass = username + ":" + password + "@"
}
return "rpc://" + userpass + host + port, nil
}