conf.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/library/cache/redis"
  6. "go-common/library/conf"
  7. "go-common/library/database/sql"
  8. "go-common/library/log"
  9. bm "go-common/library/net/http/blademaster"
  10. "go-common/library/net/trace"
  11. xtime "go-common/library/time"
  12. "github.com/BurntSushi/toml"
  13. )
  14. // Conf global variable.
  15. var (
  16. Conf = &Config{}
  17. client *conf.Client
  18. confPath string
  19. )
  20. // Config struct of conf.
  21. type Config struct {
  22. // base
  23. // log
  24. Log *log.Config
  25. // db
  26. DB *DB
  27. // redis
  28. Redis *Redis
  29. // http
  30. BM *HTTPServers
  31. // http client
  32. HTTPClient HTTPClient
  33. // tracer
  34. Tracer *trace.Config
  35. //stat
  36. Stat int
  37. Stra int
  38. }
  39. // DB db config.
  40. type DB struct {
  41. Ab *sql.Config
  42. }
  43. // Redis conf.
  44. type Redis struct {
  45. *redis.Config
  46. Expire xtime.Duration
  47. VerifyCdTimes xtime.Duration
  48. }
  49. // HTTPServers Http Servers
  50. type HTTPServers struct {
  51. Inner *bm.ServerConfig
  52. Outer *bm.ServerConfig
  53. }
  54. // HTTPClient config
  55. type HTTPClient struct {
  56. Read *bm.ClientConfig
  57. Write *bm.ClientConfig
  58. }
  59. func outer() (err error) {
  60. _, err = toml.DecodeFile(confPath, &Conf)
  61. return
  62. }
  63. func remote() (err error) {
  64. if client, err = conf.New(); err != nil {
  65. return
  66. }
  67. if err = load(); err != nil {
  68. return
  69. }
  70. go func() {
  71. for range client.Event() {
  72. log.Info("config event")
  73. }
  74. }()
  75. return
  76. }
  77. func load() (err error) {
  78. var (
  79. s string
  80. ok bool
  81. tmpConf *Config
  82. )
  83. if s, ok = client.Value("open-abtest.toml"); !ok {
  84. return errors.New("load config center error")
  85. }
  86. if _, err = toml.Decode(s, &tmpConf); err != nil {
  87. return errors.New("could not decode config")
  88. }
  89. *Conf = *tmpConf
  90. return
  91. }
  92. func init() {
  93. flag.StringVar(&confPath, "conf", "", "default config path")
  94. }
  95. // Init int config
  96. func Init() error {
  97. if confPath != "" {
  98. return outer()
  99. }
  100. return remote()
  101. }