conf.go 1.99 KB
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)
	}
}