/
setting.go
79 lines (66 loc) · 1.5 KB
/
setting.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
package setting
import (
"fmt"
"io/ioutil"
"gopkg.in/yaml.v2"
)
type config struct {
Version string `yaml:"version"`
Databases struct {
Mysql struct {
Host string `yaml:"host"`
Port int `yaml:"port"`
User string `yaml:"user"`
Password string `yaml:"password"`
Db string `yaml:"db"`
}
Redis struct {
Addr string `yaml:"addr"`
Password string `yaml:"password"`
Db int `yaml:"db"`
}
Elastic struct {
Hosts []string `yaml:"hosts"`
}
}
}
const DefaultConf = "conf/app.yml"
var (
AppVer string
cf *config
CustomConf = DefaultConf
HTTPPort = "9090"
HTTPAddr = "localhost"
RedisAddr = "localhost:6379"
RedisPass = ""
RedisDB = 0
EsHosts = []string{"http://127.0.0.1:9200"}
MysqlHost = "localhost"
MysqlPort = 3306
MysqlUser = "root"
MysqlPassword = ""
MysqlDB = ""
)
func NewContext() {
yf, err := ioutil.ReadFile(CustomConf)
if err != nil {
panic(fmt.Sprintf("get conf yaml file with err #%v", err))
}
cf = new(config)
err = yaml.Unmarshal(yf, cf)
if err != nil {
panic(fmt.Sprintf("yaml.Unmarshal: %v", err))
}
// redis
RedisAddr = cf.Databases.Redis.Addr
RedisPass = cf.Databases.Redis.Password
RedisDB = cf.Databases.Redis.Db
// elasticsearch
EsHosts = cf.Databases.Elastic.Hosts
// mysql
MysqlHost = cf.Databases.Mysql.Host
MysqlPort = cf.Databases.Mysql.Port
MysqlUser = cf.Databases.Mysql.User
MysqlPassword = cf.Databases.Mysql.Password
MysqlDB = cf.Databases.Mysql.Db
}