decode.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package configs
  2. import (
  3. "crm-api/pkg/lfshook"
  4. "io/ioutil"
  5. "os"
  6. "github.com/gofrs/uuid"
  7. log "github.com/sirupsen/logrus"
  8. "gopkg.in/yaml.v2"
  9. )
  10. // Config https://godoc.org/gopkg.in/yaml.v2
  11. // Config 存储配置
  12. type Config struct {
  13. IdentityKey string `yaml:"identityKey"`
  14. PbxNumber string `yaml:"pbxnumber"`
  15. AsteriskAMIHost string `yaml:"asteriskAMIHost"` // Host
  16. AsteriskAMIPort string `yaml:"asteriskAMIPort"` // Port
  17. AsteriskAMIUser string `yaml:"asteriskAMIUser"` // User
  18. AsteriskAMISecret string `yaml:"asteriskAMISecret"` // Secret
  19. MysqlDBHost string `yaml:"mysqlDBHost"` // Host
  20. MysqlDBUser string `yaml:"mysqlDBUser"` // User
  21. MysqlDBSecret string `yaml:"mysqlDBSecret"` // Secret
  22. MysqlDBName string `yaml:"mysqlDBName"` // Name
  23. RedisDBHost string `yaml:"redisDBHost"` // Host
  24. RedisDBPort string `yaml:"redisDBPort"` // User
  25. RedisDBSecret string `yaml:"redisDBSecret"` // Secret
  26. LogInfoPath string `yaml:"logInfoPath"` //logInfoPath
  27. LogErrorPath string `yaml:"logErrorPath"` //logErrorPath
  28. StoragePath string `yaml:"storagePath"` // 存储目录
  29. LogoPath string `yaml:"logoPath"` // 存储目录
  30. WebType string `yaml:"webtype"` //https http
  31. WebHost string `yaml:"webhost"` //Host
  32. WebPort int `yaml:"webport"` //port
  33. AllowOrigin string `yaml:"allowOrigin"` // allowOrigin
  34. PushConfig string `yaml:"pushConfig"` // 推送配置目录
  35. I18nPath string `yaml:"i18nPath"` //i18n
  36. LogLevel log.Level //logLevel
  37. // BasicAuthUser string `yaml:"basicAuthUser"` //BasicAuth Username
  38. // BasicAuthPWD string `yaml:"basicAuthPWD"` //BasicAuth Password
  39. // ApiKey string `yaml:"apiKey"` //ApiKey
  40. // ApiKeyValue string `yaml:"apiKeyValue"` //ApiKeyValue
  41. }
  42. // ConfigPath 配置文件路径
  43. var ConfigPath = "./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. return
  57. }
  58. ConfigGlobal = &Config{}
  59. err = yaml.Unmarshal(fileByte, ConfigGlobal)
  60. if err != nil {
  61. lfshook.NewLogger().Errorf("Unmarshal config file %+v", err)
  62. return
  63. }
  64. // jwt secret 生成
  65. if ConfigGlobal.IdentityKey == "" {
  66. uuid, _ := uuid.NewV4()
  67. ConfigGlobal.IdentityKey = uuid.String()
  68. EncodeConfig()
  69. }
  70. }
  71. // EncodeConfig 保存配置
  72. func EncodeConfig() error {
  73. out, _ := yaml.Marshal(ConfigGlobal)
  74. err := ioutil.WriteFile(ConfigPath, out, 0777)
  75. if err != nil {
  76. lfshook.NewLogger().Errorf("save config file %+v", err)
  77. }
  78. return err
  79. }