conf.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/library/conf"
  6. "go-common/library/database/sql"
  7. ecode "go-common/library/ecode/tip"
  8. "go-common/library/log"
  9. bm "go-common/library/net/http/blademaster"
  10. "go-common/library/net/rpc"
  11. "go-common/library/net/trace"
  12. "github.com/BurntSushi/toml"
  13. )
  14. var (
  15. confPath string
  16. // Conf init Config struct.
  17. Conf = &Config{}
  18. client *conf.Client
  19. )
  20. // Config struct.
  21. type Config struct {
  22. // Env
  23. Env string
  24. // tracer
  25. Tracer *trace.Config
  26. // xlog
  27. XLog *log.Config
  28. // http
  29. BM *HTTPServers
  30. // ecode
  31. Ecode *ecode.Config
  32. // db
  33. MySQL *MySQL
  34. // Bfs
  35. Bfs *Bfs
  36. // Feedback
  37. Feedback *Feedback
  38. // rpc
  39. LocationRPC *rpc.ClientConfig
  40. }
  41. // HTTPServers Http Servers
  42. type HTTPServers struct {
  43. Outer *bm.ServerConfig
  44. Local *bm.ServerConfig
  45. }
  46. // MySQL struct.
  47. type MySQL struct {
  48. Master *sql.Config
  49. }
  50. // Bfs struct.
  51. type Bfs struct {
  52. Addr string
  53. Bucket string
  54. Key string
  55. Secret string
  56. MaxFileSize int
  57. }
  58. // Feedback struct.
  59. type Feedback struct {
  60. ReplysNum int
  61. MaxContentSize int
  62. ImgLimit int
  63. }
  64. func init() {
  65. flag.StringVar(&confPath, "conf", "", "config path")
  66. }
  67. // Init init conf
  68. func Init() error {
  69. if confPath != "" {
  70. return local()
  71. }
  72. return remote()
  73. }
  74. func local() (err error) {
  75. _, err = toml.DecodeFile(confPath, &Conf)
  76. return
  77. }
  78. func remote() (err error) {
  79. if client, err = conf.New(); err != nil {
  80. return
  81. }
  82. if err = load(); err != nil {
  83. return
  84. }
  85. go func() {
  86. for range client.Event() {
  87. log.Info("config reload")
  88. if load() != nil {
  89. log.Error("config reload error (%v)", err)
  90. }
  91. }
  92. }()
  93. return
  94. }
  95. func load() (err error) {
  96. var (
  97. s string
  98. ok bool
  99. tmpConf *Config
  100. )
  101. if s, ok = client.Toml2(); !ok {
  102. return errors.New("load config center error")
  103. }
  104. if _, err = toml.Decode(s, &tmpConf); err != nil {
  105. return errors.New("could not decode config")
  106. }
  107. *Conf = *tmpConf
  108. return
  109. }