From 71950479fbd6088f249e5fda3b180f294d1d745d Mon Sep 17 00:00:00 2001 From: Max Date: Tue, 6 Feb 2018 00:35:39 +0100 Subject: Moves crawler to designated directory. --- crawler/config.go | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 crawler/config.go (limited to 'crawler/config.go') diff --git a/crawler/config.go b/crawler/config.go new file mode 100644 index 0000000..2706201 --- /dev/null +++ b/crawler/config.go @@ -0,0 +1,76 @@ +package main + +import ( + log "github.com/Sirupsen/logrus" + "github.com/spf13/viper" +) + +type Config struct { + DBDriver string + DBDBName string + DBHost string + DBPort string + DBUser string + DBPassword string + DBOptions string + DBPath string // for sqlite + + Debug bool +} + +// Parses the configuration and sets the configuration struct. +func (c *Config) parseConfig(configFile string) { + + viper.SetDefault("DBDriver", "mysql") + viper.SetDefault("DBDBName", "alkobote") + viper.SetDefault("DBHost", "localhost") + viper.SetDefault("DBPort", "3306") + + viper.SetDefault("DBPath", "./alkobote.db") + + viper.SetDefault("Debug", false) + + // 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() +} + +// Actually sets the config struct +func (c *Config) setsConfig() { + c.DBDriver = viper.GetString("DBDriver") + c.DBHost = viper.GetString("DBHost") + c.DBPort = viper.GetString("DBPort") + c.DBUser = viper.GetString("DBUser") + c.DBPassword = viper.GetString("DBPassword") + c.DBDBName = viper.GetString("DBDBName") + c.DBOptions = viper.GetString("DBOptions") + c.DBPath = viper.GetString("DBPath") + c.Debug = viper.GetBool("Debug") +} -- cgit v1.2.3