conf.go
1.99 KB
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
package conf
import (
"fmt"
lumberjack "gopkg.in/natefinch/lumberjack.v2"
"gopkg.in/yaml.v3"
"io/ioutil"
"pro2d/components/db"
"pro2d/utils"
)
type RedisConf struct {
Address string `json:"address"`
Auth string `json:"auth"`
DB int `json:"db"`
}
type Etcd struct {
Endpoints []string `json:"endpoints"`
DialTimeout int `json:"dialtimeout"`
}
type MongoConf struct {
User string `yaml:"user"`
Password string `yaml:"password"`
Host string `yaml:"host"`
Port int `yaml:"port"`
TimeOut int `yaml:"timeout"`
MaxNum int `yaml:"maxnum"`
}
type TLS struct {
Status bool `yaml:"status"`
Key string `yaml:"key"`
Pem string `yaml:"pem"`
}
type SConf struct {
ID string `yaml:"id"`
Name string `yaml:"name"`
IP string `yaml:"ip"`
Port int `yaml:"port"`
DBName string `yaml:"dbname"`
}
type ServerConf struct {
ID string `yaml:"id"`
Name string `yaml:"name"`
WorkerID int64 `yaml:"workerid"`
DatacenterID int64 `yaml:"datacenterid"`
MongoConf *MongoConf `yaml:"mongo"`
TLS *TLS `yaml:"tls"`
AccountConf *SConf `yaml:"server_account"`
GameConf *SConf `yaml:"server_game"`
RedisConf *RedisConf `yaml:"redis"`
LogConf *lumberjack.Logger `json:"logconf"`
Etcd *Etcd `yaml:"etcd"`
}
var(
GlobalConf ServerConf
SnowFlack *utils.Snowflake
)
func init() {
configFile, err := ioutil.ReadFile("conf/conf.yaml")
if err != nil {
fmt.Printf("conf faild: %v", err)
return
}
//初始化配置
if err = yaml.Unmarshal(configFile, &GlobalConf); err != nil {
fmt.Printf("yaml unmarshal faild: %v", err)
return
}
//初始化日志
utils.InitLogger(GlobalConf.LogConf)
//初始化雪花算法
SnowFlack = utils.NewSnowflake(GlobalConf.WorkerID, GlobalConf.DatacenterID)
err = db.Connect(GlobalConf.MongoConf.User, GlobalConf.MongoConf.Password, GlobalConf.MongoConf.Host, GlobalConf.MongoConf.Port, GlobalConf.MongoConf.MaxNum, GlobalConf.MongoConf.TimeOut)
if err != nil {
utils.Sugar.Errorf("connect db err: %v", err)
}
}