vspd: Move config to internal package.

This enables the config to be reused in multiple binaries - eg. the
upcoming vsp admin binary which will be responsible for writing a
default config file for new vspd deployments.
This commit is contained in:
jholdstock 2024-05-23 09:59:41 +01:00 committed by Jamie Holdstock
parent 10457e6110
commit 086143fed2
2 changed files with 15 additions and 15 deletions

View File

@ -37,7 +37,7 @@ func main() {
// initLogging uses the provided vspd config to create a logging backend, and // initLogging uses the provided vspd config to create a logging backend, and
// returns a function which can be used to create ready-to-use subsystem // returns a function which can be used to create ready-to-use subsystem
// loggers. // loggers.
func initLogging(cfg *vspdConfig) (func(subsystem string) slog.Logger, error) { func initLogging(cfg *vspd.Config) (func(subsystem string) slog.Logger, error) {
backend, err := newLogBackend(cfg.LogDir(), "vspd", cfg.MaxLogSize, cfg.LogsToKeep) backend, err := newLogBackend(cfg.LogDir(), "vspd", cfg.MaxLogSize, cfg.LogsToKeep)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to initialize logger: %w", err) return nil, fmt.Errorf("failed to initialize logger: %w", err)
@ -60,7 +60,7 @@ func initLogging(cfg *vspdConfig) (func(subsystem string) slog.Logger, error) {
// fact that deferred functions do not run when os.Exit() is called. // fact that deferred functions do not run when os.Exit() is called.
func run() int { func run() int {
// Load config file and parse CLI args. // Load config file and parse CLI args.
cfg, err := loadConfig() cfg, err := vspd.LoadConfig()
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "loadConfig error: %v\n", err) fmt.Fprintf(os.Stderr, "loadConfig error: %v\n", err)
return 1 return 1

View File

@ -2,7 +2,7 @@
// Use of this source code is governed by an ISC // Use of this source code is governed by an ISC
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
package main package vspd
import ( import (
"errors" "errors"
@ -29,8 +29,8 @@ const (
dbFilename = "vspd.db" dbFilename = "vspd.db"
) )
// vspdConfig defines the configuration options for the vspd process. // Config defines the configuration options for the vspd process.
type vspdConfig struct { type Config struct {
Listen string `long:"listen" ini-name:"listen" description:"The ip:port to listen for API requests."` Listen string `long:"listen" ini-name:"listen" description:"The ip:port to listen for API requests."`
LogLevel string `long:"loglevel" ini-name:"loglevel" description:"Logging level." choice:"trace" choice:"debug" choice:"info" choice:"warn" choice:"error" choice:"critical"` LogLevel string `long:"loglevel" ini-name:"loglevel" description:"Logging level." choice:"trace" choice:"debug" choice:"info" choice:"warn" choice:"error" choice:"critical"`
MaxLogSize int64 `long:"maxlogsize" ini-name:"maxlogsize" description:"File size threshold for log file rotation (MB)."` MaxLogSize int64 `long:"maxlogsize" ini-name:"maxlogsize" description:"File size threshold for log file rotation (MB)."`
@ -59,7 +59,7 @@ type vspdConfig struct {
HomeDir string `long:"homedir" no-ini:"true" description:"Path to application home directory. Used for storing VSP database and logs."` HomeDir string `long:"homedir" no-ini:"true" description:"Path to application home directory. Used for storing VSP database and logs."`
ConfigFile string `long:"configfile" no-ini:"true" description:"DEPRECATED: This behavior is no longer available and this option will be removed in a future version of the software."` ConfigFile string `long:"configfile" no-ini:"true" description:"DEPRECATED: This behavior is no longer available and this option will be removed in a future version of the software."`
// The following fields are derived from the above fields by loadConfig(). // The following fields are derived from the above fields by LoadConfig().
dataDir string dataDir string
network *config.Network network *config.Network
dcrdCert []byte dcrdCert []byte
@ -67,27 +67,27 @@ type vspdConfig struct {
walletCerts [][]byte walletCerts [][]byte
} }
func (cfg *vspdConfig) Network() *config.Network { func (cfg *Config) Network() *config.Network {
return cfg.network return cfg.network
} }
func (cfg *vspdConfig) LogDir() string { func (cfg *Config) LogDir() string {
return filepath.Join(cfg.HomeDir, "logs", cfg.network.Name) return filepath.Join(cfg.HomeDir, "logs", cfg.network.Name)
} }
func (cfg *vspdConfig) DatabaseFile() string { func (cfg *Config) DatabaseFile() string {
return filepath.Join(cfg.dataDir, dbFilename) return filepath.Join(cfg.dataDir, dbFilename)
} }
func (cfg *vspdConfig) DcrdDetails() (string, string, string, []byte) { func (cfg *Config) DcrdDetails() (string, string, string, []byte) {
return cfg.DcrdUser, cfg.DcrdPass, cfg.DcrdHost, cfg.dcrdCert return cfg.DcrdUser, cfg.DcrdPass, cfg.DcrdHost, cfg.dcrdCert
} }
func (cfg *vspdConfig) WalletDetails() ([]string, []string, []string, [][]byte) { func (cfg *Config) WalletDetails() ([]string, []string, []string, [][]byte) {
return cfg.walletUsers, cfg.walletPasswords, cfg.walletHosts, cfg.walletCerts return cfg.walletUsers, cfg.walletPasswords, cfg.walletHosts, cfg.walletCerts
} }
var defaultConfig = vspdConfig{ var DefaultConfig = Config{
Listen: ":8800", Listen: ":8800",
LogLevel: "debug", LogLevel: "debug",
MaxLogSize: int64(10), MaxLogSize: int64(10),
@ -175,7 +175,7 @@ func normalizeAddress(addr, defaultPort string) string {
return addr return addr
} }
// loadConfig initializes and parses the config using a config file and command // LoadConfig initializes and parses the config using a config file and command
// line options. // line options.
// //
// The configuration proceeds as follows: // The configuration proceeds as follows:
@ -187,8 +187,8 @@ func normalizeAddress(addr, defaultPort string) string {
// The above results in vspd functioning properly without any config settings // The above results in vspd functioning properly without any config settings
// 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() (*Config, error) {
cfg := defaultConfig cfg := DefaultConfig
// 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) {