conf.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/library/conf"
  6. "go-common/library/database/sql"
  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. "go-common/library/time"
  12. "github.com/BurntSushi/toml"
  13. )
  14. // config init.
  15. var (
  16. confPath string
  17. Conf *Config
  18. )
  19. // Config struct.
  20. type Config struct {
  21. Version string `toml:"version"`
  22. // xlog
  23. Log *log.Config
  24. // HTTPServer
  25. BM *BM
  26. // tracer
  27. Tracer *trace.Config
  28. // oss
  29. Oss *Oss
  30. // db
  31. DB *DB
  32. // reload time
  33. Reload time.Duration
  34. GitRelation map[string]string
  35. // identify
  36. Auth *permit.Config
  37. // other
  38. InvalidFrameworkFile []string
  39. TextSizeLimitList []TextSizeLimit
  40. Property *Property
  41. }
  42. // BM http.
  43. type BM struct {
  44. Inner *bm.ServerConfig
  45. Local *bm.ServerConfig
  46. }
  47. // TextSizeLimit struct.
  48. type TextSizeLimit struct {
  49. Size int64
  50. Limit float64
  51. }
  52. // Oss struct.
  53. type Oss struct {
  54. Endpoint string
  55. AccessKeyID string
  56. AccessKeySecret string
  57. Bucket string
  58. OriginDir string
  59. PublishDir string
  60. }
  61. // DB struct.
  62. type DB struct {
  63. Macross *sql.Config
  64. }
  65. // Property struct.
  66. type Property struct {
  67. Mail *struct {
  68. Host string
  69. Port int
  70. Address string
  71. Pwd string
  72. Name string
  73. }
  74. Package *struct {
  75. URLPrefix string
  76. SavePath string
  77. }
  78. }
  79. func init() {
  80. flag.StringVar(&confPath, "conf", "", "default config path")
  81. }
  82. // Init init config
  83. func Init() (err error) {
  84. if confPath != "" {
  85. _, err = toml.DecodeFile(confPath, &Conf)
  86. return
  87. }
  88. err = configCenter()
  89. return
  90. }
  91. // configCenter ugc
  92. func configCenter() (err error) {
  93. var (
  94. client *conf.Client
  95. c string
  96. ok bool
  97. )
  98. if client, err = conf.New(); err != nil {
  99. panic(err)
  100. }
  101. if c, ok = client.Toml2(); !ok {
  102. err = errors.New("load config center error")
  103. return
  104. }
  105. _, err = toml.Decode(c, &Conf)
  106. go func() {
  107. for e := range client.Event() {
  108. log.Error("get config from config center error(%v)", e)
  109. }
  110. }()
  111. return
  112. }