forked from michael1011/lightningtip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
201 lines (142 loc) · 4.4 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
package main
import (
"fmt"
"github.com/jessevdk/go-flags"
"github.com/michael1011/lightningtip/backends"
"github.com/op/go-logging"
"os"
"os/user"
"path"
"runtime"
"strings"
)
const (
defaultConfigFile = "lightningTip.conf"
defaultDataDir = "LightningTip"
defaultLogFile = "lightningTip.log"
defaultLogLevel = "info"
defaultRESTHost = "0.0.0.0:8081"
defaultTLSCertFile = ""
defaultTLSKeyFile = ""
defaultAccessDomain = ""
defaultTipExpiry = 3600
defaultLndGRPCHost = "localhost:10009"
defaultLndCertFile = "tls.cert"
defaultMacaroonFile = "admin.macaroon"
)
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"`
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"`
LND *backends.LND `group:"LND" namespace:"lnd"`
}
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,
RESTHost: defaultRESTHost,
TLSCertFile: defaultTLSCertFile,
TLSKeyFile: defaultTLSKeyFile,
AccessDomain: defaultAccessDomain,
TipExpiry: defaultTipExpiry,
LND: &backends.LND{
GRPCHost: defaultLndGRPCHost,
CertFile: path.Join(getDefaultLndDir(), defaultLndCertFile),
MacaroonFile: path.Join(getDefaultLndDir(), defaultMacaroonFile),
},
}
// Ignore unknown flags the first time parsing command line flags to prevent showing the unknown flag error twice
flags.NewParser(&cfg, flags.IgnoreUnknown).Parse()
errFile := flags.IniParse(cfg.ConfigFile, &cfg)
// 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)
}
backends.UseLogger(*log)
// TODO: add more backend options like for example c-lighting and eclair
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 dir
}
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 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 dir
}