github.com/infraboard/keyauth@v0.8.1/apps/system/notify/sms/config.go (about) 1 package sms 2 3 import ( 4 "fmt" 5 6 "github.com/caarlos0/env/v6" 7 "github.com/go-playground/validator/v10" 8 9 "github.com/infraboard/keyauth/apps/system/notify" 10 ) 11 12 const ( 13 // DEFAULT_TENCENT_SMS_ENDPOINT todo 14 DEFAULT_TENCENT_SMS_ENDPOINT = "sms.tencentcloudapi.com" 15 ) 16 17 // use a single instance of Validate, it caches struct info 18 var ( 19 validate = validator.New() 20 ) 21 22 // LoadConfigFromEnv todo 23 func LoadConfigFromEnv() (*Config, error) { 24 cfg := &Config{} 25 if err := env.Parse(cfg); err != nil { 26 return nil, fmt.Errorf("load config from env, %s", err.Error()) 27 } 28 return cfg, nil 29 } 30 31 // NewDefaultConfig todo 32 func NewDefaultConfig() *Config { 33 return &Config{ 34 EnabledProvider: ProviderTenCent, 35 Tencent: &TenCentConfig{}, 36 ALI: &ALI{}, 37 } 38 } 39 40 // Config 短信配置 41 type Config struct { 42 EnabledProvider Provider `bson:"enabled_provider" json:"enabled_provider"` 43 Tencent *TenCentConfig `bson:"tencent" json:"tencent"` 44 ALI *ALI `bson:"ali" json:"ali"` 45 } 46 47 // Desensitize 脱敏 48 func (c *Config) Desensitize() { 49 c.Tencent.SecretKey = "" 50 } 51 52 // Validate todo 53 func (c *Config) Validate() error { 54 switch c.EnabledProvider { 55 case ProviderTenCent: 56 return c.Tencent.Validate() 57 case ProviderALI: 58 return fmt.Errorf("not impl") 59 default: 60 return fmt.Errorf("unknown provider type: %s", c.EnabledProvider) 61 } 62 } 63 64 // TenCentConfig todo 65 // 接口和相关文档请参考https://console.cloud.tencent.com/api/explorer?Product=sms&Version=2019-07-11&Action=SendSms&SignVersion= 66 type TenCentConfig struct { 67 Endpoint string `bson:"endpoint" json:"endpoint" env:"K_SMS_TENCENT_ENDPOINT"` 68 SecretID string `bson:"secret_id" json:"secret_id" validate:"required,lte=64" env:"K_SMS_TENCENT_SECRET_ID"` 69 SecretKey string `bson:"secret_key" json:"secret_key" validate:"required,lte=64" env:"K_SMS_TENCENT_SECRET_KEY"` 70 AppID string `bson:"app_id" json:"app_id" validate:"required,lte=64" env:"K_SMS_TENCENT_APPID"` 71 TemplateID string `bson:"template_id" json:"template_id" validate:"required,lte=64" env:"K_SMS_TENCENT_TEMPLATE_ID"` 72 Sign string `bson:"sign" json:"sign" validate:"required,lte=128" env:"K_SMS_TENCENT_SIGN"` 73 } 74 75 // Validate todo 76 func (s *TenCentConfig) Validate() error { 77 return validate.Struct(s) 78 } 79 80 // GetEndpoint todo 81 func (s *TenCentConfig) GetEndpoint() string { 82 if s.Endpoint != "" { 83 return s.Endpoint 84 } 85 86 return DEFAULT_TENCENT_SMS_ENDPOINT 87 } 88 89 // NewDeaultTestSendRequest todo 90 func NewDeaultTestSendRequest() *TestSendRequest { 91 return &TestSendRequest{ 92 SendSMSRequest: notify.NewSendSMSRequest(), 93 Config: NewDefaultConfig(), 94 } 95 } 96 97 // TestSendRequest todo 98 type TestSendRequest struct { 99 *notify.SendSMSRequest 100 *Config 101 } 102 103 // Send todo 104 func (req *TestSendRequest) Send() error { 105 sd, err := NewSender(req.Config) 106 if err != nil { 107 return err 108 } 109 110 return sd.Send(req.SendSMSRequest) 111 }