-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
98 lines (87 loc) · 2.69 KB
/
config.go
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
97
98
package main
import (
"fmt"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
const defaultEnvString = "-1" // default value for fields which can be initialized by environment variables
type Config struct {
Logger LoggerConf
DataBase DBMSConf
Server ServerConf
Rabbit RabbitConf
}
type LoggerConf struct {
Level string `mapstructure:"level"`
Path string `mapstructure:"path"`
}
type DBMSConf struct {
Login string `mapstructure:"login"`
Password string `mapstructure:"password"`
Port int `mapstructure:"port"`
DBName string `mapstructure:"dbname"`
Host string `mapstructure:"host"`
MigrationsDir string `mapstructure:"migrations"`
}
type ServerConf struct {
Port int `mapstructure:"port"`
}
type RabbitConf struct {
URL string `mapstructure:"url"`
ExchangeName string `mapstructure:"name"`
ClickRoutingKey string `mapstructure:"click routing key"`
ShowRoutingKey string `mapstructure:"show routing key"`
}
func NewConfig() (Config, error) {
c := Config{}
c.DataBase.MigrationsDir = defaultEnvString
c.DataBase.Login = defaultEnvString
c.DataBase.DBName = defaultEnvString
c.DataBase.Password = defaultEnvString
err := viper.Unmarshal(&c)
if err != nil {
log.Errorf("unable to decode into struct")
return c, err
}
if c.DataBase.Login == defaultEnvString {
ok := false
c.DataBase.Login, ok = viper.Get("dblogin").(string)
if !ok {
return c, fmt.Errorf(
"database login is not set. Define environment variable 'POSTGRES_USER' "+
"or \"database\":\"login\" in the config file or check it is not equal '%s'",
defaultEnvString)
}
}
if c.DataBase.DBName == defaultEnvString {
ok := false
c.DataBase.DBName, ok = viper.Get("dbname").(string)
if !ok {
return c, fmt.Errorf(
"database name is not set. Define environment variable 'POSTGRES_DB' "+
"or \"database\":\"dbname\" in the config file or check it is not equal '%s'",
defaultEnvString)
}
}
if c.DataBase.Password == defaultEnvString {
ok := false
c.DataBase.Password, ok = viper.Get("dbpassword").(string)
if !ok {
return c, fmt.Errorf(
"database login is not set. Define environment variable 'POSTGRES_PASSWORD' "+
"or \"database\":\"password\" in the config file or check it is not equal '%s'",
defaultEnvString)
}
}
if c.DataBase.MigrationsDir == defaultEnvString {
ok := false
c.DataBase.MigrationsDir, ok = viper.Get("migrations").(string)
if !ok {
return c, fmt.Errorf(
"migrations directory is not set. Define environment variable 'MIGRATIONS_DIRECTORY' "+
"or \"database\":\"migrations\" in the config file or check it is not equal '%s'",
defaultEnvString)
}
}
return c, nil
}