| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- package configs
- import (
- "io/ioutil"
- "os"
- "pbx-api-gin/pkg/lfshook"
- log "github.com/sirupsen/logrus"
- "gopkg.in/yaml.v2"
- )
- // Config https://godoc.org/gopkg.in/yaml.v2
- // Config 存储配置
- type Config struct {
- //IdentityKey string `yaml:"identityKey"`
- AsteriskAMIHost string `yaml:"asteriskAMIHost"` // Host
- AsteriskAMIPort string `yaml:"asteriskAMIPort"` // Port
- AsteriskAMIUser string `yaml:"asteriskAMIUser"` // User
- AsteriskAMISecret string `yaml:"asteriskAMISecret"` // Secret
- LogInfoPath string `yaml:"logInfoPath"` //logInfoPath
- LogErrorPath string `yaml:"logErrorPath"` //logErrorPath
- RecordEventLog string `yaml:"recordingEvent"` //recordingEventfile
- DebugLogPath string `yaml:"debugLog"` //debugLog
- ProcessRecord string `yaml:"processRecord"` //processRecord
- LogLevel log.Level //logLevel
- }
- // ConfigPath 配置文件路径
- var ConfigPath = "/data/PA/configs/config.yaml"
- // ConfigGlobal 全局配置变量
- var ConfigGlobal *Config
- // DecodeConfig 解析配置
- func DecodeConfig() {
- _, err := os.Stat(ConfigPath)
- if err != nil {
- lfshook.NewLogger().Errorf("config file not exist %+v", err)
- return
- }
- fileByte, err := ioutil.ReadFile(ConfigPath)
- if err != nil {
- lfshook.NewLogger().Errorf("read config file %+v", err)
- }
- ConfigGlobal = &Config{}
- err = yaml.Unmarshal(fileByte, ConfigGlobal)
- if err != nil {
- lfshook.NewLogger().Errorf("Unmarshal config file %+v", err)
- }
- }
- // EncodeConfig 保存配置
- func EncodeConfig() error {
- out, _ := yaml.Marshal(ConfigGlobal)
- err := ioutil.WriteFile(ConfigPath, out, 0777)
- if err != nil {
- lfshook.NewLogger().Errorf("save config file %+v", err)
- }
- return err
- }
|