forked from jacexh/ultron
-
Notifications
You must be signed in to change notification settings - Fork 4
/
config.go
71 lines (59 loc) · 1.59 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
package ultron
import (
"fmt"
"os"
"path/filepath"
"github.com/jacexh/multiconfig"
)
type (
Option struct {
Server ServerOption
Logger LoggerOption
}
ServerOption struct {
HTTPAddr string `default:":2017" yaml:"http_addr,omitempty" json:"http_addr,omitempty" toml:"http_addr"`
GRPCAddr string `default:":2021" yaml:"grpc_addr,omitempty" json:"grpc_addr,omitempty" toml:"grpc_addr"`
}
LoggerOption struct {
Level string `default:"info" yaml:"level,omitempty" json:"level,omitempty" toml:"level"`
FileName string `yaml:"filename,omitempty" json:"filename,omitempty" toml:"filename"`
MaxSize int `default:"100" yaml:"max_size,omitempty" json:"max_size,omitempty" toml:"max_size"`
MaxBackups int `default:"30" yaml:"max_backups,omitempty" json:"max_backups,omitempty" toml:"max_backups"`
}
)
var (
configFileBaseName = "config"
configFileFormat = "yml"
searchInPaths []string = []string{"."}
)
var (
loadedOption *Option
)
func findInDir(dir string, file string) string {
fp := filepath.Join(dir, file)
fi, err := os.Stat(fp)
if err == nil && !fi.IsDir() {
return fp
}
return ""
}
func findConfigFile() string {
fp := fmt.Sprintf("%s.%s", configFileBaseName, configFileFormat)
for _, dir := range searchInPaths {
if path := findInDir(dir, fp); path != "" {
return path
}
}
return ""
}
func loadConfig() *Option {
f := findConfigFile()
opt := new(Option)
loader := multiconfig.NewWithPathAndEnvPrefix(f, "ULTRON")
loader.MustLoad(opt)
buildLogger(opt.Logger) // todo
return opt
}
func init() {
loadedOption = loadConfig()
}