conf.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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/rpc"
  11. "go-common/library/net/rpc/warden"
  12. "go-common/library/queue/databus"
  13. "go-common/library/queue/databus/databusutil"
  14. xtime "go-common/library/time"
  15. "github.com/BurntSushi/toml"
  16. )
  17. var (
  18. confPath string
  19. // Conf config.
  20. Conf = &Config{}
  21. client *conf.Client
  22. )
  23. // Config conf.
  24. type Config struct {
  25. Env string
  26. Sms *Sms
  27. Databus *databus.Config
  28. LoginDatabus *databus.Config
  29. ExpDatabus *databus.Config
  30. Xlog *log.Config
  31. DB *DB
  32. CoinJob *CoinJob
  33. AccountRPC *warden.ClientConfig
  34. MemRPC *warden.ClientConfig
  35. ArchiveRPC *rpc.ClientConfig
  36. CoinRPC *rpc.ClientConfig
  37. // BM
  38. BM *bm.ServerConfig
  39. // redis
  40. Redis *redis.Config
  41. Databusutil *databusutil.Config
  42. }
  43. // CoinJob job conf.
  44. type CoinJob struct {
  45. // award conf
  46. StartTime int64
  47. Start bool
  48. LoginExpire xtime.Duration
  49. }
  50. // Sms sms conf.
  51. type Sms struct {
  52. Phone string
  53. Token string
  54. }
  55. // DB db conf
  56. type DB struct {
  57. Coin *sql.Config
  58. }
  59. func init() {
  60. flag.StringVar(&confPath, "conf", "", "default config path")
  61. }
  62. // Init init conf.
  63. func Init() (err error) {
  64. if confPath != "" {
  65. return local()
  66. }
  67. return remote()
  68. }
  69. func local() (err error) {
  70. _, err = toml.DecodeFile(confPath, &Conf)
  71. return
  72. }
  73. func remote() (err error) {
  74. if client, err = conf.New(); err != nil {
  75. return
  76. }
  77. err = load()
  78. return
  79. }
  80. func load() (err error) {
  81. var (
  82. s string
  83. ok bool
  84. tmpConf *Config
  85. )
  86. if s, ok = client.Toml2(); !ok {
  87. return errors.New("load config center error")
  88. }
  89. if _, err = toml.Decode(s, &tmpConf); err != nil {
  90. return errors.New("could not decode config")
  91. }
  92. *Conf = *tmpConf
  93. return
  94. }