summaryrefslogtreecommitdiff
path: root/crawler/config.go
diff options
context:
space:
mode:
Diffstat (limited to 'crawler/config.go')
-rw-r--r--crawler/config.go76
1 files changed, 76 insertions, 0 deletions
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")
+}