app_env.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package config
  2. import (
  3. "fmt"
  4. "github.com/spf13/viper"
  5. )
  6. type EnvConfig struct {
  7. Mysql Mysql `mapstructure:"mysql" json:"mysql" yaml:"mysql"`
  8. BizList []BizInfo `mapstructure:"biz-list" json:"biz-list" yaml:"biz-list"`
  9. JWT JWT `mapstructure:"jwt" json:"jwt" yaml:"jwt"`
  10. Zap *Zap `mapstructure:"zap" json:"zap" yaml:"zap"`
  11. Redis *Redis `mapstructure:"redis" json:"redis" yaml:"redis"`
  12. File *File `mapstructure:"file" json:"file" yaml:"file"`
  13. StsOss *OssSts `mapstructure:"sts-oss" json:"sts-oss" yaml:"sts-oss"`
  14. }
  15. var envConfig *EnvConfig
  16. func EnvConf() *EnvConfig {
  17. if envConfig == nil {
  18. appEnvConfigInit()
  19. }
  20. return envConfig
  21. }
  22. // 环境配置初始化
  23. func appEnvConfigInit() {
  24. configFile := GetConfigFileNameByMod(AppConf().Mod)
  25. vp := viper.New()
  26. vp.SetConfigFile(configFile)
  27. vp.SetConfigType("json")
  28. setEnvDefaultConf(vp)
  29. handVpToFile(vp)
  30. if err := vp.Unmarshal(&envConfig); err != nil {
  31. panic(err)
  32. }
  33. }
  34. func setEnvDefaultConf(v *viper.Viper) {
  35. v.SetDefault("file", &File{
  36. OssType: OssTypeLocal,
  37. Path: fmt.Sprintf("http://127.0.0.1:%d", AdminHostPort),
  38. ProxyPath: "/admin/api/static",
  39. StorePath: "./static",
  40. OriginConf: map[string]interface{}{
  41. "endpoint": "oss-ap-southeast-1.aliyuncs.com",
  42. "access-key-id": "LTAI5tHwrKhv2sd927cXYoRw",
  43. "access-key-secret": "9TezWQUvvXGvucUdXXFMNaIGjwmusa",
  44. "bucket-name": "oss-sgp-prd-fomo-1",
  45. "bucket-url": "https://oss-sgp-prd-fomo-1.oss-ap-southeast-1.aliyuncs.com",
  46. "base-path": "test",
  47. },
  48. })
  49. v.SetDefault("mysql", Mysql{
  50. GeneralDB: GeneralDB{
  51. Prefix: "",
  52. Port: "3306",
  53. Config: `charset=utf8mb4&parseTime=True&loc=Local`,
  54. Dbname: AdminDbName,
  55. Username: "root",
  56. Password: "123456",
  57. Path: "127.0.0.1",
  58. Engine: "",
  59. LogMode: "error",
  60. MaxIdleConns: 10,
  61. MaxOpenConns: 10,
  62. Singular: false,
  63. LogZap: false,
  64. }})
  65. v.SetDefault("biz-list", []BizInfo{
  66. {
  67. AliasName: BizAliasName,
  68. ProxyUrl: fmt.Sprintf("http://127.0.0.1:%d", BizHostPort),
  69. ProxyAlias: BizAliasName,
  70. GeneralDB: GeneralDB{
  71. Prefix: "",
  72. Port: "3306",
  73. Config: `charset=utf8mb4&parseTime=True&loc=Local`,
  74. Dbname: BizDbName,
  75. Username: "root",
  76. Password: "123456",
  77. Path: "127.0.0.1",
  78. Engine: "",
  79. LogMode: "error",
  80. MaxIdleConns: 10,
  81. MaxOpenConns: 10,
  82. Singular: false,
  83. LogZap: false,
  84. },
  85. },
  86. })
  87. v.SetDefault("zap", &Zap{
  88. TagName: "app",
  89. Level: "debug",
  90. Prefix: "AMS_",
  91. Format: "_json",
  92. Director: "logs",
  93. EncodeLevel: "LowercaseLevelEncoder",
  94. StacktraceKey: "error",
  95. MaxAge: 3,
  96. RotationSize: 100,
  97. RotationCount: 3,
  98. ShowLine: true,
  99. LogInConsole: true,
  100. LogOutputFile: true,
  101. })
  102. v.SetDefault("redis", &Redis{
  103. Addr: "127.0.0.1:6379",
  104. Password: "",
  105. Db: 0,
  106. EnabledTls: false,
  107. EnabledCluster: false,
  108. Cluster: []string{"127.0.0.1:6379"},
  109. })
  110. v.SetDefault("jwt", &JWT{
  111. SigningKey: fmt.Sprintf("%s.!@#$1234", BizAliasName),
  112. ExpiresTime: 24,
  113. Issuer: "appTemp",
  114. })
  115. v.SetDefault("sts-oss", &OssSts{
  116. AccessKeyId: "LTAI5tHwrKhv2sd927cXYoRw",
  117. AccessKeySecret: "9TezWQUvvXGvucUdXXFMNaIGjwmusa",
  118. BucketName: "oss-sgp-prd-fomo-1",
  119. BucketUrl: "oss-sgp-prd-fomo-1.oss-ap-southeast-1.aliyuncs.com",
  120. BasePath: "test",
  121. StsEndpoint: "sts.ap-southeast-1.aliyuncs.com",
  122. StsDurationSeconds: 1200,
  123. StsRoleSessionName: "fomo-sts",
  124. StsRoleArn: "acs:ram::5796137199632408:role/fomo-sts",
  125. StsRegion: "oss-ap-southeast-1",
  126. })
  127. }