conf.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/library/conf"
  6. "go-common/library/database/orm"
  7. "go-common/library/log"
  8. bm "go-common/library/net/http/blademaster"
  9. "go-common/library/net/http/blademaster/middleware/permit"
  10. "go-common/library/net/http/blademaster/middleware/verify"
  11. "go-common/library/net/trace"
  12. xtime "go-common/library/time"
  13. "github.com/BurntSushi/toml"
  14. )
  15. // Config .
  16. type Config struct {
  17. App *bm.App
  18. Log *log.Config
  19. Verify *verify.Config
  20. Tracer *trace.Config
  21. ORM *orm.Config
  22. Permit *permit.Config2
  23. Cfg *cfg
  24. // Uname load ticker
  25. UnameTicker xtime.Duration
  26. }
  27. // HTTPServers is http servers .
  28. type HTTPServers struct {
  29. Inner *bm.ServerConfig
  30. }
  31. type cfg struct {
  32. RankGroupMaxPs int
  33. }
  34. var (
  35. confPath string
  36. client *conf.Client
  37. // Conf config
  38. Conf = &Config{}
  39. )
  40. func init() {
  41. flag.StringVar(&confPath, "conf", "", "config path")
  42. }
  43. // Init .
  44. func Init() (err error) {
  45. if confPath != "" {
  46. return local()
  47. }
  48. return remote()
  49. }
  50. func local() (err error) {
  51. _, err = toml.DecodeFile(confPath, &Conf)
  52. return
  53. }
  54. func remote() (err error) {
  55. if client, err = conf.New(); err != nil {
  56. return
  57. }
  58. if err = load(); err != nil {
  59. return
  60. }
  61. go func() {
  62. for range client.Event() {
  63. log.Info("config reload")
  64. }
  65. }()
  66. return
  67. }
  68. func load() (err error) {
  69. var (
  70. s string
  71. ok bool
  72. tmpConf *Config
  73. )
  74. if s, ok = client.Toml2(); !ok {
  75. return errors.New("load config center error")
  76. }
  77. if _, err = toml.Decode(s, &tmpConf); err != nil {
  78. return errors.New("could not decode config")
  79. }
  80. *Conf = *tmpConf
  81. return
  82. }