diff options
| author | horus_arch | 2018-02-05 00:13:47 +0100 |
|---|---|---|
| committer | horus_arch | 2018-02-05 00:13:47 +0100 |
| commit | 543ebae42be1b7385e476a4699a7f88e95a2d120 (patch) | |
| tree | 492b065baf9fe93326a1e6de36583565b386699f /config.go | |
| parent | 4f7a9316da8e19fa9466ee377148c7d07bf39fd9 (diff) | |
| download | alkobote-543ebae42be1b7385e476a4699a7f88e95a2d120.tar.gz | |
Adds dependency on sqlx, viper and logrus.
Diffstat (limited to 'config.go')
| -rw-r--r-- | config.go | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/config.go b/config.go new file mode 100644 index 0000000..f877eba --- /dev/null +++ b/config.go @@ -0,0 +1,54 @@ +package main + +import ( + log "github.com/Sirupsen/logrus" + "github.com/spf13/viper" +) + +type Config struct { + DBDBName string + DBHost string + DBPort int + DBUser string + DBPassword string + DBOptions string +} + +// Parses the configuration and sets the configuration struct. +func (c *Config) parseConfig(configFile string) { + + viper.SetDefault("DBDBName", "alkobote") + viper.SetDefault("DBHost", "127.0.0.1") + viper.SetDefault("DBPort", 3306) + + // Name of the configuration file + viper.SetConfigName("config") + + // Where to find the config file + if configFile == "" { + viper.AddConfigPath("/etc/alkobote.de/") + viper.AddConfigPath(".") + viper.AddConfigPath("$HOME/.config/alkobote.de/") + viper.AddConfigPath("$HOME/alkobote.de/") + } else { + viper.AddConfigPath(configFile) + } + + // Env variables need to be prefixed with "ALKOBOTE_" + viper.SetEnvPrefix("ALKOBOTE") + + // Parses automatic the matching env variables + viper.AutomaticEnv() + + // Reads the config + err := viper.ReadInConfig() + if err != nil { + log.WithFields( + log.Fields{ + "error": err.Error(), + }, + ).Fatal("Fatal error config file") + } + + c.setsConfig() +} |
