-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
61 lines (54 loc) · 1.75 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
package common
import (
"log"
"strings"
"github.com/fsnotify/fsnotify"
"github.com/getsentry/raven-go"
"github.com/spf13/viper"
)
func SetConfig() error {
viper.SetConfigName("example") // name of config file (without extension)
viper.AddConfigPath("conf")
viper.SetConfigType("yaml")
viper.AutomaticEnv()
viper.SetEnvPrefix("EXAMPLE")
replacer := strings.NewReplacer(".", "_")
viper.SetEnvKeyReplacer(replacer)
err := viper.ReadInConfig() // Find and read the config file
if err != nil { // Handle errors reading the config file
log.Println("Fatal error config file:", err)
raven.CaptureError(err, map[string]string{"type": "config"})
return err
}
return nil
}
func WatchConfig() error {
viper.WatchConfig()
viper.OnConfigChange(func(e fsnotify.Event) {
log.Println("Config file changed:", e.Name)
})
return nil
}
func DefaultConfig() error {
// basic default values
viper.SetDefault("basic.debug", true)
viper.SetDefault("basic.maintainance", false)
viper.SetDefault("basic.port", "8080")
// storage default values
viper.SetDefault("storage.mysql.user", "root")
viper.SetDefault("storage.mysql.password", "")
viper.SetDefault("storage.mysql.host", "localhost")
viper.SetDefault("storage.mysql.port", "3306")
viper.SetDefault("sotrage.mysql.database", "example")
viper.SetDefault("storage.mysql.timezone", "Asia%2FShanghai")
viper.SetDefault("storage.mysql.retry_interval", 20)
viper.SetDefault("storage.mysql.max_idle_conns", 30)
viper.SetDefault("storage.mysql.max_open_conns", 100)
// sentry default values
viper.SetDefault("sentry.dsn", "")
viper.SetDefault("sentry.default_logger_name", "example")
viper.SetDefault("sentry.sample_rate", 1)
// log default values
viper.SetDefault("log.file", "log/example.log")
return nil
}