config.go 4.1 KB

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