decode.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. //StoragePath string `yaml:"storagePath"` // 存储目录
  33. //AllEventPushUrl string `yaml:"allEventPushUrl"`
  34. //WebHost string `yaml:"webhost"` //Host
  35. //WebPort int64 `yaml:"webport"` //port
  36. AllowOrigin string `yaml:"allowOrigin"` // allowOrigin
  37. LogLevel log.Level //logLevel
  38. }
  39. // ConfigPath 配置文件路径
  40. var ConfigPath = "./config.yaml"
  41. // ConfigGlobal 全局配置变量
  42. var ConfigGlobal *Config
  43. // DecodeConfig 解析配置
  44. func DecodeConfig() {
  45. _, err := os.Stat(ConfigPath)
  46. if err != nil {
  47. lfshook.NewLogger().Errorf("config file not exist %+v", err)
  48. return
  49. }
  50. fileByte, err := ioutil.ReadFile(ConfigPath)
  51. if err != nil {
  52. lfshook.NewLogger().Errorf("read config file %+v", err)
  53. }
  54. ConfigGlobal = &Config{}
  55. err = yaml.Unmarshal(fileByte, ConfigGlobal)
  56. if err != nil {
  57. lfshook.NewLogger().Errorf("Unmarshal config file %+v", err)
  58. }
  59. }
  60. // EncodeConfig 保存配置
  61. func EncodeConfig() error {
  62. out, _ := yaml.Marshal(ConfigGlobal)
  63. err := ioutil.WriteFile(ConfigPath, out, 0777)
  64. if err != nil {
  65. lfshook.NewLogger().Errorf("save config file %+v", err)
  66. }
  67. return err
  68. }