config: Initialize default config struct directly.

There is no need to declare a dozen different "default..." vars only to
immediately insert them into a default config struct when the struct
itself could just be initialized with the default values instead.
This commit is contained in:
jholdstock 2024-05-20 10:17:26 +01:00 committed by Jamie Holdstock
parent 8b6b2e4fef
commit 17f993ae83

View File

@ -25,24 +25,9 @@ import (
flags "github.com/jessevdk/go-flags" flags "github.com/jessevdk/go-flags"
) )
const appName = "vspd" const (
configFilename = "vspd.conf"
var ( dbFilename = "vspd.db"
defaultListen = ":8800"
defaultLogLevel = "debug"
defaultMaxLogSize = int64(10)
defaultLogsToKeep = 20
defaultVSPFee = 3.0
defaultNetworkName = "testnet"
defaultHomeDir = dcrutil.AppDataDir(appName, false)
defaultConfigFilename = fmt.Sprintf("%s.conf", appName)
defaultDBFilename = fmt.Sprintf("%s.db", appName)
defaultDcrdHost = "127.0.0.1"
defaultWalletHost = "127.0.0.1"
defaultWebServerDebug = false
defaultBackupInterval = time.Minute * 3
defaultVspClosed = false
defaultDesignation = "Voting Service Provider"
) )
// vspdConfig defines the configuration options for the vspd process. // vspdConfig defines the configuration options for the vspd process.
@ -85,6 +70,22 @@ type vspdConfig struct {
walletCerts [][]byte walletCerts [][]byte
} }
var defaultConfig = vspdConfig{
Listen: ":8800",
LogLevel: "debug",
MaxLogSize: int64(10),
LogsToKeep: 20,
NetworkName: "testnet",
VSPFee: 3.0,
HomeDir: dcrutil.AppDataDir("vspd", false),
DcrdHost: "127.0.0.1",
WalletHosts: "127.0.0.1",
WebServerDebug: false,
BackupInterval: time.Minute * 3,
VspClosed: false,
Designation: "Voting Service Provider",
}
// fileExists reports whether the named file or directory exists. // fileExists reports whether the named file or directory exists.
func fileExists(name string) bool { func fileExists(name string) bool {
if _, err := os.Stat(name); os.IsNotExist(err) { if _, err := os.Stat(name); os.IsNotExist(err) {
@ -170,23 +171,7 @@ func normalizeAddress(addr, defaultPort string) string {
// while still allowing the user to override settings with config files and // while still allowing the user to override settings with config files and
// command line options. Command line options always take precedence. // command line options. Command line options always take precedence.
func loadConfig() (*vspdConfig, error) { func loadConfig() (*vspdConfig, error) {
cfg := defaultConfig
// Default config.
cfg := vspdConfig{
Listen: defaultListen,
LogLevel: defaultLogLevel,
MaxLogSize: defaultMaxLogSize,
LogsToKeep: defaultLogsToKeep,
NetworkName: defaultNetworkName,
VSPFee: defaultVSPFee,
HomeDir: defaultHomeDir,
DcrdHost: defaultDcrdHost,
WalletHosts: defaultWalletHost,
WebServerDebug: defaultWebServerDebug,
BackupInterval: defaultBackupInterval,
VspClosed: defaultVspClosed,
Designation: defaultDesignation,
}
// If command line options are requesting help, write it to stdout and exit. // If command line options are requesting help, write it to stdout and exit.
if config.WriteHelp(&cfg) { if config.WriteHelp(&cfg) {
@ -230,7 +215,7 @@ func loadConfig() (*vspdConfig, error) {
// Create a default config file when one does not exist and the user did // Create a default config file when one does not exist and the user did
// not specify an override. // not specify an override.
configFile := filepath.Join(cfg.HomeDir, defaultConfigFilename) configFile := filepath.Join(cfg.HomeDir, configFilename)
if !fileExists(configFile) { if !fileExists(configFile) {
preIni := flags.NewIniParser(preParser) preIni := flags.NewIniParser(preParser)
err = preIni.WriteFile(configFile, err = preIni.WriteFile(configFile,
@ -407,8 +392,8 @@ func loadConfig() (*vspdConfig, error) {
return nil, fmt.Errorf("unknown log level: %s", cfg.LogLevel) return nil, fmt.Errorf("unknown log level: %s", cfg.LogLevel)
} }
// Set the database path // Set the database path.
cfg.dbPath = filepath.Join(dataDir, defaultDBFilename) cfg.dbPath = filepath.Join(dataDir, dbFilename)
// If xpub has been provided, create a new database and exit. // If xpub has been provided, create a new database and exit.
if cfg.FeeXPub != "" { if cfg.FeeXPub != "" {