-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
113 lines (96 loc) · 2.68 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
package config
import (
"strings"
"bytes"
"fmt"
"io/ioutil"
"github.com/fsnotify/fsnotify"
"github.com/spf13/viper"
)
const (
configName = "config"
)
var defaultConf = []byte(`
{
"debug": false,
"address": "0.0.0.0",
"port": 8040,
"log": {
"format": "string",
"access_log": "stdout",
"access_level": "debug",
"error_log": "stderr",
"error_level": "error",
"sentry_dsn": ""
}
}
`)
// ConfJSON is config struct
type ConfJSON struct {
Core SectionCore `json:"core"`
Log SectionLog `json:"log"`
}
// SectionCore is sub section of config
type SectionCore struct {
Debug bool `json:"debug"`
Address string `json:"address"`
Port int `json:"port"`
}
// SectionLog is sub section of config.
type SectionLog struct {
Format string `json:"format"`
AccessLog string `json:"access_log"`
AccessLevel string `json:"access_level"`
ErrorLog string `json:"error_log"`
ErrorLevel string `json:"error_level"`
SentryDSN string `json:"sentry_dsn"`
}
// KV key value type
type KV map[string]string
// LoadConf load the config settings
func LoadConf(prefix string, configPath string) (ConfJSON, error) {
var confJSON ConfJSON
lowerPrefix := strings.ToLower(prefix)
viper.SetConfigType("json")
viper.AutomaticEnv()
viper.SetEnvPrefix(lowerPrefix)
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
if configPath != "" {
content, err := ioutil.ReadFile(configPath)
if err != nil {
return confJSON, err
}
if err := viper.ReadConfig(bytes.NewBuffer(content)); err != nil {
return confJSON, err
}
} else {
viper.AddConfigPath(".")
viper.AddConfigPath("/etc/" + lowerPrefix + "/")
viper.AddConfigPath("$HOME/." + lowerPrefix)
viper.SetConfigName(configName)
// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
fmt.Println("Using config file:", viper.ConfigFileUsed())
viper.WatchConfig()
viper.OnConfigChange(func(e fsnotify.Event) {
fmt.Println("Config file changed:", e.Name)
})
} else {
fmt.Println("load default config ...")
// load default config
if err := viper.ReadConfig(bytes.NewBuffer(defaultConf)); err != nil {
return confJSON, err
}
}
}
confJSON.Core.Debug = viper.GetBool("debug")
confJSON.Core.Address = viper.GetString("address")
confJSON.Core.Port = viper.GetInt("port")
confJSON.Log.Format = viper.GetString("log.format")
confJSON.Log.AccessLog = viper.GetString("log.access_log")
confJSON.Log.AccessLevel = viper.GetString("log.access_level")
confJSON.Log.ErrorLog = viper.GetString("log.error_log")
confJSON.Log.ErrorLevel = viper.GetString("log.error_level")
confJSON.Log.SentryDSN = viper.GetString("log.sentry_dsn")
return confJSON, nil
}