config.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. package common
  2. import (
  3. "fmt"
  4. )
  5. import (
  6. "github.com/spf13/viper"
  7. )
  8. //配置文件
  9. var (
  10. ApplicationConfig = new(application)
  11. BasicConfig = new(basic)
  12. LoggerConfig = new(logger)
  13. RobotConfig = new(robot)
  14. PostgresConfig = new(postgres)
  15. MetadataConfig = new(metadata)
  16. )
  17. // 应用配置
  18. type application struct {
  19. Host string
  20. Port int
  21. Mode string
  22. }
  23. func (p *application) Addr() string {
  24. return fmt.Sprintf("%s:%d", p.Host, p.Port)
  25. }
  26. type basic struct {
  27. Jwt string
  28. }
  29. // 日志配置
  30. type logger struct {
  31. LogDir string // 日志文件夹路径
  32. LogLevel string // 日志打印等级
  33. MaxSize int //在进行切割之前,日志文件的最大大小(以MB为单位)
  34. MaxAge int //保留旧文件的最大天数
  35. Compress bool //是否压缩/归档旧文件
  36. Stdout bool
  37. }
  38. type robot struct {
  39. Info string
  40. Dev string
  41. Prod string
  42. }
  43. // 数据库配置
  44. type postgres struct {
  45. Ip string
  46. Port int
  47. Username string
  48. Password string
  49. Name string
  50. }
  51. // metadata配置
  52. type metadata struct {
  53. Languages []Lang
  54. }
  55. func (p *metadata) setup() {
  56. if !ValidLanguages(p.Languages) {
  57. panic(fmt.Sprintf("invalid languages, optional values are: %v", AllLanguages()))
  58. }
  59. }
  60. func (p *metadata) GetLanguages() []Lang {
  61. if len(p.Languages) == 0 {
  62. return AllLanguages()
  63. }
  64. return p.Languages
  65. }
  66. func InitApplication() *application {
  67. return &application{
  68. Host: viper.GetString("host"),
  69. Port: viper.GetInt("port"),
  70. Mode: viper.GetString("mode"),
  71. }
  72. }
  73. func InitBasic(cfg *viper.Viper) *basic {
  74. var basic basic
  75. err := cfg.Unmarshal(&basic)
  76. if err != nil {
  77. panic("InitBasic err")
  78. }
  79. return &basic
  80. }
  81. func InitLog(cfg *viper.Viper) *logger {
  82. var logger logger
  83. err := cfg.Unmarshal(&logger)
  84. if err != nil {
  85. panic("InitLog err")
  86. }
  87. return &logger
  88. }
  89. func InitRobot(cfg *viper.Viper) *robot {
  90. var robot robot
  91. err := cfg.Unmarshal(&robot)
  92. if err != nil {
  93. panic("InitRobot error")
  94. }
  95. return &robot
  96. }
  97. func InitPostgres(cfg *viper.Viper) *postgres {
  98. var postgres postgres
  99. err := cfg.Unmarshal(&postgres)
  100. if err != nil {
  101. panic("InitPostgres err")
  102. }
  103. return &postgres
  104. }
  105. func InitMetadata(cfg *viper.Viper) *metadata {
  106. var metadata metadata
  107. err := cfg.Unmarshal(&metadata)
  108. if err != nil {
  109. panic("InitMetadata err")
  110. }
  111. metadata.setup()
  112. return &metadata
  113. }
  114. // 载入配置文件
  115. func SetupConfig(path string) {
  116. viper.SetConfigFile(path)
  117. if err := viper.ReadInConfig(); err != nil {
  118. panic(err)
  119. }
  120. ApplicationConfig = InitApplication()
  121. cfgBasic := viper.Sub("basic")
  122. if cfgBasic == nil {
  123. panic("No found basic in the configuration")
  124. }
  125. BasicConfig = InitBasic(cfgBasic)
  126. cfgLog := viper.Sub("logger")
  127. if cfgLog == nil {
  128. panic("No found logger in the configuration")
  129. }
  130. LoggerConfig = InitLog(cfgLog)
  131. cfgRobot := viper.Sub("robot")
  132. if cfgLog == nil {
  133. panic("No found robot in the configuration")
  134. }
  135. RobotConfig = InitRobot(cfgRobot)
  136. cfgPostgres := viper.Sub("postgres")
  137. if cfgPostgres == nil {
  138. panic("No found postgres in the configuration")
  139. }
  140. PostgresConfig = InitPostgres(cfgPostgres)
  141. metadataApolloConfig := viper.Sub("metadata")
  142. if metadataApolloConfig == nil {
  143. panic("No found metadata in the configuration")
  144. }
  145. MetadataConfig = InitMetadata(metadataApolloConfig)
  146. }