conf.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/library/conf"
  6. "go-common/library/database/sql"
  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/rpc/warden"
  12. "go-common/library/net/trace"
  13. "go-common/library/queue/databus"
  14. "github.com/BurntSushi/toml"
  15. )
  16. // global var
  17. var (
  18. confPath string
  19. client *conf.Client
  20. // Conf config
  21. Conf = &Config{}
  22. )
  23. // Config config set
  24. type Config struct {
  25. Ecode *ecode.Config
  26. Log *log.Config
  27. HTTPServer *bm.ServerConfig
  28. HTTPClient *bm.ClientConfig
  29. PushRPC *warden.ClientConfig
  30. Tracer *trace.Config
  31. Auth *auth.Config
  32. MySQL *sql.Config
  33. ReportPub *databus.Config
  34. CallbackPub *databus.Config
  35. Push *push
  36. }
  37. type push struct {
  38. CallbackSize int
  39. CallbackChanLen int
  40. CallbackGoroutines int
  41. }
  42. func init() {
  43. flag.StringVar(&confPath, "conf", "", "default config path")
  44. }
  45. // Init init conf
  46. func Init() error {
  47. if confPath != "" {
  48. return local()
  49. }
  50. return remote()
  51. }
  52. func local() (err error) {
  53. _, err = toml.DecodeFile(confPath, &Conf)
  54. return
  55. }
  56. func remote() (err error) {
  57. if client, err = conf.New(); err != nil {
  58. return
  59. }
  60. err = load()
  61. return
  62. }
  63. func load() (err error) {
  64. var (
  65. s string
  66. ok bool
  67. tmpConf *Config
  68. )
  69. if s, ok = client.Toml2(); !ok {
  70. return errors.New("load config center error")
  71. }
  72. if _, err = toml.Decode(s, &tmpConf); err != nil {
  73. return errors.New("could not decode config")
  74. }
  75. *Conf = *tmpConf
  76. return
  77. }