refactor(handler): 重构runner及checker架构

This commit is contained in:
mei
2026-06-06 19:31:16 +08:00
parent 7c4fb1e1d8
commit 7957a91609
13 changed files with 405 additions and 19 deletions
+23
View File
@@ -0,0 +1,23 @@
package runner
import (
"log"
"github.com/ssdomei232/goodBaby/drivers/bilibili"
"github.com/ssdomei232/goodBaby/model"
)
// BilibiliDynamicExecutor B站动态执行器
type BilibiliDynamicExecutor struct{}
func (e *BilibiliDynamicExecutor) GetType() string {
return "bilibili-dynamic"
}
func (e *BilibiliDynamicExecutor) Execute(rule *model.Rule) error {
log.Printf("执行B站动态规则: %s (ID: %d)", rule.Name, rule.ID)
bilibili.SendBiliDynamicMsg(rule)
return nil
}
+23
View File
@@ -0,0 +1,23 @@
package runner
import (
"log"
"github.com/ssdomei232/goodBaby/drivers/email"
"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)
email.SendMail(rule)
return nil
}
+23
View File
@@ -0,0 +1,23 @@
package runner
import (
"log"
"github.com/ssdomei232/goodBaby/drivers/github"
"github.com/ssdomei232/goodBaby/model"
)
// GithubMakeRepoPublicExecutor GitHub仓库公开执行器
type GithubMakeRepoPublicExecutor struct{}
func (e *GithubMakeRepoPublicExecutor) GetType() string {
return "github-make-repo-public"
}
func (e *GithubMakeRepoPublicExecutor) Execute(rule *model.Rule) error {
log.Printf("执行GitHub仓库公开规则: %s (ID: %d)", rule.Name, rule.ID)
github.MakeRepositoryPublic(rule)
return nil
}
+76
View File
@@ -0,0 +1,76 @@
package runner
import (
"fmt"
"github.com/ssdomei232/goodBaby/model"
)
// RuleExecutor 规则执行器接口
type RuleExecutor interface {
// Execute 执行规则
Execute(rule *model.Rule) error
// GetType 获取执行器支持的规则类型
GetType() string
}
// ExecutorRegistry 执行器注册表
type ExecutorRegistry struct {
executors map[string]RuleExecutor
}
// NewExecutorRegistry 创建新的执行器注册表
func NewExecutorRegistry() *ExecutorRegistry {
return &ExecutorRegistry{
executors: make(map[string]RuleExecutor),
}
}
// Register 注册规则执行器
func (er *ExecutorRegistry) Register(executor RuleExecutor) {
er.executors[executor.GetType()] = executor
}
// Execute 根据规则类型执行规则
func (er *ExecutorRegistry) Execute(rule *model.Rule) error {
executor, exists := er.executors[rule.Type]
if !exists {
return fmt.Errorf("不支持的规则类型: %s", rule.Type)
}
return executor.Execute(rule)
}
// GetSupportedTypes 获取所有支持的规则类型
func (er *ExecutorRegistry) GetSupportedTypes() []string {
types := make([]string, 0, len(er.executors))
for t := range er.executors {
types = append(types, t)
}
return types
}
var globalExecutorRegistry *ExecutorRegistry
// InitExecutorRegistry 初始化执行器注册表并注册所有执行器
func InitExecutorRegistry() *ExecutorRegistry {
registry := NewExecutorRegistry()
// 注册所有规则执行器
registry.Register(&BilibiliDynamicExecutor{})
registry.Register(&EmailExecutor{})
registry.Register(&GithubMakeRepoPublicExecutor{})
registry.Register(&OneBotExecutor{})
// 未来添加新规则类型时,在这里注册即可
globalExecutorRegistry = registry
return registry
}
// GetGlobalExecutorRegistry 获取全局执行器注册表
func GetGlobalExecutorRegistry() *ExecutorRegistry {
if globalExecutorRegistry == nil {
panic("执行器注册表未初始化,请先调用 InitExecutorRegistry()")
}
return globalExecutorRegistry
}
+23
View File
@@ -0,0 +1,23 @@
package runner
import (
"log"
"github.com/ssdomei232/goodBaby/drivers/onebot"
"github.com/ssdomei232/goodBaby/model"
)
// OneBotExecutor OneBot执行器
type OneBotExecutor struct{}
func (e *OneBotExecutor) GetType() string {
return "onebot"
}
func (e *OneBotExecutor) Execute(rule *model.Rule) error {
log.Printf("执行OneBot规则: %s (ID: %d)", rule.Name, rule.ID)
onebot.SendOneBotMsg(rule)
return nil
}
+5 -15
View File
@@ -3,10 +3,6 @@ package runner
import (
"log"
"github.com/ssdomei232/goodBaby/drivers/bilibili"
"github.com/ssdomei232/goodBaby/drivers/email"
"github.com/ssdomei232/goodBaby/drivers/github"
"github.com/ssdomei232/goodBaby/drivers/onebot"
"github.com/ssdomei232/goodBaby/handler/db"
"github.com/ssdomei232/goodBaby/model"
)
@@ -28,21 +24,15 @@ func Runner(timer *model.Timer) {
// 2. 执行每个 Rule
for _, rule := range rules {
// 执行 rule
go executeRule(&rule)
}
}
// super switch
// executeRule 执行单个规则
func executeRule(rule *model.Rule) {
switch rule.Type {
case "bilibili-dynamic":
bilibili.SendBiliDynamicMsg(rule)
case "email":
email.SendMail(rule)
case "github-make-repo-public":
github.MakeRepositoryPublic(rule)
case "onebot":
onebot.SendOneBotMsg(rule)
executorRegistry := GetGlobalExecutorRegistry()
if err := executorRegistry.Execute(rule); err != nil {
log.Printf("执行规则失败 [ID: %d, Type: %s]: %v", rule.ID, rule.Type, err)
}
}