conf.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/library/net/http/blademaster/middleware/auth"
  6. "go-common/library/net/rpc"
  7. "go-common/library/cache/memcache"
  8. "go-common/library/cache/redis"
  9. "go-common/library/conf"
  10. "go-common/library/database/sql"
  11. ecode "go-common/library/ecode/tip"
  12. "go-common/library/log"
  13. bm "go-common/library/net/http/blademaster"
  14. "go-common/library/net/http/blademaster/middleware/verify"
  15. "go-common/library/net/rpc/liverpc"
  16. "go-common/library/net/trace"
  17. "github.com/BurntSushi/toml"
  18. "go-common/library/log/infoc"
  19. "go-common/library/net/rpc/warden"
  20. )
  21. var (
  22. confPath string
  23. client *conf.Client
  24. // Conf config
  25. Conf = &Config{}
  26. )
  27. const (
  28. // APPKey to call main site api
  29. APPKey = "fb06a25c6338edbc"
  30. )
  31. const (
  32. // MainInnerHostHTTP api host
  33. MainInnerHostHTTP = "http://api.bilibili.co"
  34. )
  35. // Config .
  36. type Config struct {
  37. Log *log.Config
  38. BM *bm.ServerConfig
  39. Verify *verify.Config
  40. Tracer *trace.Config
  41. Redis *redis.Config
  42. Memcache *memcache.Config
  43. MySQL *sql.Config
  44. Ecode *ecode.Config
  45. LiveRpc map[string]*liverpc.ClientConfig
  46. HTTPClient *bm.ClientConfig
  47. HistoryRPC *rpc.ClientConfig
  48. Auth *auth.Config
  49. Warden *warden.ClientConfig
  50. Infoc *Infoc
  51. Host *Host
  52. AccountRPC *rpc.ClientConfig
  53. }
  54. // Infoc .
  55. type Infoc struct {
  56. CapsuleInfoc *infoc.Config
  57. }
  58. // Host prc host
  59. type Host struct {
  60. LiveRpc string
  61. }
  62. func init() {
  63. flag.StringVar(&confPath, "conf", "", "default config path")
  64. }
  65. // Init init conf
  66. func Init() error {
  67. if confPath != "" {
  68. return local()
  69. }
  70. return remote()
  71. }
  72. func local() (err error) {
  73. _, err = toml.DecodeFile(confPath, &Conf)
  74. return
  75. }
  76. func remote() (err error) {
  77. if client, err = conf.New(); err != nil {
  78. return
  79. }
  80. if err = load(); err != nil {
  81. return
  82. }
  83. go func() {
  84. for range client.Event() {
  85. log.Info("config reload")
  86. if load() != nil {
  87. log.Error("config reload error (%v)", err)
  88. }
  89. }
  90. }()
  91. return
  92. }
  93. func load() (err error) {
  94. var (
  95. s string
  96. ok bool
  97. tmpConf *Config
  98. )
  99. if s, ok = client.Toml2(); !ok {
  100. return errors.New("load config center error")
  101. }
  102. if _, err = toml.Decode(s, &tmpConf); err != nil {
  103. return errors.New("could not decode config")
  104. }
  105. *Conf = *tmpConf
  106. return
  107. }