refactor(internal): 分离runner
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
package email
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/mail"
|
||||
)
|
||||
|
||||
// EmailRuleValidator 邮件规则验证器
|
||||
type EmailRuleValidator struct{}
|
||||
|
||||
func (v *EmailRuleValidator) GetType() string {
|
||||
return "email"
|
||||
}
|
||||
|
||||
func (v *EmailRuleValidator) Validate(configJSON string) error {
|
||||
var config EmailRule
|
||||
if err := json.Unmarshal([]byte(configJSON), &config); err != nil {
|
||||
return fmt.Errorf("解析邮件规则配置失败: %v", err)
|
||||
}
|
||||
|
||||
if config.Title == "" {
|
||||
return fmt.Errorf("邮件规则配置中 title 不能为空")
|
||||
}
|
||||
|
||||
if config.Msg == "" {
|
||||
return fmt.Errorf("邮件规则配置中 msg 不能为空")
|
||||
}
|
||||
|
||||
if len(config.Destinations) == 0 {
|
||||
return fmt.Errorf("邮件规则配置中 destinations 不能为空")
|
||||
}
|
||||
|
||||
// 验证邮箱地址格式
|
||||
for _, dest := range config.Destinations {
|
||||
if _, err := mail.ParseAddress(dest); err != nil {
|
||||
return fmt.Errorf("无效的邮箱地址: %s", dest)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package email
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/ssdomei232/goodBaby/model"
|
||||
)
|
||||
|
||||
// EmailExecutor 邮件执行器
|
||||
type EmailExecutor struct{}
|
||||
|
||||
func (e *EmailExecutor) GetType() string {
|
||||
return "email"
|
||||
}
|
||||
|
||||
func (e *EmailExecutor) Execute(rule *model.Rule) error {
|
||||
log.Printf("执行邮件规则: %s (ID: %d)", rule.Name, rule.ID)
|
||||
|
||||
SendMail(rule)
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user