aperture: add NewConfig function

Add a NewConfig func so that all pointer variables in the config object
can be initialised so that we can avoid needing to do nil checks
everywhere.
This commit is contained in:
Elle Mouton
2022-02-11 09:09:27 +02:00
parent 3f36447006
commit 559d2b3b97
3 changed files with 21 additions and 9 deletions

View File

@@ -181,9 +181,10 @@ func (a *Aperture) Start(errChan chan error) error {
var err error
// Start the prometheus exporter.
if err := StartPrometheusExporter(a.cfg.Prometheus); err != nil {
return fmt.Errorf("unable to start the prometheus exporter: "+
"%v", err)
err = StartPrometheusExporter(a.cfg.Prometheus)
if err != nil {
return fmt.Errorf("unable to start the prometheus "+
"exporter: %v", err)
}
// Initialize our etcd client.
@@ -281,7 +282,7 @@ func (a *Aperture) Start(errChan chan error) error {
// will only be reached through the onion services, which already
// provide encryption, so running this additional HTTP server should be
// relatively safe.
if a.cfg.Tor != nil && (a.cfg.Tor.V2 || a.cfg.Tor.V3) {
if a.cfg.Tor.V2 || a.cfg.Tor.V3 {
torController, err := initTorListener(a.cfg, a.etcdClient)
if err != nil {
return err
@@ -363,7 +364,7 @@ func fileExists(name string) bool {
func getConfig() (*Config, error) {
// Pre-parse command line flags to determine whether we've been pointed
// to a custom config file.
cfg := &Config{}
cfg := NewConfig()
if _, err := flags.Parse(cfg); err != nil {
return nil, err
}

View File

@@ -128,10 +128,8 @@ type Config struct {
}
func (c *Config) validate() error {
if c.Authenticator != nil {
if err := c.Authenticator.validate(); err != nil {
return err
}
if err := c.Authenticator.validate(); err != nil {
return err
}
if c.ListenAddr == "" {
@@ -140,3 +138,14 @@ func (c *Config) validate() error {
return nil
}
// NewConfig initializes a new Config variable.
func NewConfig() *Config {
return &Config{
Etcd: &EtcdConfig{},
Authenticator: &AuthConfig{},
Tor: &TorConfig{},
HashMail: &HashMailConfig{},
Prometheus: &PrometheusConfig{},
}
}

View File

@@ -163,6 +163,8 @@ func setupAperture(t *testing.T) {
MessageRate: time.Millisecond,
MessageBurstAllowance: math.MaxUint32,
},
Prometheus: &PrometheusConfig{},
Tor: &TorConfig{},
}
aperture := NewAperture(apertureCfg)
errChan := make(chan error)