diff --git a/handlers/auth_test.go b/handlers/auth_test.go index 640f861..5ab7f51 100644 --- a/handlers/auth_test.go +++ b/handlers/auth_test.go @@ -42,14 +42,9 @@ var ( func TestCreateBackend(t *testing.T) { secret = util.GetPrivateKey() - viper.SetConfigName("config") viper.AddConfigPath("../") - util.SetConfigDefaults() - err := viper.ReadInConfig() - if err != nil { - t.Fatalf("could not reload config file: %v", err) - } - if err := util.CheckForDatadir(); err != nil { + viper.Set("General.DataDir", "../data") + if err := util.ReadInConfig(); err != nil { t.Fatalf("could not reload config file: %v", err) } store, err := store.New(logrus.New()) diff --git a/util/config.go b/util/config.go index ad31e8a..5f24edd 100644 --- a/util/config.go +++ b/util/config.go @@ -11,21 +11,23 @@ import ( var dataDirPath string +// ReadInConfig loads the configuration and other needed folders for further usage func ReadInConfig() error { viper.AutomaticEnv() viper.SetEnvPrefix("gus") viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) viper.SetConfigName("config") viper.AddConfigPath(".") - SetConfigDefaults() + setConfigDefaults() err := viper.ReadInConfig() if err != nil { return errors.Wrap(err, "could not reload config file") } - return CheckForDatadir() + return checkForDatadir() } -func SetConfigDefaults() { +// setConfigDefaults sets the default values for the configuration +func setConfigDefaults() { viper.SetDefault("http.ListenAddr", ":8080") viper.SetDefault("http.BaseURL", "http://localhost:3000") @@ -34,11 +36,13 @@ func SetConfigDefaults() { viper.SetDefault("General.ShortedIDLength", 4) } +// GetDataDir returns the absolute path of the data directory func GetDataDir() string { return dataDirPath } -func CheckForDatadir() error { +// checkForDatadir checks for the data dir and creates it if it not exists +func checkForDatadir() error { var err error dataDirPath, err = filepath.Abs(viper.GetString("General.DataDir")) if err != nil { diff --git a/util/private.go b/util/private.go index 0576f51..96d3d79 100644 --- a/util/private.go +++ b/util/private.go @@ -11,6 +11,7 @@ import ( var privateKey []byte +// CheckForPrivateKey checks if already an private key exists, if not one will be randomly generated func CheckForPrivateKey() error { privateDat := filepath.Join(GetDataDir(), "private.dat") d, err := ioutil.ReadFile(privateDat) @@ -31,6 +32,7 @@ func CheckForPrivateKey() error { return nil } +// GetPrivateKey returns the private key from the memory func GetPrivateKey() []byte { return privateKey }