config.go 4.1 KB

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