config_default_init.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package config
  2. import (
  3. "fmt"
  4. "github.com/spf13/viper"
  5. "os"
  6. )
  7. // 初始化配置文件 -- 当存在则不做处理
  8. func ConfInit() error {
  9. //fmt.Println("begin create config...")
  10. createAppConfig()
  11. //fmt.Println(fmt.Sprintf("create app config:%s", "success"))
  12. createEnvConfig()
  13. //fmt.Println(fmt.Sprintf("create env config:%s", "success"))
  14. return nil
  15. }
  16. func createAppConfig() {
  17. vp := viper.New()
  18. vp.SetConfigFile(ConfigAppFile)
  19. setDefaultAppConfig(vp) // 设置默认配置
  20. saveConfigFile(vp) // 文件读取 如果读取不到则生成(dev环境)
  21. if err := vp.Unmarshal(&appConf); err != nil {
  22. panic(err)
  23. }
  24. }
  25. // 保存配置文件
  26. func saveConfigFile(vp *viper.Viper) {
  27. err := vp.ReadInConfig()
  28. if err != nil {
  29. if _, err = os.Stat(ConfigDir); os.IsNotExist(err) {
  30. err = os.Mkdir(ConfigDir, 0755)
  31. if err != nil {
  32. panic(err)
  33. }
  34. }
  35. if err := vp.WriteConfig(); err != nil {
  36. panic(fmt.Errorf("Fatal error config file: %s \n", err))
  37. }
  38. }
  39. }
  40. func createEnvConfig() {
  41. configFile := GetConfigFileByMod(ModEnvDev)
  42. vp := viper.New()
  43. vp.SetConfigFile(configFile)
  44. vp.SetConfigType("json")
  45. setDefaultEnvConfig(vp)
  46. saveConfigFile(vp) // 文件读取 如果读取不到则生成(dev环境)
  47. if err := vp.Unmarshal(&envConfig); err != nil {
  48. panic(err)
  49. }
  50. }
  51. // 环境配置初始化
  52. func setDefaultAppConfig(vp *viper.Viper) {
  53. vp.SetConfigType("json")
  54. vp.SetDefault("api_addr", BizHostPort)
  55. vp.SetDefault("job_addr", JobHostPort)
  56. vp.SetDefault("mod", "dev")
  57. vp.SetDefault("router_prefix", "")
  58. }
  59. func setDefaultEnvConfig(vp *viper.Viper) {
  60. vp.SetDefault("jwt", &JWT{
  61. SigningKey: fmt.Sprintf("%s!@#¥Secret", SignSystemName),
  62. ExpiresTime: 24,
  63. Issuer: "app",
  64. })
  65. vp.SetDefault("exchange", &Exchange{
  66. RootUrl: "https://www.jcwork.club",
  67. Appid: "2hhaivh1CjEKhb7Q",
  68. Secret: "m6iITfTEsMVHkLRG2JP9Qn1HmxvFKfBTPyAl6evNnmJXRqCxVj4GLqmY2KFuG4Mh",
  69. })
  70. vp.SetDefault("mysql", &Mysql{
  71. GeneralDB: GeneralDB{
  72. Prefix: "",
  73. Port: "3306",
  74. Config: `charset=utf8mb4&parseTime=True&loc=Local`,
  75. Dbname: BizDbName,
  76. Username: "root",
  77. Password: "123456",
  78. Path: "127.0.0.1",
  79. Engine: "",
  80. LogMode: "error",
  81. MaxIdleConns: 10,
  82. MaxOpenConns: 10,
  83. Singular: false,
  84. LogZap: false,
  85. }})
  86. vp.SetDefault("zap", &Zap{
  87. TagName: "app",
  88. Level: "debug",
  89. Prefix: "APP_",
  90. Format: "_json",
  91. Director: "logs",
  92. EncodeLevel: "LowercaseLevelEncoder",
  93. StacktraceKey: "error",
  94. MaxAge: 10,
  95. RotationSize: 100,
  96. RotationCount: 10,
  97. ShowLine: true,
  98. LogInConsole: true,
  99. LogOutputFile: true,
  100. })
  101. vp.SetDefault("redis", &Redis{
  102. Addr: "127.0.0.1:6379",
  103. Password: "",
  104. Db: 0,
  105. EnabledTls: false,
  106. EnabledCluster: false,
  107. Cluster: []string{"127.0.0.1:6379"},
  108. })
  109. vp.SetDefault("file", &File{
  110. OssType: "aws",
  111. Path: "http://127.0.0.1:9000",
  112. ProxyPath: "/daytask-media",
  113. StorePath: "./static",
  114. OriginConf: map[string]interface{}{
  115. "bucket": "daytask-media",
  116. "region": "us-east-1",
  117. "endpoint": "http://127.0.0.1:9000",
  118. "secret-id": "minioadmin",
  119. "secret-key": "minioadmin",
  120. "base-url": "http://127.0.0.1:9000/daytask-media",
  121. "path-prefix": "uploads",
  122. "s3-force-path-style": true,
  123. "disable-ssl": true,
  124. },
  125. })
  126. }