config.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. package common
  2. import (
  3. "encoding/hex"
  4. "fmt"
  5. )
  6. import (
  7. "github.com/spf13/viper"
  8. )
  9. //配置文件
  10. var (
  11. ApplicationConfig = new(application)
  12. BasicConfig = new(basic)
  13. LoggerConfig = new(logger)
  14. RobotConfig = new(robot)
  15. PostgresConfig = new(postgres)
  16. MetadataConfig = new(metadata)
  17. )
  18. // 应用配置
  19. type application struct {
  20. Host string
  21. Port int
  22. Mode string
  23. }
  24. func (p *application) Addr() string {
  25. return fmt.Sprintf("%s:%d", p.Host, p.Port)
  26. }
  27. type basic struct {
  28. Jwt string
  29. }
  30. // 日志配置
  31. type logger struct {
  32. LogDir string // 日志文件夹路径
  33. LogLevel string // 日志打印等级
  34. MaxSize int //在进行切割之前,日志文件的最大大小(以MB为单位)
  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 InitApplication() *application {
  96. return &application{
  97. Host: viper.GetString("host"),
  98. Port: viper.GetInt("port"),
  99. Mode: viper.GetString("mode"),
  100. }
  101. }
  102. func InitBasic(cfg *viper.Viper) *basic {
  103. var basic basic
  104. err := cfg.Unmarshal(&basic)
  105. if err != nil {
  106. panic("InitBasic err")
  107. }
  108. return &basic
  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) {
  146. viper.SetConfigFile(path)
  147. if err := viper.ReadInConfig(); err != nil {
  148. panic(err)
  149. }
  150. ApplicationConfig = InitApplication()
  151. cfgBasic := viper.Sub("basic")
  152. if cfgBasic == nil {
  153. panic("No found basic in the configuration")
  154. }
  155. BasicConfig = InitBasic(cfgBasic)
  156. cfgLog := viper.Sub("logger")
  157. if cfgLog == nil {
  158. panic("No found logger in the configuration")
  159. }
  160. LoggerConfig = InitLog(cfgLog)
  161. cfgRobot := viper.Sub("robot")
  162. if cfgLog == nil {
  163. panic("No found robot in the configuration")
  164. }
  165. RobotConfig = InitRobot(cfgRobot)
  166. cfgPostgres := viper.Sub("postgres")
  167. if cfgPostgres == nil {
  168. panic("No found postgres in the configuration")
  169. }
  170. PostgresConfig = InitPostgres(cfgPostgres)
  171. metadataApolloConfig := viper.Sub("metadata")
  172. if metadataApolloConfig == nil {
  173. panic("No found metadata in the configuration")
  174. }
  175. MetadataConfig = InitMetadata(metadataApolloConfig)
  176. }