forked from hulklab/yago
/
conf.go
174 lines (140 loc) · 3.57 KB
/
conf.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package yago
import (
"flag"
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"sync"
"github.com/goees/yago/libs/arr"
"github.com/spf13/viper"
//_ "github.com/spf13/viper/remote"
)
type AppConfig struct {
*viper.Viper
}
func (c *AppConfig) ReadFileConfig(cfgPath string) error {
err := c.ReadInConfig() // Find and read the config file
if err != nil { // Handle errors reading the config file
return fmt.Errorf("Fatal error config file: %s \n, \"--help\" gives usage information", err)
}
// deal with import file
importFiles, err := c.readImportFiles(cfgPath)
if err != nil {
return fmt.Errorf("Fatal error merge import config file: %s ", err)
}
if len(importFiles) >= 2 {
//log.Println("import configs:", importFiles)
// the last one don't need merge
for i := len(importFiles) - 2; i >= 0; i-- {
importFile := importFiles[i]
c.SetConfigFile(importFile)
_ = c.MergeInConfig()
}
}
return err
}
func (c *AppConfig) readImportFiles(cfgPath string) ([]string, error) {
if !c.IsSet("import") {
return nil, nil
}
importFiles := make([]string, 0)
// put current file into the head of list
importFiles = append(importFiles, cfgPath)
for {
includeFile := c.GetString("import")
if !filepath.IsAbs(includeFile) {
includeFile, _ = filepath.Abs(filepath.Join(filepath.Dir(cfgPath), includeFile))
}
if arr.InArray(includeFile, importFiles) {
return importFiles, fmt.Errorf("circle import config file")
}
importFiles = append(importFiles, includeFile)
c.SetConfigFile(includeFile)
err := c.ReadInConfig()
if err != nil {
return importFiles, fmt.Errorf("Fatal error merge include config file: %s ", err)
}
if !c.IsSet("import") {
break
}
}
return importFiles, nil
}
func IsEnvProd() bool {
return Config.GetString("app.env") == "prod"
}
func IsEnvDev() bool {
return Config.GetString("app.env") == "dev"
}
func GetAppName() string {
return Config.GetString("app.app_name")
}
var Config *AppConfig
func NewAppConfig(cfgPath string) *AppConfig {
cfg := &AppConfig{viper.New()}
// 设置远程配置
//err := cfg.AddRemoteProvider("etcd", "http://127.0.0.1:2379", "/yago/conf/app.toml")
//if err != nil {
// panic(err)
//}
//cfg.SetConfigType("toml")
//err = cfg.ReadRemoteConfig()
cfg.SetConfigFile(cfgPath)
err := cfg.ReadFileConfig(cfgPath)
if err != nil {
panic(err)
}
cfg.SetDefault("app.app_name", "APP")
appName := cfg.GetString("app.app_name")
appName = strings.ReplaceAll(appName, "-", "_")
cfg.SetEnvPrefix(appName)
cfg.AutomaticEnv()
replacer := strings.NewReplacer(".", "_")
cfg.SetEnvKeyReplacer(replacer)
return cfg
}
func Hostname() string {
endpoint := Config.GetString("endpoint")
if endpoint == "" {
endpoint, _ = os.Hostname()
}
return endpoint
}
func defaultCfgPath() string {
defaultDir, _ := filepath.Abs(filepath.Dir(os.Args[0]))
if runtime.GOOS == "windows" {
return fmt.Sprintf("%s\\app.toml", defaultDir)
} else {
return fmt.Sprintf("%s/app.toml", defaultDir)
}
}
var cfgPath *string
var cfgLock = new(sync.Mutex)
func initConfig() {
cfgPath = flag.String("c", defaultCfgPath(), "config file path")
_ = flag.Bool("h", false, "help")
_ = flag.Bool("help", false, "help")
flag.Parse()
Config = NewAppConfig(*cfgPath)
}
func reloadConfig() error {
cfgLock.Lock()
defer cfgLock.Unlock()
// reload file
err := Config.ReadFileConfig(*cfgPath)
if err != nil {
return err
}
// clear components config
Component.clear()
return nil
}
func getPidFile() (string, bool) {
pidfile := Config.GetString("app.pidfile")
if pidfile == "" {
return "", false
}
return pidfile, true
}