Skip to content

Commit

Permalink
Remove the AppConfig singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
mcamou committed Nov 13, 2024
1 parent 5d12c5e commit b839305
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 35 deletions.
5 changes: 4 additions & 1 deletion cmd/masa-node/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ func main() {
os.Exit(0)
}

cfg := config.GetInstance()
cfg, err := config.GetConfig()
if err != nil {
logrus.Fatalf("[-] %v", err)
}

Check warning on line 36 in cmd/masa-node/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/masa-node/main.go#L33-L36

Added lines #L33 - L36 were not covered by tests
cfg.LogConfig()
cfg.SetupLogging()

Expand Down
56 changes: 22 additions & 34 deletions pkg/config/app.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package config

import (
"fmt"
"log"
"os/user"
"path/filepath"
"reflect"
"strings"
"sync"

"github.com/masa-finance/masa-oracle/internal/versioning"

Expand All @@ -17,11 +17,6 @@ import (
"github.com/spf13/viper"
)

var (
instance *AppConfig
once sync.Once
)

// AppConfig represents the configuration settings for the application.
// It holds various parameters and settings that control the behavior and runtime environment of the application.
// The fields in this struct are tagged with `mapstructure` to facilitate configuration loading from various sources
Expand Down Expand Up @@ -66,41 +61,34 @@ type AppConfig struct {
TelegramStop bg.StopFunc
}

// GetInstance returns the singleton instance of AppConfig.
// GetConfig parses and fills in the AppConfig. The configuration values are generated
// by the following steps in order:
//
// If the instance has not been initialized yet, GetInstance will initialize it by:
// 1. Creating a new AppConfig instance.
// 2. Setting default configuration values.
// 3. Overriding defaults with values from environment variables.
// 4. Overriding defaults and environment variables with values from the configuration file.
// 5. Overriding all previous values with command-line flags.
// 6. Unmarshalling the configuration into the AppConfig instance.
// 1. Set default configuration values.
// 2. Override with values from environment variables.
// 3. Override with values from the configuration file.
// 4. Override with command-line flags.
//
// If the unmarshalling fails, the instance is set to nil.
//
// Subsequent calls to GetInstance will return the same initialized instance.
func GetInstance() *AppConfig {
once.Do(func() {
instance = &AppConfig{}
// In case of any errors it returns nill with an error object
func GetConfig() (*AppConfig, error) {
instance := &AppConfig{}

Check warning on line 74 in pkg/config/app.go

View check run for this annotation

Codecov / codecov/patch

pkg/config/app.go#L73-L74

Added lines #L73 - L74 were not covered by tests

instance.setDefaultConfig()
// TODO Shouldn't the env vars override the file config, instead of the other way around?
instance.setEnvVariableConfig()
instance.setDefaultConfig()
// TODO Shouldn't the env vars override the file config, instead of the other way around?
instance.setEnvVariableConfig()
instance.setFileConfig(viper.GetString("FILE_PATH"))

Check warning on line 79 in pkg/config/app.go

View check run for this annotation

Codecov / codecov/patch

pkg/config/app.go#L76-L79

Added lines #L76 - L79 were not covered by tests

instance.setFileConfig(viper.GetString("FILE_PATH"))
if err := instance.setCommandLineConfig(); err != nil {
return nil, fmt.Errorf("Error while setting command line options: %v", err)
}

Check warning on line 83 in pkg/config/app.go

View check run for this annotation

Codecov / codecov/patch

pkg/config/app.go#L81-L83

Added lines #L81 - L83 were not covered by tests

if err := instance.setCommandLineConfig(); err != nil {
logrus.Fatal(err)
}
if err := viper.Unmarshal(instance); err != nil {
return nil, fmt.Errorf("Unable to unmarshal config into struct: %v", err)
}

Check warning on line 87 in pkg/config/app.go

View check run for this annotation

Codecov / codecov/patch

pkg/config/app.go#L85-L87

Added lines #L85 - L87 were not covered by tests

if err := viper.Unmarshal(instance); err != nil {
logrus.Errorf("[-] Unable to unmarshal config into struct, %v", err)
instance = nil
}
instance.APIEnabled = viper.GetBool("api_enabled")

Check warning on line 89 in pkg/config/app.go

View check run for this annotation

Codecov / codecov/patch

pkg/config/app.go#L89

Added line #L89 was not covered by tests

instance.APIEnabled = viper.GetBool("api_enabled")
})
return instance
return instance, nil

Check warning on line 91 in pkg/config/app.go

View check run for this annotation

Codecov / codecov/patch

pkg/config/app.go#L91

Added line #L91 was not covered by tests
}

// setDefaultConfig sets the default configuration values for the AppConfig instance.
Expand Down

0 comments on commit b839305

Please sign in to comment.