conf.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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/rpc"
  11. "go-common/library/net/rpc/warden"
  12. "go-common/library/net/trace"
  13. "github.com/BurntSushi/toml"
  14. )
  15. var (
  16. // config
  17. confPath string
  18. client *conf.Client
  19. // Conf .
  20. Conf = &Config{}
  21. )
  22. // Config def.
  23. type Config struct {
  24. Auth *permit.Config
  25. HTTPServer *bm.ServerConfig
  26. HTTPClient *bm.ClientConfig
  27. ORM *orm.Config
  28. Log *log.Config
  29. Tracer *trace.Config
  30. Host *Host
  31. // tag rpc client
  32. TagRPC *rpc.ClientConfig
  33. //article rpc client
  34. ArticlrRPC *rpc.ClientConfig
  35. ArcClient *warden.ClientConfig
  36. AccClient *warden.ClientConfig
  37. }
  38. // Host remote host
  39. type Host struct {
  40. API string
  41. }
  42. func local() (err error) {
  43. _, err = toml.DecodeFile(confPath, &Conf)
  44. return
  45. }
  46. func remote() (err error) {
  47. if client, err = conf.New(); err != nil {
  48. return
  49. }
  50. if err = load(); err != nil {
  51. return
  52. }
  53. go func() {
  54. for range client.Event() {
  55. log.Info("config reload")
  56. if load() != nil {
  57. log.Error("config reload error (%v)", err)
  58. }
  59. }
  60. }()
  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. }
  78. func init() {
  79. flag.StringVar(&confPath, "conf", "", "default config path")
  80. }
  81. // Init int config
  82. func Init() error {
  83. if confPath != "" {
  84. return local()
  85. }
  86. return remote()
  87. }