conf.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/library/conf"
  6. "go-common/library/database/orm"
  7. "go-common/library/database/sql"
  8. "go-common/library/log"
  9. bm "go-common/library/net/http/blademaster"
  10. "go-common/library/net/http/blademaster/middleware/permit"
  11. xtime "go-common/library/time"
  12. "go-common/library/database/hbase.v2"
  13. "github.com/BurntSushi/toml"
  14. )
  15. // global var
  16. var (
  17. confPath string
  18. client *conf.Client
  19. // Conf config
  20. Conf = &Config{}
  21. )
  22. // HTTPClient http client
  23. type HTTPClient struct {
  24. Read *bm.ClientConfig
  25. Write *bm.ClientConfig
  26. }
  27. // Config config set
  28. type Config struct {
  29. // base
  30. Log *log.Config
  31. // http
  32. BM *bm.ServerConfig
  33. // auth
  34. Auth *permit.Config
  35. // MySQL
  36. MySQL *sql.Config
  37. //httpClient
  38. HTTPClient *HTTPClient
  39. //ORM
  40. ORM *orm.Config
  41. //bfs hosts
  42. BfsDownloadHost string
  43. BfsUpdateHost string
  44. BfsDeleteHost string
  45. // bfs
  46. Bfs *Bfs
  47. // Hbase
  48. Hbase *HBaseConfig
  49. }
  50. // Bfs .
  51. type Bfs struct {
  52. BfsURL string
  53. WaterMarkURL string
  54. ImageGenURL string
  55. TimeOut xtime.Duration
  56. WmTimeOut xtime.Duration
  57. ImageGenTimeOut xtime.Duration
  58. }
  59. func init() {
  60. flag.StringVar(&confPath, "conf", "", "default config path")
  61. }
  62. // Init init conf
  63. func Init() error {
  64. if confPath != "" {
  65. return local()
  66. }
  67. return remote()
  68. }
  69. func local() (err error) {
  70. _, err = toml.DecodeFile(confPath, &Conf)
  71. return
  72. }
  73. func remote() (err error) {
  74. if client, err = conf.New(); err != nil {
  75. return
  76. }
  77. if err = load(); err != nil {
  78. return
  79. }
  80. go func() {
  81. for range client.Event() {
  82. log.Info("config reload")
  83. if load() != nil {
  84. log.Error("config reload error (%v)", err)
  85. }
  86. }
  87. }()
  88. return
  89. }
  90. func load() (err error) {
  91. var (
  92. s string
  93. ok bool
  94. tmpConf *Config
  95. )
  96. if s, ok = client.Toml2(); !ok {
  97. return errors.New("load config center error")
  98. }
  99. if _, err = toml.Decode(s, &tmpConf); err != nil {
  100. return errors.New("could not decode config")
  101. }
  102. *Conf = *tmpConf
  103. return
  104. }
  105. // Item describe bfs bucket accessKey and accessSecret
  106. type Item struct {
  107. Name string // bucket name
  108. KeyID string // accessKey
  109. KeySecret string // accessSecret
  110. }
  111. // HBaseConfig ...
  112. type HBaseConfig struct {
  113. *hbase.Config
  114. WriteTimeout xtime.Duration
  115. ReadTimeout xtime.Duration
  116. }