| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- package conf
- import (
- "fmt"
- "os"
- "strings"
- "sync"
- "git.shuncheng.lu/bigthing/gocommon/pkg/conf/goconfig"
- apollo2 "git.shuncheng.lu/bigthing/gocommon/pkg/conf/remote/apollo"
- "git.shuncheng.lu/bigthing/gocommon/pkg/internal/util"
- )
- type DriverType uint8
- const (
- Local DriverType = iota + 1
- Apollo
- Nacos
- )
- var (
- driverTypeMap = map[DriverType]string{
- Local: "Local",
- Apollo: "Apollo",
- Nacos: "Nacos",
- }
- )
- var (
- rootPath string
- config *goconfig.ConfigFile
- confMutex sync.RWMutex
- mainIniPath = "/config/env.ini"
- globalDriver = Local // 全局的driver 类型
- thirdLogSwitch bool // 第三方日志开关:true 开启,false 关闭;没有配置则默认开启; 启动时才会获取到值
- )
- func getConfig() *goconfig.ConfigFile {
- confMutex.RLock()
- defer confMutex.RUnlock()
- return config
- }
- func reloadConfig() {
- confMutex.Lock()
- defer confMutex.Unlock()
- var err error
- configPath := rootPath + mainIniPath
- config, err = goconfig.LoadConfigFile(configPath)
- if err != nil {
- fmt.Println("conf:reload config file, error:", err)
- return
- }
- driver := config.MustValue("conf", "driver", "file")
- if driver == "apollo" {
- //apollo.Start()
- config.SetValue("include_files", "path", apollo2.GetConfigPath())
- }
- if err = loadIncludeFiles(config); err != nil {
- fmt.Println("conf:reload files include files error:", err)
- return
- }
- }
- func Init() error {
- var err error
- rootPath = util.GetRootPath()
- config, err = newConfig()
- if err != nil {
- return err
- }
- driver := config.MustValue("conf", "driver", "file")
- if driver == "apollo" {
- globalDriver = Apollo
- interval := config.MustInt("conf", "remote_refresh_interval", 30)
- apollo2.SetRefreshInterval(interval)
- apollo2.Start()
- config.SetValue("include_files", "path", apollo2.GetConfigPath())
- go listen(apollo2.Watch())
- }
- if driver == "nacos" {
- globalDriver = Nacos
- if err := initNacosConfig(); err != nil {
- return err
- }
- }
- if err := loadIncludeFiles(config); err != nil {
- return err
- }
- err = configMustInit()
- if err != nil {
- return err
- }
- // 设置第三方日志开关
- setThirdLogSwitch()
- util.Debugf("Config load config success, driver=%v, env=%v, project_name=%v, port=%v", driverTypeMap[globalDriver], GetEnv(), GetProjectName(), GetAppPort())
- return nil
- }
- func listen(watch <-chan struct{}) {
- for {
- if _, ok := <-watch; ok {
- reloadConfig()
- }
- }
- }
- func newConfig() (*goconfig.ConfigFile, error) {
- var (
- err error
- newConfig *goconfig.ConfigFile
- )
- configPath := rootPath + mainIniPath
- if !fileExist(configPath) {
- curDir, _ := os.Getwd()
- pos := strings.LastIndex(curDir, "src")
- if pos == -1 {
- panic("conf:can't find " + configPath)
- }
- rootPath = curDir[:pos]
- configPath = rootPath + mainIniPath
- }
- newConfig, err = goconfig.LoadConfigFile(configPath)
- if err != nil {
- return nil, err
- }
- return newConfig, nil
- }
- func loadIncludeFiles(config *goconfig.ConfigFile) error {
- includeFile := config.MustValue("include_files", "path", "")
- if includeFile != "" {
- includeFiles := strings.Split(includeFile, ",")
- incFiles := make([]string, len(includeFiles))
- for i, incFile := range includeFiles {
- incFiles[i] = incFile
- }
- return config.AppendFiles(incFiles...)
- }
- return nil
- }
- // fileExist 检查文件或目录是否存在
- // 如果由 filename 指定的文件或目录存在则返回 true,否则返回 false
- func fileExist(filename string) bool {
- _, err := os.Stat(filename)
- return err == nil || os.IsExist(err)
- }
- func setThirdLogSwitch() {
- if GetString("log", "third_log_switch") == "off" {
- thirdLogSwitch = false
- } else {
- thirdLogSwitch = true
- }
- }
- func GetThirdLogWitch() bool {
- return thirdLogSwitch
- }
|