/
conf.go
executable file
·56 lines (48 loc) · 1.21 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
package conf
import (
"errors"
"io/ioutil"
"os"
"github.com/BurntSushi/toml"
)
// config
type config struct {
Env string `toml:"env"`
LogLevel string `toml:"log_level"`
APP app `toml:"app"`
DB db `toml:"db"`
JWT jwt `toml:"jwt"`
}
type jwt struct {
SignMethod string `toml:"sign_method"`
Secret string `toml:"secret"`
Exp int `toml:"exp"`
}
// ENVMAP map[string]string
var ENVMAP = map[string]string{"dev": "/tmp/conf/devConfig.toml", "pro": "/tmp/conf/proConfig.toml"}
// Config config
var Config config
// InitConfig initializes the app configuration by the env("dev", "pro")
func InitConfig(env string) error {
configFile, ok := ENVMAP[env]
if !ok {
return errors.New("config file err: don't have the config env")
}
// Set default config.
Config = config{
Env: env,
LogLevel: "DEBUG",
}
if _, err := os.Stat(configFile); err != nil {
return errors.New("config file err:" + err.Error())
}
configBytes, err := ioutil.ReadFile(configFile)
if err != nil {
return errors.New("config load err:" + err.Error())
}
_, err = toml.Decode(string(configBytes), &Config)
if err != nil {
return errors.New("config decode err:" + err.Error())
}
return nil
}