conf.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/library/cache/memcache"
  6. "go-common/library/conf"
  7. "go-common/library/database/orm"
  8. "go-common/library/log"
  9. bm "go-common/library/net/http/blademaster"
  10. "go-common/library/net/rpc/warden"
  11. "go-common/library/net/trace"
  12. xtime "go-common/library/time"
  13. "github.com/BurntSushi/toml"
  14. )
  15. // Config .
  16. type Config struct {
  17. App *bm.App
  18. Log *log.Config
  19. Tracer *trace.Config
  20. ORM *orm.Config
  21. // Uname load ticker
  22. AppTicker xtime.Duration
  23. // mc
  24. Memcache *memcache.Config
  25. WardenServer *warden.ServerConfig
  26. }
  27. var (
  28. confPath string
  29. client *conf.Client
  30. // Conf config
  31. Conf = &Config{}
  32. )
  33. // init() .
  34. func init() {
  35. flag.StringVar(&confPath, "conf", "", "config path")
  36. }
  37. // Init .
  38. func Init() (err error) {
  39. if confPath != "" {
  40. return local()
  41. }
  42. return remote()
  43. }
  44. // local .
  45. func local() (err error) {
  46. _, err = toml.DecodeFile(confPath, &Conf)
  47. return
  48. }
  49. // remote .
  50. func remote() (err error) {
  51. if client, err = conf.New(); err != nil {
  52. return
  53. }
  54. if err = load(); err != nil {
  55. return
  56. }
  57. go func() {
  58. for range client.Event() {
  59. log.Info("config reload")
  60. }
  61. }()
  62. return
  63. }
  64. // load .
  65. func load() (err error) {
  66. var (
  67. s string
  68. ok bool
  69. tmpConf *Config
  70. )
  71. if s, ok = client.Toml2(); !ok {
  72. return errors.New("load config center error")
  73. }
  74. if _, err = toml.Decode(s, &tmpConf); err != nil {
  75. return errors.New("could not decode config")
  76. }
  77. *Conf = *tmpConf
  78. return
  79. }