summaryrefslogtreecommitdiff
path: root/crawler/config.go
blob: 20f8d135c920b16cb4f6eea2211c720751b52dc9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package main

import (
	"os"

	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

	DisableURLShorter bool
	Polr_URL          string
	Polr_API_Key      string

	Debug       bool
	FixDatabase bool
	ShopIDs     []string
}

// Parses the configuration and sets the configuration struct.
func (c *Config) parseConfig(configFile string) {

	viper.SetDefault("DB_Driver", "mysql")
	viper.SetDefault("DB_DBName", "alkobote")
	viper.SetDefault("DB_Host", "localhost")
	viper.SetDefault("DB_Port", "3306")

	viper.SetDefault("DB_Path", "./alkobote.db")

	viper.SetDefault("Debug", false)
	viper.SetDefault("FixDatabase", false)
	viper.SetDefault("DisableURLShorter", false)
	viper.SetDefault("ShopIDs", []string{})

	// 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 {
		stat, err := os.Stat(configFile)
		if stat.IsDir() || os.IsNotExist(err) {
			// provided config file does not exist, so we add the path instead
			viper.AddConfigPath(configFile)
		} else {
			// directly sets the config file
			viper.SetConfigFile(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 {
		Fatal(err, "Config: Error parsing config file.")
	}
	log.Debug("Config: Config file used: " + viper.ConfigFileUsed())

	c.setsConfig()
}

// Actually sets the config struct
func (c *Config) setsConfig() {
	c.DBDriver = viper.GetString("DB_Driver")
	c.DBHost = viper.GetString("DB_Host")
	c.DBPort = viper.GetString("DB_Port")
	c.DBUser = viper.GetString("DB_User")
	c.DBPassword = viper.GetString("DB_Password")
	c.DBDBName = viper.GetString("DB_DBName")
	c.DBOptions = viper.GetString("DB_Options")
	c.DBPath = viper.GetString("DB_Path")
	c.Debug = viper.GetBool("Debug")
	c.FixDatabase = viper.GetBool("FixDatabase")
	c.DisableURLShorter = viper.GetBool("DisableURLShorter")
	c.ShopIDs = viper.GetStringSlice("ShopIDs")
	c.Polr_URL = viper.GetString("Polr_URL")
	c.Polr_API_Key = viper.GetString("Polr_API_Key")
}