conf.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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/orm"
  8. "go-common/library/log"
  9. bm "go-common/library/net/http/blademaster"
  10. "go-common/library/net/http/blademaster/middleware/permit"
  11. "go-common/library/net/trace"
  12. "github.com/BurntSushi/toml"
  13. )
  14. var (
  15. // config
  16. confPath string
  17. client *conf.Client
  18. // Conf .
  19. Conf = &Config{}
  20. )
  21. // Config def.
  22. type Config struct {
  23. // base
  24. // auth
  25. Auth *permit.Config
  26. // http
  27. BM *bm.ServerConfig
  28. // db
  29. ORM *orm.Config
  30. // log
  31. XLog *log.Config
  32. // tracer
  33. Tracer *trace.Config
  34. // cfg
  35. Cfg *Cfg
  36. // bfs
  37. Bfs *Bfs
  38. // nas
  39. Nas *Bfs
  40. // HTTPClient .
  41. HTTPClient *bm.ClientConfig
  42. // Redis
  43. Redis *Redis
  44. }
  45. // Redis redis
  46. type Redis struct {
  47. *redis.Config
  48. }
  49. // Bfs reprensents the bfs config
  50. type Bfs struct {
  51. Key string
  52. Secret string
  53. Host string
  54. Timeout int
  55. OldURL string
  56. NewURL string
  57. }
  58. // Cfg def.
  59. type Cfg struct {
  60. HistoryVer int
  61. Storage string // NAS or BFS
  62. Filetypes []string // allowed file type to upload
  63. }
  64. func local() (err error) {
  65. _, err = toml.DecodeFile(confPath, &Conf)
  66. return
  67. }
  68. func remote() (err error) {
  69. if client, err = conf.New(); err != nil {
  70. return
  71. }
  72. if err = load(); err != nil {
  73. return
  74. }
  75. go func() {
  76. for range client.Event() {
  77. log.Info("config reload")
  78. if load() != nil {
  79. log.Error("config reload error (%v)", err)
  80. }
  81. }
  82. }()
  83. return
  84. }
  85. func load() (err error) {
  86. var (
  87. s string
  88. ok bool
  89. tmpConf *Config
  90. )
  91. if s, ok = client.Toml2(); !ok {
  92. return errors.New("load config center error")
  93. }
  94. if _, err = toml.Decode(s, &tmpConf); err != nil {
  95. return errors.New("could not decode config")
  96. }
  97. *Conf = *tmpConf
  98. return
  99. }
  100. func init() {
  101. flag.StringVar(&confPath, "conf", "", "default config path")
  102. }
  103. // Init int config
  104. func Init() error {
  105. if confPath != "" {
  106. return local()
  107. }
  108. return remote()
  109. }