conf.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/library/cache/memcache"
  6. "go-common/library/cache/redis"
  7. "go-common/library/conf"
  8. "go-common/library/database/elastic"
  9. "go-common/library/database/hbase.v2"
  10. "go-common/library/database/orm"
  11. "go-common/library/database/sql"
  12. ecode "go-common/library/ecode/tip"
  13. "go-common/library/log"
  14. bm "go-common/library/net/http/blademaster"
  15. "go-common/library/net/http/blademaster/middleware/permit"
  16. "go-common/library/net/rpc"
  17. "go-common/library/net/rpc/warden"
  18. "go-common/library/net/trace"
  19. "go-common/library/queue/databus"
  20. "go-common/library/time"
  21. "github.com/BurntSushi/toml"
  22. )
  23. // global var
  24. var (
  25. confPath string
  26. client *conf.Client
  27. // Conf config
  28. Conf = &Config{}
  29. )
  30. // Config config set
  31. type Config struct {
  32. Log *log.Config
  33. BM *bm.ServerConfig
  34. Tracer *trace.Config
  35. ORM *ORM
  36. ORMRead *ORM
  37. Ecode *ecode.Config
  38. Host *Host
  39. HTTPClient *HTTPClient
  40. HBase *hbase.Config
  41. Auth *permit.Config
  42. FHByOPHBase *hbase.Config
  43. FHByMidHBase *hbase.Config
  44. FacePriBFS *BFS
  45. FaceBFS *BFS
  46. ManagerReport *databus.Config
  47. ExpMsgDatabus *databus.Config
  48. RPCClient *RPC
  49. ES *elastic.Config
  50. Realname *Realname
  51. ReviewNotify *ReviewNotify
  52. Redis *redis.Config
  53. Memcache *memcache.Config
  54. // block
  55. AccountNotify *databus.Config
  56. BlockProperty *Property
  57. BlockMemcache *memcache.Config
  58. BlockMySQL *sql.Config
  59. }
  60. // Host is Host config
  61. type Host struct {
  62. Message string
  63. Passport string
  64. Merak string
  65. }
  66. // Property .
  67. type Property struct {
  68. BlackHouseURL string
  69. MSGURL string
  70. TelURL string
  71. MailURL string
  72. }
  73. // RPC config
  74. type RPC struct {
  75. Coin *rpc.ClientConfig
  76. Account *warden.ClientConfig
  77. Figure *rpc.ClientConfig
  78. Member *rpc.ClientConfig
  79. Spy *rpc.ClientConfig
  80. Relation *rpc.ClientConfig
  81. }
  82. // ORM is database config
  83. type ORM struct {
  84. Member *orm.Config
  85. MemberRead *orm.Config
  86. Account *orm.Config
  87. }
  88. // BFS bfs config
  89. type BFS struct {
  90. Timeout time.Duration
  91. MaxFileSize int
  92. Bucket string
  93. URL string
  94. Key string
  95. Secret string
  96. }
  97. // Realname conf
  98. type Realname struct {
  99. ImageURLTemplate string
  100. DataDir string
  101. RsaPub []byte
  102. RsaPriv []byte
  103. }
  104. // HTTPClient http client
  105. type HTTPClient struct {
  106. Read *bm.ClientConfig
  107. Passport *bm.ClientConfig
  108. }
  109. // ReviewNotify notify users
  110. type ReviewNotify struct {
  111. Users []string
  112. }
  113. func init() {
  114. flag.StringVar(&confPath, "conf", "", "default config path")
  115. }
  116. // Init init conf
  117. func Init() error {
  118. if confPath != "" {
  119. return local()
  120. }
  121. return remote()
  122. }
  123. func local() (err error) {
  124. _, err = toml.DecodeFile(confPath, &Conf)
  125. return
  126. }
  127. func remote() (err error) {
  128. if client, err = conf.New(); err != nil {
  129. return
  130. }
  131. if err = load(); err != nil {
  132. return
  133. }
  134. go func() {
  135. for range client.Event() {
  136. log.Info("config reload")
  137. if load() != nil {
  138. log.Error("config reload error (%v)", err)
  139. }
  140. }
  141. }()
  142. return
  143. }
  144. func load() (err error) {
  145. var (
  146. s string
  147. ok bool
  148. tmpConf *Config
  149. )
  150. if s, ok = client.Toml2(); !ok {
  151. return errors.New("load config center error")
  152. }
  153. if _, err = toml.Decode(s, &tmpConf); err != nil {
  154. return errors.New("could not decode config")
  155. }
  156. pub, priv := loadRealnameKey()
  157. tmpConf.Realname.RsaPriv = []byte(priv)
  158. tmpConf.Realname.RsaPub = []byte(pub)
  159. *Conf = *tmpConf
  160. return
  161. }
  162. func loadRealnameKey() (string, string) {
  163. priv, ok := client.Value("realname.rsa.priv")
  164. if !ok {
  165. panic("Failed to load realname private key")
  166. }
  167. pub, ok := client.Value("realname.rsa.pub")
  168. if !ok {
  169. panic("Failed to load realname pubic key")
  170. }
  171. return pub, priv
  172. }