conf.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "github.com/BurntSushi/toml"
  6. "go-common/library/conf"
  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/http/blademaster/middleware/auth"
  11. "go-common/library/net/http/blademaster/middleware/verify"
  12. "go-common/library/net/rpc/liverpc"
  13. "go-common/library/net/rpc/warden"
  14. "go-common/library/net/trace"
  15. )
  16. var (
  17. confPath string
  18. client *conf.Client
  19. // Conf config
  20. Conf = &Config{}
  21. )
  22. // Config .
  23. type Config struct {
  24. Log *log.Config
  25. BM *bm.ServerConfig
  26. Verify *verify.Config
  27. Tracer *trace.Config
  28. Ecode *ecode.Config
  29. ResourceClient *warden.ClientConfig
  30. LiveRpc map[string]*liverpc.ClientConfig
  31. GrpcCli *warden.ClientConfig
  32. Auth *auth.Config
  33. HttpClient *bm.ClientConfig
  34. }
  35. func init() {
  36. flag.StringVar(&confPath, "conf", "", "default config path")
  37. }
  38. // Init init conf
  39. func Init() error {
  40. if confPath != "" {
  41. return local()
  42. }
  43. return remote()
  44. }
  45. func local() (err error) {
  46. _, err = toml.DecodeFile(confPath, &Conf)
  47. return
  48. }
  49. func remote() (err error) {
  50. if client, err = conf.New(); err != nil {
  51. return
  52. }
  53. if err = load(); err != nil {
  54. return
  55. }
  56. go func() {
  57. for range client.Event() {
  58. log.Info("config reload")
  59. if load() != nil {
  60. log.Error("config reload error (%v)", err)
  61. }
  62. }
  63. }()
  64. return
  65. }
  66. func load() (err error) {
  67. var (
  68. s string
  69. ok bool
  70. tmpConf *Config
  71. )
  72. if s, ok = client.Toml2(); !ok {
  73. return errors.New("load config center error")
  74. }
  75. if _, err = toml.Decode(s, &tmpConf); err != nil {
  76. return errors.New("could not decode config")
  77. }
  78. *Conf = *tmpConf
  79. return
  80. }
  81. const (
  82. // APPKey to call main site api
  83. APPKey = "fb06a25c6338edbc"
  84. )