decode.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. //AsteriskAGIPort string `yaml:"asteriskAGIPort"` // AGIPort
  18. //AsteriskTriggerPath string `yaml:"asteriskTriggerPath"` // 广播触发规则
  19. //AsteriskPagingPath string `yaml:"asteriskPagingPath"` // 广播规则
  20. //AsteriskBroadcastTimeout int64 `yaml:"asteriskBroadcastTimeout"` // 广播响铃时长
  21. //AsteriskBroadcastName string `yaml:"asteriskBroadcastName"` // 广播时名称
  22. //AsteriskBroadcastID string `yaml:"asteriskBroadcastID"` // 广播时ID
  23. //MysqlDBHost string `yaml:"mysqlDBHost"` // Host
  24. //MysqlDBUser string `yaml:"mysqlDBUser"` // User
  25. //MysqlDBSecret string `yaml:"mysqlDBSecret"` // Secret
  26. //MysqlDBName string `yaml:"mysqlDBName"` // Name
  27. //RedisDBHost string `yaml:"redisDBHost"` // Host
  28. //RedisDBPort string `yaml:"redisDBPort"` // User
  29. //RedisDBSecret string `yaml:"redisDBSecret"` // Secret
  30. LogInfoPath string `yaml:"logInfoPath"` //logInfoPath
  31. LogErrorPath string `yaml:"logErrorPath"` //logErrorPath
  32. RecordEventLog string `yaml:"recordingEvent"` //recordingEventfile
  33. ProcessRecord string `yaml:"processRecord"` //processRecord
  34. //StoragePath string `yaml:"storagePath"` // 存储目录
  35. //AllEventPushUrl string `yaml:"allEventPushUrl"`
  36. //WebHost string `yaml:"webhost"` //Host
  37. //WebPort int64 `yaml:"webport"` //port
  38. //AllowOrigin string `yaml:"allowOrigin"` // allowOrigin
  39. //AllowOrigin string `yaml:"allowOrigin"` // allowOrigin
  40. LogLevel log.Level //logLevel
  41. }
  42. // ConfigPath 配置文件路径
  43. var ConfigPath = "/data/test/configs/config.yaml"
  44. // ConfigGlobal 全局配置变量
  45. var ConfigGlobal *Config
  46. // DecodeConfig 解析配置
  47. func DecodeConfig() {
  48. _, err := os.Stat(ConfigPath)
  49. if err != nil {
  50. lfshook.NewLogger().Errorf("config file not exist %+v", err)
  51. return
  52. }
  53. fileByte, err := ioutil.ReadFile(ConfigPath)
  54. if err != nil {
  55. lfshook.NewLogger().Errorf("read config file %+v", err)
  56. }
  57. ConfigGlobal = &Config{}
  58. err = yaml.Unmarshal(fileByte, ConfigGlobal)
  59. if err != nil {
  60. lfshook.NewLogger().Errorf("Unmarshal config file %+v", err)
  61. }
  62. }
  63. // EncodeConfig 保存配置
  64. func EncodeConfig() error {
  65. out, _ := yaml.Marshal(ConfigGlobal)
  66. err := ioutil.WriteFile(ConfigPath, out, 0777)
  67. if err != nil {
  68. lfshook.NewLogger().Errorf("save config file %+v", err)
  69. }
  70. return err
  71. }