conf.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/library/cache/redis"
  6. "go-common/library/conf"
  7. "go-common/library/database/sql"
  8. "go-common/library/log"
  9. bm "go-common/library/net/http/blademaster"
  10. "go-common/library/net/trace"
  11. "go-common/library/time"
  12. "github.com/BurntSushi/toml"
  13. )
  14. var (
  15. // ConfPath local config path
  16. ConfPath string
  17. // Conf config
  18. Conf = &Config{}
  19. client *conf.Client
  20. )
  21. // Config str
  22. type Config struct {
  23. // base
  24. // channal len
  25. ChanSize int64
  26. // log
  27. Log *log.Config
  28. // http
  29. BM *bm.ServerConfig
  30. // identify
  31. App *bm.App
  32. // tracer
  33. Tracer *trace.Config
  34. // tick load pgc
  35. Tick time.Duration
  36. // orm
  37. DB *DB
  38. // http client of search
  39. HTTPClient *HTTPClient
  40. // host
  41. Host *Host
  42. // redis
  43. Redis *Redis
  44. }
  45. // DB def db struct
  46. type DB struct {
  47. Main *sql.Config
  48. Slave *sql.Config
  49. }
  50. // Redis .
  51. type Redis struct {
  52. *redis.Config
  53. UpRatingExpire time.Duration
  54. }
  55. // HTTPClient http client
  56. type HTTPClient struct {
  57. Read *bm.ClientConfig
  58. }
  59. // Host http host
  60. type Host struct {
  61. AccountURI string
  62. ArchiveURI string
  63. UperURI string
  64. }
  65. func init() {
  66. flag.StringVar(&ConfPath, "conf", "", "default config path")
  67. }
  68. // Init init conf
  69. func Init() (err error) {
  70. if ConfPath != "" {
  71. return local()
  72. }
  73. return remote()
  74. }
  75. func local() (err error) {
  76. _, err = toml.DecodeFile(ConfPath, &Conf)
  77. return
  78. }
  79. func remote() (err error) {
  80. if client, err = conf.New(); err != nil {
  81. return
  82. }
  83. if err = load(); err != nil {
  84. return
  85. }
  86. go func() {
  87. for range client.Event() {
  88. log.Info("config reload")
  89. if load() != nil {
  90. log.Error("config reload error (%v)", err)
  91. }
  92. }
  93. }()
  94. return
  95. }
  96. func load() (err error) {
  97. var (
  98. s string
  99. ok bool
  100. tmpConf *Config
  101. )
  102. if s, ok = client.Toml2(); !ok {
  103. return errors.New("load config center error")
  104. }
  105. if _, err = toml.Decode(s, &tmpConf); err != nil {
  106. return errors.New("could not decode config")
  107. }
  108. *Conf = *tmpConf
  109. return
  110. }