-
Notifications
You must be signed in to change notification settings - Fork 3
/
config.go
133 lines (115 loc) · 3.71 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package config
import (
"fmt"
"os"
"github.com/pkg/errors"
"github.com/spf13/viper"
)
const (
defaultEnv = "dev"
)
type Config struct {
Calendar CalendarServiceConfigurations `mapstructure:"calendar_service"`
Account AccountServiceConfigurations `mapstructure:"account_service"`
WebApi WebApiServiceConfigurations `mapstructure:"web_api_service"`
}
type CalendarServiceConfigurations struct {
DBUser string `mapstructure:"db_user"`
DBPass string `mapstructure:"db_pass"`
DBPort int `mapstructure:"db_port"`
DBName string `mapstructure:"db_name"`
URI string `mapstructure:"db_uri"`
MaxOpenConns int `mapstructure:"db_max_open_conns"`
MaxIdleConns int `mapstructure:"db_max_idle_conns"`
MaxIdleTime string `mapstructure:"db_max_idle_time"`
SSLMode string `mapstructure:"db_ssl_mode"`
GRPCPort string `mapstructure:"grpc_port"`
GRPCHost string `mapstructure:"grpc_host"`
}
type AccountServiceConfigurations struct {
DBUser string `mapstructure:"db_user"`
DBPass string `mapstructure:"db_pass"`
DBHost string `mapstructure:"db_host"`
DBPort int `mapstructure:"db_port"`
DBName string `mapstructure:"db_name"`
DSN string `mapstructure:"db_dsn"`
MaxOpenConns int `mapstructure:"db_max_open_conns"`
MaxIdleConns int `mapstructure:"db_max_idle_conns"`
MaxIdleTime string `mapstructure:"db_max_idle_time"`
SSLMode string `mapstructure:"db_ssl_mode"`
RedisUrl string `mapstructure:"redis_url"`
RedisPass string `mapstructure:"redis_pass"`
GRPCPort string `mapstructure:"grpc_port"`
GRPCHost string `mapstructure:"grpc_host"`
}
type WebApiServiceConfigurations struct {
CalendarServicePort string `mapstructure:"calendar_service_port"`
CalendarServiceHost string `mapstructure:"calendar_service_host"`
AccountServicePort string `mapstructure:"account_service_port"`
AccountServiceHost string `mapstructure:"account_service_host"`
HTTPPort string `mapstructure:"http_port"`
HTTPHost string `mapstructure:"http_host"`
}
var cfgReader *configReader
type (
configReader struct {
configFile string
v *viper.Viper
}
)
//// GetValueByKey gets value by key from the config
//func GetValueByKey(key string) (string, error) {
// newConfigReader()
//
// var err error
// if err = cfgReader.v.ReadInConfig(); err != nil {
// return "", errors.Wrap(err, "failed to read config file")
// }
// return cfgReader.v.GetString(key), nil
//}
// GetConfigByKey gets config to given struct by key
func GetConfigByKey(key string, config interface{}) error {
newConfigReader()
var err error
if err = cfgReader.v.ReadInConfig(); err != nil {
return errors.Wrap(err, "failed to read config file")
}
if err = cfgReader.v.UnmarshalKey(key, config); err != nil {
return errors.Wrap(err, "failed to unmarshal config")
}
return nil
}
//func GetConfig(config *Config) error {
// newConfigReader()
//
// var err error
// if err = cfgReader.v.ReadInConfig(); err != nil {
// return errors.Wrap(err, "failed to read config file")
// }
//
// if err = cfgReader.v.Unmarshal(config); err != nil {
// return errors.Wrap(err, "failed to unmarshal config")
// }
// return nil
//}
// getEnvironment gets environment if fail return fallback
func getEnvironment(env, fallback string) string {
e := os.Getenv(env)
if e == "" {
return fallback
}
return e
}
// newConfigReader creates new viper config reader.
func newConfigReader() {
env := getEnvironment("APP_ENVIRONMENT", defaultEnv)
configFile := fmt.Sprintf("api.%s.yaml", env)
v := viper.GetViper()
v.SetConfigType("yaml")
v.SetConfigFile(configFile)
v.AddConfigPath(".")
cfgReader = &configReader{
configFile: configFile,
v: v,
}
}