package conf import ( "fmt" "os" "github.com/BurntSushi/toml" "github.com/bwmarrin/snowflake" "github.com/fatih/color" ) type appcfg struct { Redis struct { Ip string `json:"ip"` Port int `json:"port"` } `json:"redis"` Queue struct { Ip string `json:"ip"` Port int `json:"port"` } `json:"queue"` Mysql struct { Ip string `json:"ip"` Port int `json:"port"` User string `json:"user"` Password string `json:"password"` Db string `json:"db"` } `json:"mysql"` Mongo struct { Ip string `json:"ip"` Port int `json:"port"` } `json:"mongo"` Global struct { Gamename string `json:"gamename"` Version string `json:"version"` } `json:"global"` } // user:password@tcp(host:port)/dbname?charset=utf8mb4 // 用户名:密码@tcp(主机:端口)/数据库名称?charset=utf8mb4 const _dsn = "%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=true&loc=Local" const _addr = "%s:%d" const _mongo_addr = "mongodb://%s:%d" var ( defConfig = "./conf/dev.toml" //配置文件路径,方便测试 prodConfig = "./conf/prod.toml" //配置文件路径,方便测试 Config *appcfg //运行配置实体 APPNAME string APPVERSION string ISTEST bool USEOTS bool DSN string RedisAddr string QueueAddr string MongoAddr string ObtenationIterations int = 3 node, _ = snowflake.NewNode(1) IP string = "192.168.3.235" PORT int = 9080 ) func GetNode() *snowflake.Node { return node } func GetDsn() string { return DSN } func GetRedisAddr() string { return RedisAddr } func GetQueueAddr() string { return QueueAddr } func GetConfig() *appcfg { return Config } func GetMongoAddr() string { return MongoAddr } func init() { fmt.Println("conf init") _, err := os.Stat(defConfig) color.Red("Stat ok = %v", err) if err != nil { _, err1 := os.Stat(prodConfig) if err1 != nil { ISTEST = false } } else { ISTEST = true } // ISTEST = false if ISTEST { color.Red("当前为测试环境") var err error Config, err = initDevCfg() if err != nil { panic(err) } APPNAME = Config.Global.Gamename color.Green("APPNAME=%s", APPNAME) APPVERSION = Config.Global.Version color.Green("APPVERSION=%s", APPVERSION) color.Green("Mysql=%s", Config.Mysql) DSN = fmt.Sprintf(_dsn, Config.Mysql.User, Config.Mysql.Password, Config.Mysql.Ip, Config.Mysql.Port, Config.Mysql.Db) RedisAddr = fmt.Sprintf(_addr, Config.Redis.Ip, Config.Redis.Port) QueueAddr = fmt.Sprintf(_addr, Config.Queue.Ip, Config.Queue.Port) MongoAddr = fmt.Sprintf(_mongo_addr, Config.Mongo.Ip, Config.Mongo.Port) } else { color.Red("当前为生产环境") var err error Config, err = initProdCfg() if err != nil { panic(err) } APPNAME = Config.Global.Gamename APPVERSION = Config.Global.Version DSN = fmt.Sprintf(_dsn, Config.Mysql.User, Config.Mysql.Password, Config.Mysql.Ip, Config.Mysql.Port, Config.Mysql.Db) RedisAddr = fmt.Sprintf(_addr, Config.Redis.Ip, Config.Redis.Port) QueueAddr = fmt.Sprintf(_addr, Config.Queue.Ip, Config.Queue.Port) MongoAddr = fmt.Sprintf(_mongo_addr, Config.Mongo.Ip, Config.Mongo.Port) } } func initDevCfg() (*appcfg, error) { app := &appcfg{} _, err := toml.DecodeFile(defConfig, &app) if err != nil { return nil, err } return app, nil } func initProdCfg() (*appcfg, error) { app := &appcfg{} _, err := toml.DecodeFile(prodConfig, &app) if err != nil { return nil, err } return app, nil }