config.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package config
  4. import (
  5. "fmt"
  6. "path/filepath"
  7. "strings"
  8. "github.com/spf13/viper"
  9. )
  10. type LogConfig struct {
  11. Path string
  12. Level string
  13. MaxSize int
  14. MaxBackups int
  15. MaxAge int
  16. Stacktrace bool
  17. }
  18. type MysqlConfig struct {
  19. User string
  20. Password string
  21. Addr string
  22. DB string
  23. Charset string
  24. MaxIdle int
  25. MaxConn int
  26. LogMode bool
  27. }
  28. type RedisConfig struct {
  29. Addrs []string
  30. Password string
  31. DB int
  32. PoolSize int
  33. MinIdleConns int
  34. MaxRetries int
  35. Cluster bool
  36. }
  37. type ElasticConfig struct {
  38. Addrs []string
  39. Sniff bool
  40. }
  41. type ThirdPartNode struct {
  42. Host string
  43. AppKey string
  44. AppSecret string
  45. }
  46. type AliPartNode struct {
  47. AesKey string
  48. }
  49. type ThirdPartyConfig struct {
  50. Ali AliPartNode
  51. }
  52. type RPCNode struct {
  53. ServiceName string
  54. ServicePort int
  55. ServiceIp string
  56. MysqlDb string
  57. RedisDb int
  58. LogLevel string
  59. LogStacktrace bool
  60. }
  61. type KeepaliveConfig struct {
  62. ClientTime int
  63. ClientTimeout int
  64. ServerTime int
  65. ServerTimeout int
  66. ServerMiniTime int
  67. }
  68. type OssConfig struct {
  69. BrandImage string
  70. SeriesImage string
  71. AccessKeyId string
  72. AccessKeySecret string
  73. Endpoint string
  74. Bucket string
  75. AvatarBucket string
  76. IconBucket string
  77. }
  78. type Coupon struct {
  79. Url string
  80. Action string
  81. ExpireDate string
  82. }
  83. type RPCConfig struct {
  84. Keepalive KeepaliveConfig
  85. Common RPCNode
  86. }
  87. type Configure struct {
  88. // 视频间隔
  89. VideoInterval int
  90. // 课程
  91. Courses []string
  92. // 并发数
  93. Channel int
  94. // 做题课程
  95. QuizCourses []string
  96. // 是否全部网页需要打开
  97. IsOpenAll bool
  98. OnlyVisit bool
  99. VisitCount int
  100. }
  101. var Conf *Configure
  102. var v *viper.Viper
  103. // LoadConfig 装载配置文件
  104. func LoadConfig(filename string) error {
  105. configPath, configName := filepath.Split(filename)
  106. fileList := strings.Split(configName, ".")
  107. if len(fileList) < 2 {
  108. return fmt.Errorf("%s", "文件格式不正确")
  109. }
  110. configName = fileList[0]
  111. fileExt := fileList[1]
  112. var err error
  113. if fileExt == "json" {
  114. err = LoadConfigFromJson(configName, configPath)
  115. } else if fileExt == "yaml" || fileExt == "yml" {
  116. err = LoadConfigFromYaml(configName, configPath)
  117. } else {
  118. err = fmt.Errorf("%s", "不支持的文件格式")
  119. }
  120. // 出错直接返回
  121. if err != nil {
  122. return err
  123. }
  124. return nil
  125. }
  126. // LoadConfigFromYaml 装载yaml类型的配置文件
  127. func LoadConfigFromYaml(configName, configPath string) error {
  128. v = viper.New()
  129. v.SetConfigName(configName)
  130. v.AddConfigPath(configPath)
  131. //设置配置文件类型
  132. v.SetConfigType("yaml")
  133. if err := v.ReadInConfig(); err != nil {
  134. return err
  135. }
  136. return parseConfig()
  137. }
  138. // LoadConfigFromJson 装载json类型的配置文件
  139. func LoadConfigFromJson(configName, configPath string) error {
  140. v = viper.New()
  141. v.SetConfigName(configName)
  142. v.AddConfigPath(configPath)
  143. //设置配置文件类型
  144. v.SetConfigType("json")
  145. if err := v.ReadInConfig(); err != nil {
  146. return err
  147. }
  148. return parseConfig()
  149. }
  150. func parseConfig() error {
  151. Conf = &Configure{}
  152. if err := v.Unmarshal(Conf); err != nil {
  153. return err
  154. }
  155. return nil
  156. }