config.go 4.1 KB

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