conf.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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/trace"
  11. "github.com/BurntSushi/toml"
  12. )
  13. // these are config global variables
  14. var (
  15. confPath string
  16. Conf = &Config{}
  17. client *conf.Client
  18. )
  19. // Config is the model for parse workflow config
  20. type Config struct {
  21. Log *log.Config
  22. HTTPServer *bm.ServerConfig
  23. HTTPClient *bm.ClientConfig
  24. Auth *permit.Config
  25. Tracer *trace.Config
  26. DB *orm.Config
  27. Sms *sms
  28. }
  29. type sms struct {
  30. TplPsMax int
  31. MountDir string
  32. }
  33. func init() {
  34. flag.StringVar(&confPath, "conf", "", "default config path")
  35. }
  36. // Init config
  37. func Init() (err error) {
  38. if confPath != "" {
  39. return local()
  40. }
  41. return remote()
  42. }
  43. func local() (err error) {
  44. _, err = toml.DecodeFile(confPath, &Conf)
  45. return
  46. }
  47. func remote() (err error) {
  48. if client, err = conf.New(); err != nil {
  49. return
  50. }
  51. err = load()
  52. return
  53. }
  54. func load() (err error) {
  55. var (
  56. s string
  57. ok bool
  58. tmpConf *Config
  59. )
  60. if s, ok = client.Toml2(); !ok {
  61. return errors.New("load config center error")
  62. }
  63. if _, err = toml.Decode(s, &tmpConf); err != nil {
  64. return errors.New("could not decode config")
  65. }
  66. *Conf = *tmpConf
  67. return
  68. }