summaryrefslogtreecommitdiff
path: root/crawler/config.go
blob: 27062018760a5b0212027bda210a9802eb9cbffa (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
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")
}