config.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. Locales []string
  66. Languages []Lang
  67. Product Product
  68. Sources []Source
  69. }
  70. func (p *metadata) setup() {
  71. if !ValidLanguages(p.Languages) {
  72. panic(fmt.Sprintf("invalid languages, optional values are: %v", AllLanguages()))
  73. }
  74. if !ValidProduct(p.Product) {
  75. panic(fmt.Sprintf("invalid product, optional values are: %v", AllProducts()))
  76. }
  77. if !ValidSources(p.Sources) {
  78. panic(fmt.Sprintf("invalid sources, optional values are: %v", AllSources()))
  79. }
  80. }
  81. func (p *metadata) GetLanguages() []Lang {
  82. if len(p.Languages) == 0 {
  83. return AllLanguages()
  84. }
  85. return p.Languages
  86. }
  87. func (p *metadata) GetProduct() Product {
  88. return p.Product
  89. }
  90. func (p *metadata) GetSources() []Source {
  91. if len(p.Sources) == 0 {
  92. return AllSources()
  93. }
  94. return p.Sources
  95. }
  96. func InitBasic(cfg *viper.Viper) *basic {
  97. var basic basic
  98. err := cfg.Unmarshal(&basic)
  99. if err != nil {
  100. panic("InitBasic err")
  101. }
  102. return &basic
  103. }
  104. func InitServer(cfg *viper.Viper) *server {
  105. var server server
  106. err := cfg.Unmarshal(&server)
  107. if err != nil {
  108. panic("InitServer err")
  109. }
  110. return &server
  111. }
  112. func InitLog(cfg *viper.Viper) *logger {
  113. var logger logger
  114. err := cfg.Unmarshal(&logger)
  115. if err != nil {
  116. panic("InitLog err")
  117. }
  118. return &logger
  119. }
  120. func InitRobot(cfg *viper.Viper) *robot {
  121. var robot robot
  122. err := cfg.Unmarshal(&robot)
  123. if err != nil {
  124. panic("InitRobot error")
  125. }
  126. return &robot
  127. }
  128. func InitPostgres(cfg *viper.Viper) *postgres {
  129. var postgres postgres
  130. err := cfg.Unmarshal(&postgres)
  131. if err != nil {
  132. panic("InitPostgres err")
  133. }
  134. postgres.setup()
  135. return &postgres
  136. }
  137. func InitMetadata(cfg *viper.Viper) *metadata {
  138. var metadata metadata
  139. err := cfg.Unmarshal(&metadata)
  140. if err != nil {
  141. panic("InitMetadata err")
  142. }
  143. metadata.setup()
  144. return &metadata
  145. }
  146. // 载入配置文件
  147. func SetupConfig(path string, addr string, mode string) {
  148. viper.SetConfigFile(path)
  149. if err := viper.ReadInConfig(); err != nil {
  150. panic(err)
  151. }
  152. cfgBasic := viper.Sub("basic")
  153. if cfgBasic == nil {
  154. panic("No found basic in the configuration")
  155. }
  156. BasicConfig = InitBasic(cfgBasic)
  157. cfgServer := viper.Sub("server")
  158. if cfgServer == nil {
  159. panic("No found server in the configuration")
  160. }
  161. ServerConfig = InitServer(cfgServer)
  162. BasicConfig.Mode = mode
  163. ServerConfig.Resource = addr
  164. cfgLog := viper.Sub("logger")
  165. if cfgLog == nil {
  166. panic("No found logger in the configuration")
  167. }
  168. LoggerConfig = InitLog(cfgLog)
  169. cfgRobot := viper.Sub("robot")
  170. if cfgLog == nil {
  171. panic("No found robot in the configuration")
  172. }
  173. RobotConfig = InitRobot(cfgRobot)
  174. cfgPostgres := viper.Sub("postgres")
  175. if cfgPostgres == nil {
  176. panic("No found postgres in the configuration")
  177. }
  178. PostgresConfig = InitPostgres(cfgPostgres)
  179. metadataApolloConfig := viper.Sub("metadata")
  180. if metadataApolloConfig == nil {
  181. panic("No found metadata in the configuration")
  182. }
  183. MetadataConfig = InitMetadata(metadataApolloConfig)
  184. }