forked from michael1011/lightningtip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
291 lines (209 loc) · 6.82 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package main
import (
"fmt"
"io/ioutil"
"os"
"os/user"
"path"
"path/filepath"
"runtime"
"strings"
flags "github.com/jessevdk/go-flags"
"github.com/michael1011/lightningtip/backends"
"github.com/michael1011/lightningtip/database"
"github.com/michael1011/lightningtip/notifications"
"github.com/michael1011/lightningtip/version"
logging "github.com/op/go-logging"
)
const (
defaultConfigFile = "lightningTip.conf"
defaultDataDir = "LightningTip"
defaultLogFile = "lightningTip.log"
defaultLogLevel = "info"
defaultDatabaseFile = "tips.db"
defaultRESTHost = "0.0.0.0:8081"
defaultTLSCertFile = ""
defaultTLSKeyFile = ""
defaultAccessDomain = ""
defaultTipExpiry = 3600
defaultReconnectInterval = 0
defaultKeepaliveInterval = 0
defaultLndGRPCHost = "localhost:10009"
defaultLndCertFile = "tls.cert"
defaultMacaroonFile = "invoice.macaroon"
defaultRecipient = ""
defaultSender = ""
defaultSTMPServer = ""
defaultSTMPSSL = false
defaultSTMPUser = ""
defaultSTMPPassword = ""
)
type helpOptions struct {
ShowHelp bool `long:"help" short:"h" description:"Display this help message"`
ShowVersion bool `long:"version" short:"v" description:"Display version and exit"`
}
type config struct {
ConfigFile string `long:"config" description:"Location of the config file"`
DataDir string `long:"datadir" description:"Location of the data stored by LightningTip"`
LogFile string `long:"logfile" description:"Location of the log file"`
LogLevel string `long:"loglevel" description:"Log level: debug, info, warning, error"`
DatabaseFile string `long:"databasefile" description:"Location of the database file to store settled invoices"`
RESTHost string `long:"resthost" description:"Host for the REST interface of LightningTip"`
TLSCertFile string `long:"tlscertfile" description:"Certificate for using LightningTip via HTTPS"`
TLSKeyFile string `long:"tlskeyfile" description:"Certificate for using LightningTip via HTTPS"`
AccessDomain string `long:"accessdomain" description:"The domain you are using LightningTip from"`
TipExpiry int64 `long:"tipexpiry" description:"Invoice expiry time in seconds"`
ReconnectInterval int64 `long:"reconnectinterval" description:"Reconnect interval to LND in seconds"`
KeepAliveInterval int64 `long:"keepaliveinterval" description:"Send a dummy request to LND to prevent timeouts "`
LND *backends.LND `group:"LND" namespace:"lnd"`
Mail *notifications.Mail `group:"Mail" namespace:"mail"`
Help *helpOptions `group:"Help Options"`
}
var cfg config
var backend backends.Backend
func initConfig() {
cfg = config{
ConfigFile: path.Join(getDefaultDataDir(), defaultConfigFile),
DataDir: getDefaultDataDir(),
LogFile: path.Join(getDefaultDataDir(), defaultLogFile),
LogLevel: defaultLogLevel,
DatabaseFile: path.Join(getDefaultDataDir(), defaultDatabaseFile),
RESTHost: defaultRESTHost,
TLSCertFile: defaultTLSCertFile,
TLSKeyFile: defaultTLSKeyFile,
AccessDomain: defaultAccessDomain,
TipExpiry: defaultTipExpiry,
ReconnectInterval: defaultReconnectInterval,
KeepAliveInterval: defaultKeepaliveInterval,
LND: &backends.LND{
GRPCHost: defaultLndGRPCHost,
CertFile: path.Join(getDefaultLndDir(), defaultLndCertFile),
MacaroonFile: getDefaultMacaroon(),
},
Mail: ¬ifications.Mail{
Recipient: defaultRecipient,
Sender: defaultSender,
SMTPServer: defaultSTMPServer,
SMTPSSL: defaultSTMPSSL,
SMTPUser: defaultSTMPUser,
SMTPPassword: defaultSTMPPassword,
},
}
// Ignore unknown flags the first time parsing command line flags to prevent showing the unknown flag error twice
parser := flags.NewParser(&cfg, flags.IgnoreUnknown)
parser.Parse()
errFile := flags.IniParse(cfg.ConfigFile, &cfg)
// If the user just wants to see the version initializing everything else is irrelevant
if cfg.Help.ShowVersion {
version.PrintVersion()
os.Exit(0)
}
// If the user just wants to see the help message
if cfg.Help.ShowHelp {
parser.WriteHelp(os.Stdout)
os.Exit(0)
}
// Parse flags again to override config file
_, err := flags.Parse(&cfg)
// Default log level if parsing fails
logLevel := logging.DEBUG
switch strings.ToLower(cfg.LogLevel) {
case "info":
logLevel = logging.INFO
case "warning":
logLevel = logging.WARNING
case "error":
logLevel = logging.ERROR
}
// Create data directory
var errDataDir error
var dataDirCreated bool
if _, err := os.Stat(getDefaultDataDir()); os.IsNotExist(err) {
errDataDir = os.Mkdir(getDefaultDataDir(), 0700)
dataDirCreated = true
}
errLogFile := initLogger(cfg.LogFile, logLevel)
// Show error messages
if err != nil {
log.Error("Failed to parse command line flags")
}
if errDataDir != nil {
log.Error("Could not create data directory")
log.Debug("Data directory path: " + getDefaultDataDir())
} else if dataDirCreated {
log.Debug("Created data directory: " + getDefaultDataDir())
}
if errFile != nil {
log.Warning("Failed to parse config file: " + fmt.Sprint(errFile))
} else {
log.Debug("Parsed config file: " + cfg.ConfigFile)
}
if errLogFile != nil {
log.Error("Failed to initialize log file: " + fmt.Sprint(err))
} else {
log.Debug("Initialized log file: " + cfg.LogFile)
}
database.UseLogger(*log)
backends.UseLogger(*log)
notifications.UseLogger(*log)
backend = cfg.LND
}
func getDefaultDataDir() (dir string) {
homeDir := getHomeDir()
switch runtime.GOOS {
case "windows":
fallthrough
case "darwin":
dir = path.Join(homeDir, defaultDataDir)
default:
dir = path.Join(homeDir, "."+strings.ToLower(defaultDataDir))
}
return cleanPath(dir)
}
// If the mainnet macaroon does exists it is preffered over all others
func getDefaultMacaroon() string {
networksDir := filepath.Join(getDefaultLndDir(), "/data/chain/bitcoin/")
mainnetMacaroon := filepath.Join(networksDir, "mainnet/", defaultMacaroonFile)
if _, err := os.Stat(mainnetMacaroon); err == nil {
return mainnetMacaroon
}
networks, err := ioutil.ReadDir(networksDir)
if err == nil && len(networks) != 0 {
for _, subDir := range networks {
if subDir.IsDir() {
return filepath.Join(networksDir, networks[0].Name(), defaultMacaroonFile)
}
}
}
return ""
}
func getDefaultLndDir() (dir string) {
homeDir := getHomeDir()
switch runtime.GOOS {
case "darwin":
fallthrough
case "windows":
dir = path.Join(homeDir, "Lnd")
default:
dir = path.Join(homeDir, ".lnd")
}
return cleanPath(dir)
}
func getHomeDir() (dir string) {
usr, err := user.Current()
if err == nil {
switch runtime.GOOS {
case "darwin":
dir = path.Join(usr.HomeDir, "Library/Application Support")
case "windows":
dir = path.Join(usr.HomeDir, "AppData/Local")
default:
dir = usr.HomeDir
}
}
return cleanPath(dir)
}
func cleanPath(path string) string {
path = filepath.Clean(os.ExpandEnv(path))
return strings.Replace(path, "\\", "/", -1)
}