config.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. package common
  2. import (
  3. "fmt"
  4. )
  5. import (
  6. "github.com/spf13/viper"
  7. )
  8. //配置文件
  9. var (
  10. ApplicationConfig = new(application)
  11. BasicConfig = new(basic)
  12. LoggerConfig = new(logger)
  13. RobotConfig = new(robot)
  14. PostgresConfig = new(postgres)
  15. MetadataConfig = new(metadata)
  16. )
  17. // 应用配置
  18. type application struct {
  19. Host string
  20. Port int
  21. Mode string
  22. }
  23. func (p *application) Addr() string {
  24. return fmt.Sprintf("%s:%d", p.Host, p.Port)
  25. }
  26. type basic struct {
  27. Jwt string
  28. }
  29. // 日志配置
  30. type logger struct {
  31. LogDir string // 日志文件夹路径
  32. LogLevel string // 日志打印等级
  33. MaxSize int //在进行切割之前,日志文件的最大大小(以MB为单位)
  34. MaxAge int //保留旧文件的最大天数
  35. Compress bool //是否压缩/归档旧文件
  36. Stdout bool
  37. }
  38. type robot struct {
  39. Info string
  40. Dev string
  41. Prod string
  42. }
  43. // 数据库配置
  44. type postgres struct {
  45. Ip string
  46. Port int
  47. Username string
  48. Password string
  49. Name string
  50. }
  51. // metadata配置
  52. type metadata struct {
  53. Languages []Lang
  54. Product Product
  55. Sources []Source
  56. }
  57. func (p *metadata) setup() {
  58. if !ValidLanguages(p.Languages) {
  59. panic(fmt.Sprintf("invalid languages, optional values are: %v", AllLanguages()))
  60. }
  61. if !ValidProduct(p.Product) {
  62. panic(fmt.Sprintf("invalid product, optional values are: %v", AllProducts()))
  63. }
  64. if !ValidSources(p.Sources) {
  65. panic(fmt.Sprintf("invalid sources, optional values are: %v", AllSources()))
  66. }
  67. }
  68. func (p *metadata) GetLanguages() []Lang {
  69. if len(p.Languages) == 0 {
  70. return AllLanguages()
  71. }
  72. return p.Languages
  73. }
  74. func (p *metadata) GetProduct() Product {
  75. return p.Product
  76. }
  77. func (p *metadata) GetSources() []Source {
  78. if len(p.Sources) == 0 {
  79. return AllSources()
  80. }
  81. return p.Sources
  82. }
  83. func InitApplication() *application {
  84. return &application{
  85. Host: viper.GetString("host"),
  86. Port: viper.GetInt("port"),
  87. Mode: viper.GetString("mode"),
  88. }
  89. }
  90. func InitBasic(cfg *viper.Viper) *basic {
  91. var basic basic
  92. err := cfg.Unmarshal(&basic)
  93. if err != nil {
  94. panic("InitBasic err")
  95. }
  96. return &basic
  97. }
  98. func InitLog(cfg *viper.Viper) *logger {
  99. var logger logger
  100. err := cfg.Unmarshal(&logger)
  101. if err != nil {
  102. panic("InitLog err")
  103. }
  104. return &logger
  105. }
  106. func InitRobot(cfg *viper.Viper) *robot {
  107. var robot robot
  108. err := cfg.Unmarshal(&robot)
  109. if err != nil {
  110. panic("InitRobot error")
  111. }
  112. return &robot
  113. }
  114. func InitPostgres(cfg *viper.Viper) *postgres {
  115. var postgres postgres
  116. err := cfg.Unmarshal(&postgres)
  117. if err != nil {
  118. panic("InitPostgres err")
  119. }
  120. return &postgres
  121. }
  122. func InitMetadata(cfg *viper.Viper) *metadata {
  123. var metadata metadata
  124. err := cfg.Unmarshal(&metadata)
  125. if err != nil {
  126. panic("InitMetadata err")
  127. }
  128. metadata.setup()
  129. return &metadata
  130. }
  131. // 载入配置文件
  132. func SetupConfig(path string) {
  133. viper.SetConfigFile(path)
  134. if err := viper.ReadInConfig(); err != nil {
  135. panic(err)
  136. }
  137. ApplicationConfig = InitApplication()
  138. cfgBasic := viper.Sub("basic")
  139. if cfgBasic == nil {
  140. panic("No found basic in the configuration")
  141. }
  142. BasicConfig = InitBasic(cfgBasic)
  143. cfgLog := viper.Sub("logger")
  144. if cfgLog == nil {
  145. panic("No found logger in the configuration")
  146. }
  147. LoggerConfig = InitLog(cfgLog)
  148. cfgRobot := viper.Sub("robot")
  149. if cfgLog == nil {
  150. panic("No found robot in the configuration")
  151. }
  152. RobotConfig = InitRobot(cfgRobot)
  153. cfgPostgres := viper.Sub("postgres")
  154. if cfgPostgres == nil {
  155. panic("No found postgres in the configuration")
  156. }
  157. PostgresConfig = InitPostgres(cfgPostgres)
  158. metadataApolloConfig := viper.Sub("metadata")
  159. if metadataApolloConfig == nil {
  160. panic("No found metadata in the configuration")
  161. }
  162. MetadataConfig = InitMetadata(metadataApolloConfig)
  163. }