conf.go 3.03 KB
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"`
	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)
}