package common import ( "fmt" "github.com/gin-gonic/gin" "goiot-pkg/tools" "gopkg.in/yaml.v2" "io/ioutil" "os" "path" "runtime" ) var ConfigInfo *configModel //serverModel get server information from config.yml type serverModel struct { Mode string `yaml:"mode"` // run mode Host string `yaml:"host"` // server host Port string `yaml:"port"` // server port TokenExpire int64 `yaml:"token_expire"` // token expire seconds } //logModel get log information from config.yml type logModel struct { Compress int `yaml:"compress"` ConsoleStdout int `yaml:"console_stdout"` FileStdout int `yaml:"file_stdout"` Level string `yaml:"level"` LocalTime int `yaml:"local_time"` MaxAge int `yaml:"max_age"` MaxBackups int `yaml:"max_backups"` MaxSize int `yaml:"max_size"` Path string `yaml:"path"` } type configModel struct { Server *serverModel `yaml:"server"` Log *logModel `yaml:"log"` FileServUrl string } // LoadConfigInformation load config information for application func LoadConfigInformation() error { var err error // 先读取config.yml配置文件 // 获取当前是debug模式还是release模式, 根据模式读取不同的Apollo配置 var filePath string wr, _ := os.Getwd() if runtime.GOOS == "windows" { filePath = wr + "\\conf\\config.yml" } else { filePath = path.Join(path.Join(wr, "conf"), "config.yml") } var configData []byte configData, err = ioutil.ReadFile(filePath) if err != nil { fmt.Printf(" config file read failed: %s", err) return err } err = yaml.Unmarshal(configData, &ConfigInfo) if err != nil { fmt.Printf(" config parse failed: %s", err) return err } SetGinMode(ConfigInfo.Server.Mode) return err } func LoadApolloConfig() (*tools.ApolloConfig, error) { var err error // 连接Apollo err = tools.NewApollo() if err != nil { return nil, err } // 获取Apollo配置 var config *tools.ApolloConfig config, err = tools.GetApolloConfig() if err != nil { return nil, err } // 按需加载应用配置信息 loadAppConfig(tools.GetApollo().GetNameSpace("application")) return config, nil } func SetGinMode(mode string) { if mode == "debug" { gin.SetMode(gin.DebugMode) } else if mode == "release" { gin.SetMode(gin.ReleaseMode) } } func loadAppConfig(m map[string]interface{}) { if fileServ, ok := m["fileserv_url"]; ok { ConfigInfo.FileServUrl = fileServ.(string) } }