conf.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/library/conf"
  6. ecode "go-common/library/ecode/tip"
  7. "go-common/library/log"
  8. bm "go-common/library/net/http/blademaster"
  9. "go-common/library/net/trace"
  10. "go-common/library/queue/databus"
  11. xtime "go-common/library/time"
  12. "github.com/BurntSushi/toml"
  13. )
  14. // global var
  15. var (
  16. confPath string
  17. client *conf.Client
  18. // Conf config
  19. Conf = &Config{}
  20. )
  21. // Config config set
  22. type Config struct {
  23. // base
  24. // elk
  25. Log *log.Config
  26. // http
  27. BM *HTTPServers
  28. // tracer
  29. Tracer *trace.Config
  30. // ecode
  31. Ecode *ecode.Config
  32. DonwloadHost string
  33. UploadHost string
  34. UploadAdminHost string
  35. RoutineCount int
  36. HTTPClient *bm.ClientConfig // call upload admin
  37. HTTPTimeout xtime.Duration
  38. Databus *databus.Config
  39. AIYellowing AIYellowing
  40. Threshold *Threshold
  41. }
  42. // AIYellowing describe AI upload config
  43. type AIYellowing struct {
  44. ExceptBuckets []string
  45. Producer *databus.Config
  46. Consumer *databus.Config
  47. }
  48. // Threshold .
  49. type Threshold struct {
  50. Sex int
  51. Politics int
  52. Blood int
  53. Violent int
  54. }
  55. // HTTPServers Http Servers
  56. type HTTPServers struct {
  57. Outer *bm.ServerConfig
  58. Inner *bm.ServerConfig
  59. Local *bm.ServerConfig
  60. }
  61. func init() {
  62. flag.StringVar(&confPath, "conf", "", "default config path")
  63. }
  64. // Init init conf
  65. func Init() error {
  66. if confPath != "" {
  67. return local()
  68. }
  69. return remote()
  70. }
  71. func local() (err error) {
  72. _, err = toml.DecodeFile(confPath, &Conf)
  73. return
  74. }
  75. func remote() (err error) {
  76. if client, err = conf.New(); err != nil {
  77. return
  78. }
  79. if err = load(); err != nil {
  80. return
  81. }
  82. go func() {
  83. for range client.Event() {
  84. log.Info("config reload")
  85. if load() != nil {
  86. log.Error("config reload error (%v)", err)
  87. }
  88. }
  89. }()
  90. return
  91. }
  92. func load() (err error) {
  93. var (
  94. s string
  95. ok bool
  96. tmpConf *Config
  97. )
  98. if s, ok = client.Toml2(); !ok {
  99. return errors.New("load config center error")
  100. }
  101. if _, err = toml.Decode(s, &tmpConf); err != nil {
  102. return errors.New("could not decode config")
  103. }
  104. *Conf = *tmpConf
  105. return
  106. }