refactor(handler): 重构runner及checker架构
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
package ruleConfigChecker
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/ssdomei232/goodBaby/drivers/bilibili"
|
||||
)
|
||||
|
||||
// BilibiliDynamicRuleValidator B站动态规则验证器
|
||||
type BilibiliDynamicRuleValidator struct{}
|
||||
|
||||
func (v *BilibiliDynamicRuleValidator) GetType() string {
|
||||
return "bilibili-dynamic"
|
||||
}
|
||||
|
||||
func (v *BilibiliDynamicRuleValidator) Validate(configJSON string) error {
|
||||
var config bilibili.BiliDynamicConfig
|
||||
if err := json.Unmarshal([]byte(configJSON), &config); err != nil {
|
||||
return fmt.Errorf("解析B站动态规则配置失败: %v", err)
|
||||
}
|
||||
|
||||
if config.Msg == "" {
|
||||
return fmt.Errorf("B站动态规则配置中 msg 不能为空")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package ruleConfigChecker
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/ssdomei232/goodBaby/drivers/dingtalk"
|
||||
)
|
||||
|
||||
// DingTalkRuleValidator 钉钉规则验证器
|
||||
type DingTalkRuleValidator struct{}
|
||||
|
||||
func (v *DingTalkRuleValidator) GetType() string {
|
||||
return "dingtalk"
|
||||
}
|
||||
|
||||
func (v *DingTalkRuleValidator) Validate(configJSON string) error {
|
||||
var config dingtalk.DingTalkConfig
|
||||
if err := json.Unmarshal([]byte(configJSON), &config); err != nil {
|
||||
return fmt.Errorf("解析钉钉规则配置失败: %v", err)
|
||||
}
|
||||
|
||||
if config.AccessToken == "" {
|
||||
return fmt.Errorf("钉钉规则配置中 access_token 不能为空")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package ruleConfigChecker
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/mail"
|
||||
|
||||
"github.com/ssdomei232/goodBaby/drivers/email"
|
||||
)
|
||||
|
||||
// EmailRuleValidator 邮件规则验证器
|
||||
type EmailRuleValidator struct{}
|
||||
|
||||
func (v *EmailRuleValidator) GetType() string {
|
||||
return "email"
|
||||
}
|
||||
|
||||
func (v *EmailRuleValidator) Validate(configJSON string) error {
|
||||
var config email.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,32 @@
|
||||
package ruleConfigChecker
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/ssdomei232/goodBaby/drivers/onebot"
|
||||
)
|
||||
|
||||
// OneBotRuleValidator OneBot规则验证器
|
||||
type OneBotRuleValidator struct{}
|
||||
|
||||
func (v *OneBotRuleValidator) GetType() string {
|
||||
return "onebot"
|
||||
}
|
||||
|
||||
func (v *OneBotRuleValidator) Validate(configJSON string) error {
|
||||
var config onebot.OneBotConfig
|
||||
if err := json.Unmarshal([]byte(configJSON), &config); err != nil {
|
||||
return fmt.Errorf("解析OneBot规则配置失败: %v", err)
|
||||
}
|
||||
|
||||
if config.Msg == "" {
|
||||
return fmt.Errorf("OneBot规则配置中 msg 不能为空")
|
||||
}
|
||||
|
||||
if len(config.SendGroups) == 0 {
|
||||
return fmt.Errorf("OneBot规则配置中 send_groups 不能为空")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
package ruleConfigChecker
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// RuleValidator 规则验证器接口
|
||||
type RuleValidator interface {
|
||||
// Validate 验证规则配置是否合法
|
||||
Validate(configJSON string) error
|
||||
// GetType 获取验证器支持的规则类型
|
||||
GetType() string
|
||||
}
|
||||
|
||||
// ValidatorRegistry 验证器注册表
|
||||
type ValidatorRegistry struct {
|
||||
validators map[string]RuleValidator
|
||||
}
|
||||
|
||||
// NewValidatorRegistry 创建新的验证器注册表
|
||||
func NewValidatorRegistry() *ValidatorRegistry {
|
||||
return &ValidatorRegistry{
|
||||
validators: make(map[string]RuleValidator),
|
||||
}
|
||||
}
|
||||
|
||||
// Register 注册规则验证器
|
||||
func (vr *ValidatorRegistry) Register(validator RuleValidator) {
|
||||
vr.validators[validator.GetType()] = validator
|
||||
}
|
||||
|
||||
// Validate 根据规则类型验证配置
|
||||
func (vr *ValidatorRegistry) Validate(ruleType string, configJSON string) error {
|
||||
validator, exists := vr.validators[ruleType]
|
||||
if !exists {
|
||||
return fmt.Errorf("不支持的规则类型: %s", ruleType)
|
||||
}
|
||||
|
||||
if configJSON == "" {
|
||||
return fmt.Errorf("规则配置不能为空")
|
||||
}
|
||||
|
||||
// 首先验证 JSON 格式是否正确
|
||||
var js json.RawMessage
|
||||
if err := json.Unmarshal([]byte(configJSON), &js); err != nil {
|
||||
return fmt.Errorf("规则配置 JSON 格式错误: %v", err)
|
||||
}
|
||||
|
||||
// 调用具体验证器的验证逻辑
|
||||
return validator.Validate(configJSON)
|
||||
}
|
||||
|
||||
// GetSupportedTypes 获取所有支持的规则类型
|
||||
func (vr *ValidatorRegistry) GetSupportedTypes() []string {
|
||||
types := make([]string, 0, len(vr.validators))
|
||||
for t := range vr.validators {
|
||||
types = append(types, t)
|
||||
}
|
||||
return types
|
||||
}
|
||||
|
||||
// InitValidatorRegistry 初始化验证器注册表并注册所有验证器
|
||||
func InitValidatorRegistry() *ValidatorRegistry {
|
||||
registry := NewValidatorRegistry()
|
||||
|
||||
// 注册所有规则验证器
|
||||
registry.Register(&BilibiliDynamicRuleValidator{})
|
||||
registry.Register(&EmailRuleValidator{})
|
||||
registry.Register(&OneBotRuleValidator{})
|
||||
registry.Register(&DingTalkRuleValidator{})
|
||||
|
||||
return registry
|
||||
}
|
||||
Reference in New Issue
Block a user