multi: extract config validation

As a preparation to add more configuration paths for TLS, we first
extract the config validation and path cleanup into its own function.
This commit is contained in:
Oliver Gugger 2020-09-09 16:15:45 +02:00
parent 2afc37c410
commit ce235cbc1d
No known key found for this signature in database
GPG key ID: 8E4256593F177720
4 changed files with 58 additions and 15 deletions

View file

@ -43,12 +43,9 @@ func start() error {
}
// Parse ini file.
networkDir := filepath.Join(config.BaseDir, config.Network)
if err := os.MkdirAll(networkDir, os.ModePerm); err != nil {
return err
}
poolDir := filepath.Join(config.BaseDir, config.Network)
configFile := filepath.Join(poolDir, defaultConfigFilename)
configFile := filepath.Join(networkDir, defaultConfigFilename)
if err := flags.IniParse(configFile, &config); err != nil {
// If it's a parsing related error, then we'll return
// immediately, otherwise we can proceed as possibly the config
@ -65,6 +62,11 @@ func start() error {
return err
}
// Make sure the passed configuration is valid.
if err := pool.Validate(&config); err != nil {
return err
}
// Enable http profiling and Validate profile port number if reqeusted.
if config.Profile != "" {
profilePort, err := strconv.Atoi(config.Profile)

View file

@ -1,11 +1,14 @@
package pool
import (
"fmt"
"net"
"os"
"path/filepath"
"time"
"github.com/btcsuite/btcutil"
"github.com/lightningnetwork/lnd/lncfg"
"google.golang.org/grpc"
)
@ -16,6 +19,9 @@ var (
// created.
DefaultBaseDir = btcutil.AppDataDir("pool", false)
// DefaultNetwork is the default bitcoin network pool runs on.
DefaultNetwork = "mainnet"
// DefaultLogFilename is the default name that is given to the pool log
// file.
DefaultLogFilename = "poold.log"
@ -45,7 +51,7 @@ type Config struct {
TLSPathAuctSrv string `long:"tlspathauctserver" description:"Path to auction server tls certificate"`
RPCListen string `long:"rpclisten" description:"Address to listen on for gRPC clients"`
RESTListen string `long:"restlisten" description:"Address to listen on for REST clients"`
BaseDir string `long:"basedir" description:"The base directory where pool stores all its data"`
BaseDir string `long:"basedir" description:"The base directory where pool stores all its data. If set, this option overwrites --logdir."`
LogDir string `long:"logdir" description:"Directory to log output."`
MaxLogFiles int `long:"maxlogfiles" description:"Maximum logfiles to keep (0 for no rotation)"`
@ -87,7 +93,7 @@ const (
// DefaultConfig returns the default value for the Config struct.
func DefaultConfig() Config {
return Config{
Network: "mainnet",
Network: DefaultNetwork,
RPCListen: "localhost:12010",
RESTListen: "localhost:8281",
Insecure: false,
@ -103,3 +109,44 @@ func DefaultConfig() Config {
},
}
}
// Validate cleans up paths in the config provided and validates it.
func Validate(cfg *Config) error {
// Cleanup any paths before we use them.
cfg.BaseDir = lncfg.CleanAndExpandPath(cfg.BaseDir)
cfg.LogDir = lncfg.CleanAndExpandPath(cfg.LogDir)
// Since our pool directory overrides our log dir value, make sure that
// they are not set when base dir is set. We hard here rather than
// overwriting and potentially confusing the user.
baseDirSet := cfg.BaseDir != DefaultBaseDir
if baseDirSet {
logDirSet := cfg.LogDir != defaultLogDir
if logDirSet {
return fmt.Errorf("basedir overwrites logdir, please " +
"only set one value")
}
// Once we are satisfied that no other config value was set, we
// replace them with our pool dir.
cfg.LogDir = filepath.Join(cfg.BaseDir, defaultLogDirname)
}
// Append the network type to the log and base directory so it is
// "namespaced" per network in the same fashion as the data directory.
cfg.LogDir = filepath.Join(cfg.LogDir, cfg.Network)
cfg.BaseDir = filepath.Join(cfg.BaseDir, cfg.Network)
// If either of these directories do not exist, create them.
if err := os.MkdirAll(cfg.BaseDir, os.ModePerm); err != nil {
return err
}
if err := os.MkdirAll(cfg.LogDir, os.ModePerm); err != nil {
return err
}
return nil
}

4
run.go
View file

@ -10,10 +10,6 @@ import (
// Run starts the trader daemon and blocks until it's shut down again.
func Run(cfg *Config) error {
// Append the network type to the log directory so it is
// "namespaced" per network in the same fashion as the data directory.
cfg.LogDir = filepath.Join(cfg.LogDir, cfg.Network)
// Initialize logging at the default logging level.
err := logWriter.InitLogRotator(
filepath.Join(cfg.LogDir, DefaultLogFilename),

View file

@ -8,7 +8,6 @@ import (
"fmt"
"net"
"net/http"
"path/filepath"
"sync"
"sync/atomic"
@ -255,14 +254,13 @@ func (s *Server) setupClient() error {
// Open the main database.
var err error
networkDir := filepath.Join(s.cfg.BaseDir, s.cfg.Network)
s.db, err = clientdb.New(networkDir)
s.db, err = clientdb.New(s.cfg.BaseDir)
if err != nil {
return err
}
// Setup the LSAT interceptor for the client.
s.lsatStore, err = lsat.NewFileStore(networkDir)
s.lsatStore, err = lsat.NewFileStore(s.cfg.BaseDir)
if err != nil {
return err
}