面包店
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.3 KiB

9 maanden geleden
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. CurlDebug bool
  12. SrvAddr string
  13. RedisAddr string
  14. DB *DBCfg
  15. Log *LogCfg
  16. ArkID *ArkIDCfg
  17. Admin *AdminCfg
  18. Official *OfficialCfg
  19. WxappletFilepath *WxappletFilepathCfg
  20. Local bool
  21. AppComm *AppCommCfg
  22. )
  23. //初始化配置文件,将cfg.yml读入到内存
  24. func InitCfg() {
  25. //用指定的名称、默认值、使用信息注册一个string类型flag。
  26. path := flag.String("c", "etc/cfg.yml", "config file")
  27. //解析命令行参数写入注册的flag里。
  28. //解析之后,flag的值可以直接使用。
  29. flag.Parse()
  30. var (
  31. c []byte
  32. err error
  33. conf *Config
  34. )
  35. if c, err = ioutil.ReadFile(*path); err != nil {
  36. panic(err)
  37. }
  38. //yaml.Unmarshal反序列化映射到Config
  39. if err = yaml.Unmarshal(c, &conf); err != nil {
  40. panic(err)
  41. }
  42. //数据读入内存
  43. Prd = conf.Prd
  44. Debug = conf.Debug
  45. Local = conf.Local
  46. CurlDebug = conf.CurlDebug
  47. DB = &conf.DB
  48. Log = &conf.Log
  49. ArkID = &conf.ArkID
  50. RedisAddr = conf.RedisAddr
  51. SrvAddr = conf.SrvAddr
  52. Admin = &conf.Admin
  53. Official = &conf.Official
  54. WxappletFilepath = &conf.WxappletFilepath
  55. AppComm = &conf.AppComm
  56. }