conf.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/library/conf"
  6. "go-common/library/database/orm"
  7. sqlx "go-common/library/database/sql"
  8. ecode "go-common/library/ecode/tip"
  9. "go-common/library/log"
  10. bm "go-common/library/net/http/blademaster"
  11. "go-common/library/net/http/blademaster/middleware/permit"
  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. Ecode *ecode.Config
  25. Auth *permit.Config
  26. HTTPServer *bm.ServerConfig
  27. HTTPClient *bm.ClientConfig
  28. DPClient *bm.ClientConfig
  29. ORM *orm.Config
  30. MySQL *sqlx.Config
  31. Log *log.Config
  32. Tracer *trace.Config
  33. Wechat *wechat
  34. Cfg *cfg
  35. }
  36. type wechat struct {
  37. Token string
  38. Secret string
  39. Username string
  40. }
  41. type cfg struct {
  42. MountDir string
  43. DiskFileExpireDay int64
  44. LimitPerTask int
  45. TaskGoroutines int
  46. PartitionsURL string
  47. UpimgURL string
  48. }
  49. func local() (err error) {
  50. _, err = toml.DecodeFile(confPath, &Conf)
  51. return
  52. }
  53. func remote() (err error) {
  54. if client, err = conf.New(); err != nil {
  55. return
  56. }
  57. err = load()
  58. return
  59. }
  60. func load() (err error) {
  61. var (
  62. s string
  63. ok bool
  64. tmpConf *Config
  65. )
  66. if s, ok = client.Toml2(); !ok {
  67. return errors.New("load config center error")
  68. }
  69. if _, err = toml.Decode(s, &tmpConf); err != nil {
  70. return errors.New("could not decode config")
  71. }
  72. *Conf = *tmpConf
  73. return
  74. }
  75. func init() {
  76. flag.StringVar(&confPath, "conf", "", "default config path")
  77. }
  78. // Init int config
  79. func Init() error {
  80. if confPath != "" {
  81. return local()
  82. }
  83. return remote()
  84. }