decode.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package configs
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "pbx-api-gin/pkg/lfshook"
  6. log "github.com/sirupsen/logrus"
  7. "gopkg.in/yaml.v2"
  8. )
  9. // Config https://godoc.org/gopkg.in/yaml.v2
  10. // Config 存储配置
  11. type Config struct {
  12. //IdentityKey string `yaml:"identityKey"`
  13. AsteriskAMIHost string `yaml:"asteriskAMIHost"` // Host
  14. AsteriskAMIPort string `yaml:"asteriskAMIPort"` // Port
  15. AsteriskAMIUser string `yaml:"asteriskAMIUser"` // User
  16. AsteriskAMISecret string `yaml:"asteriskAMISecret"` // Secret
  17. LogInfoPath string `yaml:"logInfoPath"` //logInfoPath
  18. LogErrorPath string `yaml:"logErrorPath"` //logErrorPath
  19. RecordEventLog string `yaml:"recordingEvent"` //recordingEventfile
  20. DebugLogPath string `yaml:"debugLog"` //debugLog
  21. ProcessRecord string `yaml:"processRecord"` //processRecord
  22. LogLevel log.Level //logLevel
  23. }
  24. // ConfigPath 配置文件路径
  25. var ConfigPath = "/data/PA/configs/config.yaml"
  26. // ConfigGlobal 全局配置变量
  27. var ConfigGlobal *Config
  28. // DecodeConfig 解析配置
  29. func DecodeConfig() {
  30. _, err := os.Stat(ConfigPath)
  31. if err != nil {
  32. lfshook.NewLogger().Errorf("config file not exist %+v", err)
  33. return
  34. }
  35. fileByte, err := ioutil.ReadFile(ConfigPath)
  36. if err != nil {
  37. lfshook.NewLogger().Errorf("read config file %+v", err)
  38. }
  39. ConfigGlobal = &Config{}
  40. err = yaml.Unmarshal(fileByte, ConfigGlobal)
  41. if err != nil {
  42. lfshook.NewLogger().Errorf("Unmarshal config file %+v", err)
  43. }
  44. }
  45. // EncodeConfig 保存配置
  46. func EncodeConfig() error {
  47. out, _ := yaml.Marshal(ConfigGlobal)
  48. err := ioutil.WriteFile(ConfigPath, out, 0777)
  49. if err != nil {
  50. lfshook.NewLogger().Errorf("save config file %+v", err)
  51. }
  52. return err
  53. }