Skip to content

Commit 5d61fd5

Browse files
authored
Merge pull request #266 from carlaKC/config-dbpath
loopd: allow custom database path
2 parents 261eb81 + d7150d5 commit 5d61fd5

File tree

3 files changed

+75
-25
lines changed

3 files changed

+75
-25
lines changed

loopd/config.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package loopd
22

33
import (
4+
"fmt"
5+
"os"
46
"path/filepath"
57

68
"github.com/btcsuite/btcutil"
79
"github.com/lightninglabs/loop/lsat"
10+
"github.com/lightningnetwork/lnd/lncfg"
811
)
912

1013
var (
@@ -14,6 +17,7 @@ var (
1417
defaultLogDirname = "logs"
1518
defaultLogFilename = "loopd.log"
1619
defaultLogDir = filepath.Join(loopDirBase, defaultLogDirname)
20+
defaultConfigFile = filepath.Join(loopDirBase, defaultConfigFilename)
1721

1822
defaultMaxLogFiles = 3
1923
defaultMaxLogFileSize = 10
@@ -43,6 +47,9 @@ type Config struct {
4347
RESTListen string `long:"restlisten" description:"Address to listen on for REST clients"`
4448
CORSOrigin string `long:"corsorigin" description:"The value to send in the Access-Control-Allow-Origin header. Header will be omitted if empty."`
4549

50+
LoopDir string `long:"loopdir" description:"The directory for all of loop's data."`
51+
ConfigFile string `long:"configfile" description:"Path to configuration file."`
52+
DataDir string `long:"datadir" description:"Directory for loopdb."`
4653
LogDir string `long:"logdir" description:"Directory to log output."`
4754
MaxLogFiles int `long:"maxlogfiles" description:"Maximum logfiles to keep (0 for no rotation)"`
4855
MaxLogFileSize int `long:"maxlogfilesize" description:"Maximum logfile size in MB"`
@@ -74,6 +81,9 @@ func DefaultConfig() Config {
7481
Server: &loopServerConfig{
7582
NoTLS: false,
7683
},
84+
LoopDir: loopDirBase,
85+
ConfigFile: defaultConfigFile,
86+
DataDir: loopDirBase,
7787
LogDir: defaultLogDir,
7888
MaxLogFiles: defaultMaxLogFiles,
7989
MaxLogFileSize: defaultMaxLogFileSize,
@@ -86,3 +96,51 @@ func DefaultConfig() Config {
8696
},
8797
}
8898
}
99+
100+
// Validate cleans up paths in the config provided and validates it.
101+
func Validate(cfg *Config) error {
102+
// Cleanup any paths before we use them.
103+
cfg.LoopDir = lncfg.CleanAndExpandPath(cfg.LoopDir)
104+
cfg.DataDir = lncfg.CleanAndExpandPath(cfg.DataDir)
105+
cfg.LogDir = lncfg.CleanAndExpandPath(cfg.LogDir)
106+
107+
// Since our loop directory overrides our log/data dir values, make sure
108+
// that they are not set when loop dir is set. We hard here rather than
109+
// overwriting and potentially confusing the user.
110+
logDirSet := cfg.LogDir != defaultLogDir
111+
dataDirSet := cfg.DataDir != loopDirBase
112+
loopDirSet := cfg.LoopDir != loopDirBase
113+
114+
if loopDirSet {
115+
if logDirSet {
116+
return fmt.Errorf("loopdir overwrites logdir, please " +
117+
"only set one value")
118+
}
119+
120+
if dataDirSet {
121+
return fmt.Errorf("loopdir overwrites datadir, please " +
122+
"only set one value")
123+
}
124+
125+
// Once we are satisfied that neither config value was set, we
126+
// replace them with our loop dir.
127+
cfg.DataDir = cfg.LoopDir
128+
cfg.LogDir = filepath.Join(cfg.LoopDir, defaultLogDirname)
129+
}
130+
131+
// Append the network type to the data and log directory so they are
132+
// "namespaced" per network.
133+
cfg.DataDir = filepath.Join(cfg.DataDir, cfg.Network)
134+
cfg.LogDir = filepath.Join(cfg.LogDir, cfg.Network)
135+
136+
// If either of these directories do not exist, create them.
137+
if err := os.MkdirAll(cfg.DataDir, os.ModePerm); err != nil {
138+
return err
139+
}
140+
141+
if err := os.MkdirAll(cfg.LogDir, os.ModePerm); err != nil {
142+
return err
143+
}
144+
145+
return nil
146+
}

loopd/run.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/lightninglabs/lndclient"
1313
"github.com/lightninglabs/loop"
1414
"github.com/lightningnetwork/lnd/build"
15+
"github.com/lightningnetwork/lnd/lncfg"
1516
"github.com/lightningnetwork/lnd/lnrpc/verrpc"
1617
"github.com/lightningnetwork/lnd/signal"
1718
)
@@ -109,12 +110,19 @@ func Run(rpcCfg RPCConfig) error {
109110
}
110111

111112
// Parse ini file.
112-
loopDir := filepath.Join(loopDirBase, config.Network)
113-
if err := os.MkdirAll(loopDir, os.ModePerm); err != nil {
114-
return err
113+
loopDir := lncfg.CleanAndExpandPath(config.LoopDir)
114+
configFile := lncfg.CleanAndExpandPath(config.ConfigFile)
115+
116+
// If our loop directory is set and the config file parameter is not
117+
// set, we assume that they want to point to a config file in their
118+
// loop dir. However, if the config file has a non-default value, then
119+
// we leave the config parameter as its custom value.
120+
if loopDir != loopDirBase && configFile == defaultConfigFile {
121+
configFile = filepath.Join(
122+
loopDir, defaultConfigFilename,
123+
)
115124
}
116125

117-
configFile := filepath.Join(loopDir, defaultConfigFilename)
118126
if err := flags.IniParse(configFile, &config); err != nil {
119127
// If it's a parsing related error, then we'll return
120128
// immediately, otherwise we can proceed as possibly the config
@@ -146,9 +154,10 @@ func Run(rpcCfg RPCConfig) error {
146154
os.Exit(0)
147155
}
148156

149-
// Append the network type to the log directory so it is
150-
// "namespaced" per network in the same fashion as the data directory.
151-
config.LogDir = filepath.Join(config.LogDir, config.Network)
157+
// Validate our config before we proceed.
158+
if err := Validate(&config); err != nil {
159+
return err
160+
}
152161

153162
// Initialize logging at the default logging level.
154163
err = logWriter.InitLogRotator(

loopd/utils.go

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package loopd
22

33
import (
4-
"os"
5-
"path/filepath"
6-
74
"github.com/btcsuite/btcutil"
85
"github.com/lightninglabs/lndclient"
96
"github.com/lightninglabs/loop"
@@ -13,11 +10,6 @@ import (
1310
func getClient(config *Config, lnd *lndclient.LndServices) (*loop.Client,
1411
func(), error) {
1512

16-
storeDir, err := getStoreDir(config.Network)
17-
if err != nil {
18-
return nil, nil, err
19-
}
20-
2113
clientConfig := &loop.ClientConfig{
2214
ServerAddress: config.Server.Host,
2315
ProxyAddress: config.Server.Proxy,
@@ -29,19 +21,10 @@ func getClient(config *Config, lnd *lndclient.LndServices) (*loop.Client,
2921
LoopOutMaxParts: config.LoopOutMaxParts,
3022
}
3123

32-
swapClient, cleanUp, err := loop.NewClient(storeDir, clientConfig)
24+
swapClient, cleanUp, err := loop.NewClient(config.DataDir, clientConfig)
3325
if err != nil {
3426
return nil, nil, err
3527
}
3628

3729
return swapClient, cleanUp, nil
3830
}
39-
40-
func getStoreDir(network string) (string, error) {
41-
dir := filepath.Join(loopDirBase, network)
42-
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
43-
return "", err
44-
}
45-
46-
return dir, nil
47-
}

0 commit comments

Comments
 (0)