package common import ( "fmt" "github.com/aiscrm/redisgo" "github.com/jinzhu/gorm" _ "github.com/jinzhu/gorm/dialects/mysql" "gopkg" "gopkg.in/yaml.v2" "io/ioutil" "log" "lzyd-message-api/wrapper" "os" "path" ) //serverModel get server information from config.yml type serverModel struct { Mode string `yaml:"mode"` // run mode Host string `yaml:"host"` // server host Port string `yaml:"port"` // server port } //logModel get log information from config.yml type logModel struct { Open bool `yaml:"open"` Mode string `yaml:"mode"` Path string `yaml:"file"` Level string `yaml:"level"` } type configModel struct { Server *serverModel `yaml:"server"` Log *logModel `yaml:"log"` } // LoadConfigInformation load config information for application func LoadConfigInformation(configPath string) (err error) { var ( filePath string wr string ) if configPath == "" { wr, _ = os.Getwd() wr = path.Join(wr, "conf") } else { wr = configPath } filePath = path.Join(wr, "config.yml") configData, err := ioutil.ReadFile(filePath) if err != nil { fmt.Printf(" config file read failed: %s", err) os.Exit(-1) } err = yaml.Unmarshal(configData, &ConfigInfo) if err != nil { fmt.Printf(" config parse failed: %s", err) os.Exit(-1) } // server information ServerInfo = ConfigInfo.Server // log information LoggerInfo = ConfigInfo.Log // log config load logPath := LoggerInfo.Path //log path logLevel := LoggerInfo.Level // log level isDebug := true // log mode if ServerInfo.Mode == "release" { isDebug = false } wrapper.LoggerInit(logLevel, logPath, isDebug) log.SetFlags(log.Lmicroseconds | log.Lshortfile | log.LstdFlags) // init db err = initDatabase("yuedong") if err != nil || DB == nil { fmt.Printf("database connect failed: %s", err) os.Exit(-1) } // init redis err = initRedisCacher() if err != nil || Cacher == nil { fmt.Printf("Redis connect failed: %s", err) os.Exit(-1) } // 初始化jwt中间件 err = initJwtMiddleware(DB, Cacher) if err != nil { fmt.Printf("jwt middleware load error: %s", err) os.Exit(-1) } return } func initDatabase(dbname string) error { var err error //内网地址:rm-bp1cbq48ara8ex330.mysql.rds.aliyuncs.com //外网地址:rm-bp1cbq48ara8ex3309o.mysql.rds.aliyuncs.com DB, err = gopkg.OpenMysqlWithConfig(gopkg.MysqlConfig{ Host: "rm-bp1cbq48ara8ex330.mysql.rds.aliyuncs.com", Port: "3306", UserName: "yuedong", Password: "Yuedong2020", Database: dbname, }) return err } func initRedisCacher() error { //内网地址:r-bp15zxt342rl5rhrx9.redis.rds.aliyuncs.com //外网地址:r-bp15zxt342rl5rhrx9pd.redis.rds.aliyuncs.com var err error Cacher, err = redisgo.New(redisgo.Options{ // prod //Addr: "r-bp15zxt342rl5rhrx9.redis.rds.aliyuncs.com:6379", //Password: "AwCDT09m*SportRedis", // dev Addr: "127.0.0.1:6379", Password: "", Db: 0, }) return err } func initJwtMiddleware(db *gorm.DB, redis *redisgo.Cacher) error { return nil }