conf.go
3.26 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
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
package common
import (
"encoding/json"
"fmt"
"gopkg.in/yaml.v3"
"io/ioutil"
"pro2d/common/logger"
"pro2d/common/snow"
"strings"
)
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"`
DBName string `yaml:"dbname"`
}
type SConf struct {
ID string `yaml:"id"`
Name string `yaml:"name"`
IP string `yaml:"ip"`
Port int `yaml:"port"`
Encipher bool `yaml:"encipher"`
DebugPort int `yaml:"debugport"`
MongoConf *MongoConf `yaml:"mongo"`
RedisConf *RedisConf `yaml:"redis"`
WorkerPoolSize int `yaml:"pool_size"`
PluginPath string `yaml:"plugin_path"`
}
type LogConsole struct {
Level string `yaml:"level" json:"level"`
Color bool `yaml:"color" json:"color"`
}
type LogFile struct {
Level string `yaml:"level" json:"level"`
Daily bool `yaml:"daily" json:"daily"`
Maxlines int `yaml:"maxlines" json:"maxlines"`
Maxsize int `yaml:"maxsize" json:"maxsize"`
Maxdays int `yaml:"maxdays" json:"maxdays"`
Append bool `yaml:"append" json:"append"`
Permit string `yaml:"permit" json:"permit"`
}
type LogConn struct {
Net string `yaml:"net" json:"net"`
Addr string `yaml:"addr" json:"addr"`
Level string `yaml:"level" json:"level"`
Reconnect bool `yaml:"reconnect" json:"reconnect"`
ReconnectOnMsg bool `yaml:"reconnectOnMsg" json:"reconnectOnMsg"`
}
type LogConf struct {
TimeFormat string `yaml:"TimeFormat" json:"TimeFormat"`
LogConsole *LogConsole `yaml:"Console" json:"Console"`
LogFile *LogFile `yaml:"File" json:"File"`
LogConn *LogConn `yaml:"Conn" json:"Conn"`
}
type TestClient struct {
Ip string `yaml:"ip"`
Port int `yaml:"port"`
Count int `yaml:"count"`
}
type ServerConf struct {
ID string `yaml:"id"`
Name string `yaml:"name"`
WorkerID int64 `yaml:"workerid"`
DatacenterID int64 `yaml:"datacenterid"`
AccountConf *SConf `yaml:"server_account"`
GameConf *SConf `yaml:"server_game"`
LogConf *LogConf `yaml:"logconf" json:"logconf"`
TestClient *TestClient `yaml:"test_client"`
Etcd *Etcd `yaml:"etcd"`
}
var (
GlobalConf ServerConf
SnowFlack *snow.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
}
c, err := json.Marshal(&GlobalConf.LogConf)
if err != nil {
fmt.Errorf("log conf %v", err)
return
}
//初始化日志
err = logger.SetLogger(string(c), strings.ToLower(GlobalConf.GameConf.Name))
if err != nil {
fmt.Errorf("log conf %v", err)
return
}
//初始化雪花算法
SnowFlack = snow.NewSnowflake(GlobalConf.WorkerID, GlobalConf.DatacenterID)
}