-
Notifications
You must be signed in to change notification settings - Fork 0
/
app_config.go
107 lines (92 loc) · 2.72 KB
/
app_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
package config
import (
"os"
"path/filepath"
"strings"
"github.com/fsnotify/fsnotify"
"github.com/shuvava/go-logging/logger"
"github.com/spf13/viper"
)
const (
defaultConfigName = "appsettings"
defaultConfigPath = "."
prodConfigName = "config_prod"
prodConfigPath = "./config"
)
// DbConfig service database configuration
type DbConfig struct {
Type string `mapstructure:"type"`
ConnectionString string `mapstructure:"connectionString"`
}
// AppConfig root app config
type AppConfig struct {
Port int `mapstructure:"port"`
LogLevel string `mapstructure:"logLevel"`
Db DbConfig `mapstructure:"db"`
}
// OnConfigChange callback for config changes
type OnConfigChange func(*AppConfig)
//NewConfig loads new config or run panic on error
func NewConfig(logger logger.Logger, fn OnConfigChange) *AppConfig {
log := logger.SetOperation("config-initialization")
var cfg AppConfig
if fn != nil {
viper.OnConfigChange(func(e fsnotify.Event) {
log.WithField("file", e.Name).
Debug("Config file was changed")
if err := viper.Unmarshal(&cfg); err != nil {
log.WithError(err).
Error("Error on config Unmarshal:")
} else {
log.Info("config auto reload!")
fn(&cfg)
}
})
}
// add config with default values
viper.SetConfigName(defaultConfigName)
viper.SetConfigType("yaml")
viper.AddConfigPath(defaultConfigPath)
err := viper.ReadInConfig() // Find and read the config file
if err != nil { // Handle errors reading the config file
log.WithError(err).
Fatal("Fatal error on reading config file")
}
// watching if default config changed
viper.WatchConfig()
if isPathExist(filepath.Join(prodConfigPath, prodConfigName)) {
// add production config
viper.SetConfigName(prodConfigName)
viper.AddConfigPath(prodConfigPath)
// merge values into default one
err = viper.MergeInConfig()
if err != nil { // Handle errors reading the config file
log.WithError(err).
Fatal("Fatal error on reading config file")
}
// watch production config changes
viper.WatchConfig()
}
// take into account env variables with the highest priority
replacer := strings.NewReplacer(".", "_")
viper.SetEnvKeyReplacer(replacer)
viper.AutomaticEnv()
err = viper.Unmarshal(&cfg)
if err != nil {
log.WithError(err).
Fatal("Fatal error on config Unmarshal")
}
return &cfg
}
// PrintConfig returns print current config into log output
func (cfg *AppConfig) PrintConfig(log logger.Logger) {
log.Info("Current config:")
log.Info(" Port :", cfg.Port)
log.Info(" LogLevel :", cfg.LogLevel)
log.Info(" Db.Type :", cfg.Db.Type)
}
// isPathExist checks if path exist
func isPathExist(path string) bool {
_, err := os.Stat(path)
return err == nil
}