Browse Source

#36 Hotfix

dependabot/npm_and_yarn/web/prismjs-1.21.0
Max Schmitt 8 years ago
parent
commit
477cf2de16
  1. 9
      handlers/auth_test.go
  2. 12
      util/config.go
  3. 2
      util/private.go

9
handlers/auth_test.go

@ -42,14 +42,9 @@ var (
func TestCreateBackend(t *testing.T) { func TestCreateBackend(t *testing.T) {
secret = util.GetPrivateKey() secret = util.GetPrivateKey()
viper.SetConfigName("config")
viper.AddConfigPath("../") viper.AddConfigPath("../")
util.SetConfigDefaults() viper.Set("General.DataDir", "../data")
err := viper.ReadInConfig() if err := util.ReadInConfig(); err != nil {
if err != nil {
t.Fatalf("could not reload config file: %v", err)
}
if err := util.CheckForDatadir(); err != nil {
t.Fatalf("could not reload config file: %v", err) t.Fatalf("could not reload config file: %v", err)
} }
store, err := store.New(logrus.New()) store, err := store.New(logrus.New())

12
util/config.go

@ -11,21 +11,23 @@ import (
var dataDirPath string var dataDirPath string
// ReadInConfig loads the configuration and other needed folders for further usage
func ReadInConfig() error { func ReadInConfig() error {
viper.AutomaticEnv() viper.AutomaticEnv()
viper.SetEnvPrefix("gus") viper.SetEnvPrefix("gus")
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.SetConfigName("config") viper.SetConfigName("config")
viper.AddConfigPath(".") viper.AddConfigPath(".")
SetConfigDefaults() setConfigDefaults()
err := viper.ReadInConfig() err := viper.ReadInConfig()
if err != nil { if err != nil {
return errors.Wrap(err, "could not reload config file") 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.ListenAddr", ":8080")
viper.SetDefault("http.BaseURL", "http://localhost:3000") viper.SetDefault("http.BaseURL", "http://localhost:3000")
@ -34,11 +36,13 @@ func SetConfigDefaults() {
viper.SetDefault("General.ShortedIDLength", 4) viper.SetDefault("General.ShortedIDLength", 4)
} }
// GetDataDir returns the absolute path of the data directory
func GetDataDir() string { func GetDataDir() string {
return dataDirPath return dataDirPath
} }
func CheckForDatadir() error { // checkForDatadir checks for the data dir and creates it if it not exists
func checkForDatadir() error {
var err error var err error
dataDirPath, err = filepath.Abs(viper.GetString("General.DataDir")) dataDirPath, err = filepath.Abs(viper.GetString("General.DataDir"))
if err != nil { if err != nil {

2
util/private.go

@ -11,6 +11,7 @@ import (
var privateKey []byte var privateKey []byte
// CheckForPrivateKey checks if already an private key exists, if not one will be randomly generated
func CheckForPrivateKey() error { func CheckForPrivateKey() error {
privateDat := filepath.Join(GetDataDir(), "private.dat") privateDat := filepath.Join(GetDataDir(), "private.dat")
d, err := ioutil.ReadFile(privateDat) d, err := ioutil.ReadFile(privateDat)
@ -31,6 +32,7 @@ func CheckForPrivateKey() error {
return nil return nil
} }
// GetPrivateKey returns the private key from the memory
func GetPrivateKey() []byte { func GetPrivateKey() []byte {
return privateKey return privateKey
} }

Loading…
Cancel
Save