config.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. package conf
  2. import (
  3. "fmt"
  4. "os"
  5. "strings"
  6. "sync"
  7. "git.shuncheng.lu/bigthing/gocommon/pkg/conf/goconfig"
  8. apollo2 "git.shuncheng.lu/bigthing/gocommon/pkg/conf/remote/apollo"
  9. "git.shuncheng.lu/bigthing/gocommon/pkg/internal/util"
  10. )
  11. type DriverType uint8
  12. const (
  13. Local DriverType = iota + 1
  14. Apollo
  15. Nacos
  16. )
  17. var (
  18. driverTypeMap = map[DriverType]string{
  19. Local: "Local",
  20. Apollo: "Apollo",
  21. Nacos: "Nacos",
  22. }
  23. )
  24. var (
  25. rootPath string
  26. config *goconfig.ConfigFile
  27. confMutex sync.RWMutex
  28. mainIniPath = "/config/env.ini"
  29. globalDriver = Local // 全局的driver 类型
  30. thirdLogSwitch bool // 第三方日志开关:true 开启,false 关闭;没有配置则默认开启; 启动时才会获取到值
  31. )
  32. func getConfig() *goconfig.ConfigFile {
  33. confMutex.RLock()
  34. defer confMutex.RUnlock()
  35. return config
  36. }
  37. func reloadConfig() {
  38. confMutex.Lock()
  39. defer confMutex.Unlock()
  40. var err error
  41. configPath := rootPath + mainIniPath
  42. config, err = goconfig.LoadConfigFile(configPath)
  43. if err != nil {
  44. fmt.Println("conf:reload config file, error:", err)
  45. return
  46. }
  47. driver := config.MustValue("conf", "driver", "file")
  48. if driver == "apollo" {
  49. //apollo.Start()
  50. config.SetValue("include_files", "path", apollo2.GetConfigPath())
  51. }
  52. if err = loadIncludeFiles(config); err != nil {
  53. fmt.Println("conf:reload files include files error:", err)
  54. return
  55. }
  56. }
  57. func Init() error {
  58. var err error
  59. rootPath = util.GetRootPath()
  60. config, err = newConfig()
  61. if err != nil {
  62. return err
  63. }
  64. driver := config.MustValue("conf", "driver", "file")
  65. if driver == "apollo" {
  66. globalDriver = Apollo
  67. interval := config.MustInt("conf", "remote_refresh_interval", 30)
  68. apollo2.SetRefreshInterval(interval)
  69. apollo2.Start()
  70. config.SetValue("include_files", "path", apollo2.GetConfigPath())
  71. go listen(apollo2.Watch())
  72. }
  73. if driver == "nacos" {
  74. globalDriver = Nacos
  75. if err := initNacosConfig(); err != nil {
  76. return err
  77. }
  78. }
  79. if err := loadIncludeFiles(config); err != nil {
  80. return err
  81. }
  82. err = configMustInit()
  83. if err != nil {
  84. return err
  85. }
  86. // 设置第三方日志开关
  87. setThirdLogSwitch()
  88. util.Debugf("Config load config success, driver=%v, env=%v, project_name=%v, port=%v", driverTypeMap[globalDriver], GetEnv(), GetProjectName(), GetAppPort())
  89. return nil
  90. }
  91. func listen(watch <-chan struct{}) {
  92. for {
  93. if _, ok := <-watch; ok {
  94. reloadConfig()
  95. }
  96. }
  97. }
  98. func newConfig() (*goconfig.ConfigFile, error) {
  99. var (
  100. err error
  101. newConfig *goconfig.ConfigFile
  102. )
  103. configPath := rootPath + mainIniPath
  104. if !fileExist(configPath) {
  105. curDir, _ := os.Getwd()
  106. pos := strings.LastIndex(curDir, "src")
  107. if pos == -1 {
  108. panic("conf:can't find " + configPath)
  109. }
  110. rootPath = curDir[:pos]
  111. configPath = rootPath + mainIniPath
  112. }
  113. newConfig, err = goconfig.LoadConfigFile(configPath)
  114. if err != nil {
  115. return nil, err
  116. }
  117. return newConfig, nil
  118. }
  119. func loadIncludeFiles(config *goconfig.ConfigFile) error {
  120. includeFile := config.MustValue("include_files", "path", "")
  121. if includeFile != "" {
  122. includeFiles := strings.Split(includeFile, ",")
  123. incFiles := make([]string, len(includeFiles))
  124. for i, incFile := range includeFiles {
  125. incFiles[i] = incFile
  126. }
  127. return config.AppendFiles(incFiles...)
  128. }
  129. return nil
  130. }
  131. // fileExist 检查文件或目录是否存在
  132. // 如果由 filename 指定的文件或目录存在则返回 true,否则返回 false
  133. func fileExist(filename string) bool {
  134. _, err := os.Stat(filename)
  135. return err == nil || os.IsExist(err)
  136. }
  137. func setThirdLogSwitch() {
  138. if GetString("log", "third_log_switch") == "off" {
  139. thirdLogSwitch = false
  140. } else {
  141. thirdLogSwitch = true
  142. }
  143. }
  144. func GetThirdLogWitch() bool {
  145. return thirdLogSwitch
  146. }