From 033fd91aa1bde7de4e111427262c08cc0252cd25 Mon Sep 17 00:00:00 2001 From: mei Date: Sat, 20 Jun 2026 06:50:43 +0800 Subject: [PATCH] =?UTF-8?q?feat(internal):=20=E8=B4=A6=E5=8F=B7=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E9=AA=8C=E8=AF=81=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + drivers/bilibili/accountConfigChecker.go | 26 +++++++++ drivers/email/accountConfigChecker.go | 36 +++++++++++++ internal/accountConfigChecker/interface.go | 61 ++++++++++++++++++++++ internal/accountConfigChecker/reg.go | 10 ++++ 5 files changed, 134 insertions(+) create mode 100644 drivers/bilibili/accountConfigChecker.go create mode 100644 drivers/email/accountConfigChecker.go create mode 100644 internal/accountConfigChecker/interface.go create mode 100644 internal/accountConfigChecker/reg.go diff --git a/README.md b/README.md index de7e996..32ffe93 100644 --- a/README.md +++ b/README.md @@ -13,3 +13,4 @@ * WebUI * 更多通知渠道 * 更灵活的设置 +* 配置测试 diff --git a/drivers/bilibili/accountConfigChecker.go b/drivers/bilibili/accountConfigChecker.go new file mode 100644 index 0000000..298bdf8 --- /dev/null +++ b/drivers/bilibili/accountConfigChecker.go @@ -0,0 +1,26 @@ +package bilibili + +import ( + "encoding/json" + "fmt" +) + +// BilibiliAccountConfigValidator B站账号配置验证器 +type BilibiliAccountConfigValidator struct{} + +func (v *BilibiliAccountConfigValidator) GetType() string { + return "bilibili-account" +} + +func (v *BilibiliAccountConfigValidator) Validate(config string) error { + var cfg BiliAccount + if err := json.Unmarshal([]byte(config), &cfg); err != nil { + return fmt.Errorf("解析B站账号配置失败: %v", err) + } + + if cfg.RawCookies == "" { + return fmt.Errorf("B站账号配置中 cookies 不能为空") + } + + return nil +} diff --git a/drivers/email/accountConfigChecker.go b/drivers/email/accountConfigChecker.go new file mode 100644 index 0000000..39aed17 --- /dev/null +++ b/drivers/email/accountConfigChecker.go @@ -0,0 +1,36 @@ +package email + +import ( + "encoding/json" + "fmt" +) + +// EmailAccountConfigValidator 邮箱账号配置验证器 +type EmailAccountConfigValidator struct{} + +func (v *EmailAccountConfigValidator) GetType() string { + return "email-account" +} + +func (v *EmailAccountConfigValidator) Validate(config string) error { + // 邮箱账号配置验证逻辑 + var cfg EmailAccountConfig + if err := json.Unmarshal([]byte(config), &cfg); err != nil { + return fmt.Errorf("解析邮箱账号配置失败: %v", err) + } + + if cfg.SMTPServer == "" { + return fmt.Errorf("邮箱账号配置中 SMTP 服务器不能为空") + } + if cfg.Port == 0 { + return fmt.Errorf("邮箱账号配置中 SMTP 端口不能为空") + } + if cfg.Username == "" { + return fmt.Errorf("邮箱账号配置中用户名不能为空") + } + if cfg.Password == "" { + return fmt.Errorf("邮箱账号配置中密码不能为空") + } + + return nil +} diff --git a/internal/accountConfigChecker/interface.go b/internal/accountConfigChecker/interface.go new file mode 100644 index 0000000..e3228b7 --- /dev/null +++ b/internal/accountConfigChecker/interface.go @@ -0,0 +1,61 @@ +package accountConfigChecker + +import ( + "encoding/json" + "fmt" +) + +// AccountValidator 账号验证器接口 +type AccountValidator interface { + // Validate 验证账号配置是否合法 + Validate(account string) error + // GetType 获取验证器支持的账号类型 + GetType() string +} + +// ValidatorRegistry 验证器注册表 +type ValidatorRegistry struct { + validators map[string]AccountValidator +} + +// NewValidatorRegistry 创建新的验证器注册表 +func NewValidatorRegistry() *ValidatorRegistry { + return &ValidatorRegistry{ + validators: make(map[string]AccountValidator), + } +} + +// Register 注册账号验证器 +func (vr *ValidatorRegistry) Register(validator AccountValidator) { + vr.validators[validator.GetType()] = validator +} + +// Validate 根据账号类型验证配置 +func (vr *ValidatorRegistry) Validate(accountType string, config string) error { + validator, exists := vr.validators[accountType] + if !exists { + return fmt.Errorf("不支持的账号类型: %s", accountType) + } + + if config == "" { + return fmt.Errorf("账号配置不能为空") + } + + // 首先验证 JSON 格式是否正确 + var js json.RawMessage + if err := json.Unmarshal([]byte(config), &js); err != nil { + return fmt.Errorf("账号配置 JSON 格式错误: %v", err) + } + + // 调用具体验证器的验证逻辑 + return validator.Validate(config) +} + +// GetSupportedTypes 获取所有支持的账号类型 +func (vr *ValidatorRegistry) GetSupportedTypes() []string { + types := make([]string, 0, len(vr.validators)) + for t := range vr.validators { + types = append(types, t) + } + return types +} diff --git a/internal/accountConfigChecker/reg.go b/internal/accountConfigChecker/reg.go new file mode 100644 index 0000000..3fbb54f --- /dev/null +++ b/internal/accountConfigChecker/reg.go @@ -0,0 +1,10 @@ +package accountConfigChecker + +func InitValidatorRegistry() *ValidatorRegistry { + registry := NewValidatorRegistry() + + // 注册所有规则验证器 + // registry.Register(&bilibili.BilibiliAccountValidator{}) + + return registry +}