config.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package common
  2. import (
  3. "fmt"
  4. "github.com/aiscrm/redisgo"
  5. "github.com/jinzhu/gorm"
  6. _ "github.com/jinzhu/gorm/dialects/mysql"
  7. "gopkg"
  8. "gopkg.in/yaml.v2"
  9. "io/ioutil"
  10. "log"
  11. "lzyd-message-api/wrapper"
  12. "os"
  13. "path"
  14. )
  15. //serverModel get server information from config.yml
  16. type serverModel struct {
  17. Mode string `yaml:"mode"` // run mode
  18. Host string `yaml:"host"` // server host
  19. Port string `yaml:"port"` // server port
  20. }
  21. //logModel get log information from config.yml
  22. type logModel struct {
  23. Open bool `yaml:"open"`
  24. Mode string `yaml:"mode"`
  25. Path string `yaml:"file"`
  26. Level string `yaml:"level"`
  27. }
  28. type configModel struct {
  29. Server *serverModel `yaml:"server"`
  30. Log *logModel `yaml:"log"`
  31. }
  32. // LoadConfigInformation load config information for application
  33. func LoadConfigInformation(configPath string) (err error) {
  34. var (
  35. filePath string
  36. wr string
  37. )
  38. if configPath == "" {
  39. wr, _ = os.Getwd()
  40. wr = path.Join(wr, "conf")
  41. } else {
  42. wr = configPath
  43. }
  44. filePath = path.Join(wr, "config.yml")
  45. configData, err := ioutil.ReadFile(filePath)
  46. if err != nil {
  47. fmt.Printf(" config file read failed: %s", err)
  48. os.Exit(-1)
  49. }
  50. err = yaml.Unmarshal(configData, &ConfigInfo)
  51. if err != nil {
  52. fmt.Printf(" config parse failed: %s", err)
  53. os.Exit(-1)
  54. }
  55. // server information
  56. ServerInfo = ConfigInfo.Server
  57. // log information
  58. LoggerInfo = ConfigInfo.Log
  59. // log config load
  60. logPath := LoggerInfo.Path //log path
  61. logLevel := LoggerInfo.Level // log level
  62. isDebug := true // log mode
  63. if ServerInfo.Mode == "release" {
  64. isDebug = false
  65. }
  66. wrapper.LoggerInit(logLevel, logPath, isDebug)
  67. log.SetFlags(log.Lmicroseconds | log.Lshortfile | log.LstdFlags)
  68. // init db
  69. err = initDatabase("yuedong")
  70. if err != nil || DB == nil {
  71. fmt.Printf("database connect failed: %s", err)
  72. os.Exit(-1)
  73. }
  74. // init redis
  75. err = initRedisCacher()
  76. if err != nil || Cacher == nil {
  77. fmt.Printf("Redis connect failed: %s", err)
  78. os.Exit(-1)
  79. }
  80. // 初始化jwt中间件
  81. err = initJwtMiddleware(DB, Cacher)
  82. if err != nil {
  83. fmt.Printf("jwt middleware load error: %s", err)
  84. os.Exit(-1)
  85. }
  86. return
  87. }
  88. func initDatabase(dbname string) error {
  89. var err error
  90. //内网地址:rm-bp1cbq48ara8ex330.mysql.rds.aliyuncs.com
  91. //外网地址:rm-bp1cbq48ara8ex3309o.mysql.rds.aliyuncs.com
  92. DB, err = gopkg.OpenMysqlWithConfig(gopkg.MysqlConfig{
  93. Host: "rm-bp1cbq48ara8ex330.mysql.rds.aliyuncs.com",
  94. Port: "3306",
  95. UserName: "yuedong",
  96. Password: "Yuedong2020",
  97. Database: dbname,
  98. })
  99. return err
  100. }
  101. func initRedisCacher() error {
  102. //内网地址:r-bp15zxt342rl5rhrx9.redis.rds.aliyuncs.com
  103. //外网地址:r-bp15zxt342rl5rhrx9pd.redis.rds.aliyuncs.com
  104. var err error
  105. Cacher, err = redisgo.New(redisgo.Options{
  106. // prod
  107. //Addr: "r-bp15zxt342rl5rhrx9.redis.rds.aliyuncs.com:6379",
  108. //Password: "AwCDT09m*SportRedis",
  109. // dev
  110. Addr: "127.0.0.1:6379",
  111. Password: "",
  112. Db: 0,
  113. })
  114. return err
  115. }
  116. func initJwtMiddleware(db *gorm.DB, redis *redisgo.Cacher) error {
  117. return nil
  118. }