From 8c2f3313035fc6d2da7a512354386b130c26d48c Mon Sep 17 00:00:00 2001 From: michael1011 Date: Fri, 30 Mar 2018 15:17:23 +0200 Subject: [PATCH] changed default data directory as stated in #1 --- config.go | 112 +++++++++++++++++++++++++++++++-------- lightningtip.go | 7 +-- log.go | 9 ++-- sample-lightningTip.conf | 10 ++++ 4 files changed, 107 insertions(+), 31 deletions(-) diff --git a/config.go b/config.go index ee56895..4d4cb62 100644 --- a/config.go +++ b/config.go @@ -1,9 +1,11 @@ package main import ( + "fmt" "github.com/jessevdk/go-flags" "github.com/michael1011/lightningtip/backends" "github.com/op/go-logging" + "os" "os/user" "path" "runtime" @@ -13,12 +15,14 @@ import ( const ( defaultConfigFile = "lightningTip.conf" + defaultDataDir = "LightningTip" + defaultLogFile = "lightningTip.log" - defaultLogLevel = "debug" + defaultLogLevel = "info" defaultRESTHost = "0.0.0.0:8081" - defaultTlsCertFile = "" - defaultTlsKeyFile = "" + defaultTLSCertFile = "" + defaultTLSKeyFile = "" defaultAccessDomain = "" @@ -32,12 +36,14 @@ const ( 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"` + 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"` @@ -51,17 +57,17 @@ var cfg config var backend backends.Backend func initConfig() { - lndDir := getDefaultLndDir() - cfg = config{ - ConfigFile: defaultConfigFile, + ConfigFile: path.Join(getDefaultDataDir(), defaultConfigFile), - LogFile: defaultLogFile, + DataDir: getDefaultDataDir(), + + LogFile: path.Join(getDefaultDataDir(), defaultLogFile), LogLevel: defaultLogLevel, RESTHost: defaultRESTHost, - TlsCertFile: defaultTlsCertFile, - TlsKeyFile: defaultTlsKeyFile, + TLSCertFile: defaultTLSCertFile, + TLSKeyFile: defaultTLSKeyFile, AccessDomain: defaultAccessDomain, @@ -69,8 +75,8 @@ func initConfig() { LND: &backends.LND{ GRPCHost: defaultLndGRPCHost, - CertFile: path.Join(lndDir, defaultLndCertFile), - MacaroonFile: path.Join(lndDir, defaultMacaroonFile), + CertFile: path.Join(getDefaultLndDir(), defaultLndCertFile), + MacaroonFile: path.Join(getDefaultLndDir(), defaultMacaroonFile), }, } @@ -82,7 +88,7 @@ func initConfig() { // Parse flags again to override config file _, err := flags.Parse(&cfg) - // Default log level + // Default log level if parsing fails logLevel := logging.DEBUG switch strings.ToLower(cfg.LogLevel) { @@ -96,36 +102,98 @@ func initConfig() { logLevel = logging.ERROR } - initLogFile(cfg.LogFile, logLevel) + // 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.Infof("Failed to parse config file: %v", errFile) + 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) } // 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 "windows": - dir = path.Join(usr.HomeDir, "AppData/Local/Lnd") - case "darwin": - dir = path.Join(usr.HomeDir, "Library/Application Support/Lnd/tls.cert") + dir = path.Join(usr.HomeDir, "Library/Application Support") + + case "windows": + dir = path.Join(usr.HomeDir, "AppData/Local") default: - dir = path.Join(usr.HomeDir, ".lnd") + dir = usr.HomeDir } } return dir -} \ No newline at end of file +} diff --git a/lightningtip.go b/lightningtip.go index dda8196..b1a2e52 100644 --- a/lightningtip.go +++ b/lightningtip.go @@ -54,6 +54,7 @@ type errorResponse struct { Error string } +// TODO: add setup guide for systemd // TODO: add option to show URI of Lightning node func main() { initLog() @@ -121,15 +122,15 @@ func main() { go func() { var err error - if cfg.TlsCertFile != "" && cfg.TlsKeyFile != "" { - err = http.ListenAndServeTLS(cfg.RESTHost, cfg.TlsCertFile, cfg.TlsKeyFile, nil) + if cfg.TLSCertFile != "" && cfg.TLSKeyFile != "" { + err = http.ListenAndServeTLS(cfg.RESTHost, cfg.TLSCertFile, cfg.TLSKeyFile, nil) } else { err = http.ListenAndServe(cfg.RESTHost, nil) } if err != nil { - log.Error("Failed to start HTTP server: " + fmt.Sprint(err)) + log.Errorf("Failed to start HTTP server: " + fmt.Sprint(err)) os.Exit(1) } diff --git a/log.go b/log.go index e383cfa..095aa96 100644 --- a/log.go +++ b/log.go @@ -1,7 +1,6 @@ package main import ( - "fmt" "github.com/op/go-logging" "os" ) @@ -17,13 +16,11 @@ func initLog() { logging.SetBackend(backendConsole) } -func initLogFile(logFile string, level logging.Level) { +func initLogger(logFile string, level logging.Level) error { file, err := os.OpenFile(logFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0600) if err != nil { - log.Error("Failed to initialize log file: " + fmt.Sprint(err)) - - return + return err } backendFile := logging.NewLogBackend(file, "", 0) @@ -36,5 +33,5 @@ func initLogFile(logFile string, level logging.Level) { logging.SetBackend(backendConsoleLeveled, backendFileLeveled) - log.Debug("Successfully initialized log file") + return nil } diff --git a/sample-lightningTip.conf b/sample-lightningTip.conf index 03e5fbf..d502248 100644 --- a/sample-lightningTip.conf +++ b/sample-lightningTip.conf @@ -1,4 +1,14 @@ [Application Options] +# Directory for all data stored by LightningTip (config and log file) +# Gets overwritten by individual settings and flags like: "logfile" and "config" +# +# Defaults values: +# Darwin (macOS): /Users//Library/Application Support/LightningTip +# Linux: /home//.lightningtip +# Windows: C:\Users\\AppData\Local\LightningTip +# +# datadir = + # Location of the log file # logfile = lightningTip.log