lightningtip/config.go

122 lines
2.5 KiB
Go
Raw Normal View History

2018-03-22 13:52:16 -07:00
package main
import (
"github.com/jessevdk/go-flags"
2018-03-23 16:08:03 -07:00
"github.com/michael1011/lightningtip/backends"
2018-03-22 13:52:16 -07:00
"github.com/op/go-logging"
2018-03-23 16:08:03 -07:00
"os/user"
"path"
"runtime"
2018-03-22 13:52:16 -07:00
"strings"
)
const (
defaultConfigFile = "lightningTip.conf"
defaultLogFile = "lightningTip.log"
defaultLogLevel = "debug"
2018-03-23 16:08:03 -07:00
defaultRESTHost = "localhost:8081"
defaultAccessDomain = ""
2018-03-24 15:03:55 -07:00
2018-03-25 08:45:42 -07:00
defaultTipExpiry = 3600
2018-03-24 15:03:55 -07:00
2018-03-29 06:01:28 -07:00
defaultLndGRPCHost = "localhost:10009"
2018-03-23 16:08:03 -07:00
defaultLndCertFile = "tls.cert"
defaultMacaroonFile = "admin.macaroon"
2018-03-22 13:52:16 -07:00
)
type config struct {
2018-03-29 06:01:28 -07:00
ConfigFile string `long:"config" Description:"Location of the config file"`
2018-03-22 13:52:16 -07:00
2018-03-29 06:01:28 -07:00
LogFile string `long:"logfile" Description:"Location of the log file"`
2018-03-22 13:52:16 -07:00
LogLevel string `long:"loglevel" Description:"Log level: debug, info, warning, error"`
2018-03-23 16:08:03 -07:00
2018-03-29 06:01:28 -07:00
RESTHost string `long:"resthost" Description:"Host for the REST interface of LightningTip"`
AccessDomain string `long:"accessdomain" Description:"The domain you are using LightningTip from"`
2018-03-24 15:03:55 -07:00
TipExpiry int64 `long:"tipexpiry" Description:"Invoice expiry time in seconds"`
2018-03-24 15:03:55 -07:00
2018-03-23 16:08:03 -07:00
LND *backends.LND `group:"LND" namespace:"lnd"`
2018-03-22 13:52:16 -07:00
}
var cfg config
2018-03-23 16:08:03 -07:00
var backend backends.Backend
2018-03-22 13:52:16 -07:00
func initConfig() {
2018-03-23 16:08:03 -07:00
lndDir := getDefaultLndDir()
2018-03-22 13:52:16 -07:00
cfg = config{
ConfigFile: defaultConfigFile,
LogFile: defaultLogFile,
LogLevel: defaultLogLevel,
2018-03-23 16:08:03 -07:00
RESTHost: defaultRESTHost,
AccessDomain: defaultAccessDomain,
2018-03-24 15:03:55 -07:00
2018-03-25 08:45:42 -07:00
TipExpiry: defaultTipExpiry,
2018-03-24 15:03:55 -07:00
2018-03-23 16:08:03 -07:00
LND: &backends.LND{
2018-03-29 06:01:28 -07:00
GRPCHost: defaultLndGRPCHost,
2018-03-23 16:08:03 -07:00
CertFile: path.Join(lndDir, defaultLndCertFile),
MacaroonFile: path.Join(lndDir, defaultMacaroonFile),
},
2018-03-22 13:52:16 -07:00
}
_, err := flags.Parse(&cfg)
errFile := flags.IniParse(cfg.ConfigFile, &cfg)
// Parse flags again to override config file
_, err = flags.Parse(&cfg)
// Default log level
logLevel := logging.DEBUG
switch strings.ToLower(cfg.LogLevel) {
case "info":
logLevel = logging.INFO
case "warning":
logLevel = logging.WARNING
case "error":
logLevel = logging.ERROR
}
initLogFile(cfg.LogFile, logLevel)
if err != nil {
log.Error("Failed to parse command line flags")
}
if errFile != nil {
2018-03-23 16:08:03 -07:00
log.Infof("Failed to parse config file: %v", errFile)
}
2018-03-24 15:03:55 -07:00
// TODO: add more backend options like for example c-lighting and eclair
2018-03-23 16:08:03 -07:00
backend = cfg.LND
}
func getDefaultLndDir() (dir string) {
usr, err := user.Current()
if err == nil {
switch runtime.GOOS {
case "windows":
dir = path.Join(usr.HomeDir, "AppData/Local/Lnd")
case "darwin":
dir = path.Join(usr.HomeDir, "Library/Application Support/Lnd/tls.cert")
default:
dir = path.Join(usr.HomeDir, ".lnd")
}
2018-03-22 13:52:16 -07:00
}
2018-03-23 16:08:03 -07:00
return dir
2018-03-22 13:52:16 -07:00
}