调整SetOptions的方式

This commit is contained in:
Yun
2025-11-21 15:54:49 +08:00
parent 04bdd98d6f
commit eeaa518fc3
17 changed files with 90 additions and 86 deletions
+13 -11
View File
@@ -14,9 +14,9 @@ import (
)
const (
MaxRetries = 3
DefaultRegion = "us-east-1"
MaxRecipients = 50 // AWS SES limit
MaxRetries = 3
DefaultRegion = "us-east-1"
MaxRecipients = 50 // AWS SES limit
)
type Aws struct {
@@ -31,18 +31,18 @@ func NewAws() *Aws {
return aws
}
func (l *Aws) SetOption(ctx context.Context, opt ...interfaces.Option) (interfaces.EmailInterface, error) {
func (l *Aws) SetOption(ctx context.Context, opt ...interfaces.Option) error {
for _, o := range opt {
o(&l.Options)
}
if l.Options.Aws == nil {
return nil, fmt.Errorf("AWS configuration is required")
return fmt.Errorf("AWS configuration is required")
}
// 验证配置
if err := l.validateConfig(); err != nil {
return nil, fmt.Errorf("invalid AWS config: %w", err)
return fmt.Errorf("invalid AWS config: %w", err)
}
if l.Options.Aws.Region == "" {
@@ -51,18 +51,20 @@ func (l *Aws) SetOption(ctx context.Context, opt ...interfaces.Option) (interfac
// 初始化SES客户端
if err := l.initSESClient(); err != nil {
return nil, fmt.Errorf("failed to initialize SES client: %w", err)
return fmt.Errorf("failed to initialize SES client: %w", err)
}
// 安全日志输出
l.Options.Logger.Infof(ctx, "AWS SES configured - Region:%s Sender:%s",
l.Options.Logger.Infof(ctx, "AWS SES configured - Region:%s Sender:%s",
l.Options.Aws.Region, l.Options.Aws.Sender)
return l, nil
l.IsSet = true
return nil
}
func (l *Aws) Send(ctx context.Context, params interfaces.Message) error {
if l.sesClient == nil {
if !l.IsSet {
return fmt.Errorf("AWS SES client not initialized")
}
@@ -204,4 +206,4 @@ func (l *Aws) sendEmail(ctx context.Context, params interfaces.Message) error {
}
return nil
}
}
+3 -3
View File
@@ -25,7 +25,7 @@ func NewAwsOld() *AwsOld {
return aws
}
func (l *AwsOld) SetOption(ctx context.Context, opt ...interfaces.Option) (interfaces.EmailInterface, error) {
func (l *AwsOld) SetOption(ctx context.Context, opt ...interfaces.Option) error {
for _, o := range opt {
o(&l.Options)
@@ -33,14 +33,14 @@ func (l *AwsOld) SetOption(ctx context.Context, opt ...interfaces.Option) (inter
l.Options.Logger.Infof(ctx, "Aws:%+v", l.Options.Aws)
if l.Options.Aws == nil {
return nil, errors.New("not aws")
return errors.New("not aws")
}
if l.Options.Aws.Region == "" {
l.Options.Aws.Region = "ap-northeast-1"
}
return l, nil
return nil
}
func (l *AwsOld) Send(ctx context.Context, params interfaces.Message) error {
+2 -2
View File
@@ -21,11 +21,11 @@ func TestSend(t *testing.T) {
// #发件人
// Source: "chenlihan@dreaminglife.cn"
a := aws.NewAws()
ini := aws.NewAws()
ctx := context.Background()
ini, err := a.SetOption(ctx, interfaces.SetAws(&interfaces.EmailConfigDataAws{
err := ini.SetOption(ctx, interfaces.SetAws(&interfaces.EmailConfigDataAws{
AccessId: "AKIAU6GD3MNRHKR4RZG5",
AccessSecret: "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",
Region: "ap-northeast-1",