蛋蛋星球 后台端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

init_cfg.go 1.0 KiB

2 months ago
1 month ago
2 months ago
1 month ago
2 months ago
1 month ago
2 months ago
1 month ago
2 months ago
1 month ago
2 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package cfg
  2. import (
  3. "flag"
  4. "io/ioutil"
  5. "gopkg.in/yaml.v2"
  6. )
  7. // 配置文件数据,全局变量
  8. var (
  9. Debug bool
  10. Prd bool
  11. SrvAddr string
  12. SmartCanteenPay string
  13. RedisAddr string
  14. RedisPassword string
  15. DB *DBCfg
  16. MQ *MQCfg
  17. Log *LogCfg
  18. )
  19. // 初始化配置文件,将cfg.yml读入到内存
  20. func InitCfg() {
  21. //用指定的名称、默认值、使用信息注册一个string类型flag。
  22. path := flag.String("c", "etc/cfg.yml", "config file")
  23. //解析命令行参数写入注册的flag里。
  24. //解析之后,flag的值可以直接使用。
  25. flag.Parse()
  26. var (
  27. c []byte
  28. err error
  29. conf *Config
  30. )
  31. if c, err = ioutil.ReadFile(*path); err != nil {
  32. panic(err)
  33. }
  34. //yaml.Unmarshal反序列化映射到Config
  35. if err = yaml.Unmarshal(c, &conf); err != nil {
  36. panic(err)
  37. }
  38. //数据读入内存
  39. Prd = conf.Prd
  40. Debug = conf.Debug
  41. DB = &conf.DB
  42. Log = &conf.Log
  43. RedisAddr = conf.RedisAddr
  44. RedisPassword = conf.RedisPassword
  45. SrvAddr = conf.SrvAddr
  46. MQ = &conf.MQ
  47. }