config.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. MaxBackups int //最多保留文件个数
  36. MaxAge int //保留旧文件的最大天数
  37. Compress bool //是否压缩/归档旧文件
  38. Stdout bool
  39. }
  40. type robot struct {
  41. Info string
  42. Dev string
  43. Prod string
  44. }
  45. // 数据库配置
  46. type postgres struct {
  47. Ip string
  48. Port int
  49. Username string
  50. Password string
  51. Name string
  52. }
  53. func (p *postgres) setup() {
  54. raw, err := hex.DecodeString(p.Password)
  55. if err != nil {
  56. panic("db password is invalid")
  57. }
  58. pwd, err := DBPwdDecrypt(raw)
  59. if err != nil {
  60. panic("db password is invalid")
  61. }
  62. p.Password = string(pwd)
  63. }
  64. // metadata配置
  65. type metadata struct {
  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 InitApplication() *application {
  97. return &application{
  98. Host: viper.GetString("host"),
  99. Port: viper.GetInt("port"),
  100. Mode: viper.GetString("mode"),
  101. }
  102. }
  103. func InitBasic(cfg *viper.Viper) *basic {
  104. var basic basic
  105. err := cfg.Unmarshal(&basic)
  106. if err != nil {
  107. panic("InitBasic err")
  108. }
  109. return &basic
  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) {
  147. viper.SetConfigFile(path)
  148. if err := viper.ReadInConfig(); err != nil {
  149. panic(err)
  150. }
  151. ApplicationConfig = InitApplication()
  152. cfgBasic := viper.Sub("basic")
  153. if cfgBasic == nil {
  154. panic("No found basic in the configuration")
  155. }
  156. BasicConfig = InitBasic(cfgBasic)
  157. cfgLog := viper.Sub("logger")
  158. if cfgLog == nil {
  159. panic("No found logger in the configuration")
  160. }
  161. LoggerConfig = InitLog(cfgLog)
  162. cfgRobot := viper.Sub("robot")
  163. if cfgLog == nil {
  164. panic("No found robot in the configuration")
  165. }
  166. RobotConfig = InitRobot(cfgRobot)
  167. cfgPostgres := viper.Sub("postgres")
  168. if cfgPostgres == nil {
  169. panic("No found postgres in the configuration")
  170. }
  171. PostgresConfig = InitPostgres(cfgPostgres)
  172. metadataApolloConfig := viper.Sub("metadata")
  173. if metadataApolloConfig == nil {
  174. panic("No found metadata in the configuration")
  175. }
  176. MetadataConfig = InitMetadata(metadataApolloConfig)
  177. }