feat(drivers): 饭碗警告(WIP)

This commit is contained in:
mei
2026-07-29 11:24:23 +08:00
parent 4a61527952
commit 9220cf82aa
8 changed files with 125 additions and 2 deletions
-2
View File
@@ -9,8 +9,6 @@ import (
)
// DingTalkExecutor 钉钉机器人执行器
//
// 之前只注册了校验器却没有执行器,导致钉钉规则可以创建但永远不会被执行。
type DingTalkExecutor struct{}
func (e *DingTalkExecutor) GetType() string {
+21
View File
@@ -0,0 +1,21 @@
package fwalert
import (
"fmt"
"github.com/go-resty/resty/v2"
)
// send 发送消息
func send(config *FwAlertRuleConfig) error {
restyClient := resty.New()
req := restyClient.R()
resp, err := req.Execute("GET", config.WebhookURL+"?message="+config.Msg)
if err != nil {
return err
}
if resp.StatusCode() != 200 {
return fmt.Errorf("发送饭碗警告失败: %s", resp.Status())
}
return nil
}
+8
View File
@@ -0,0 +1,8 @@
package fwalert
const RuleTypeFwalert = "fwalert"
type FwAlertRuleConfig struct {
WebhookURL string `json:"webhook_url"`
Msg string `json:"msg"`
}
+62
View File
@@ -0,0 +1,62 @@
package fwalert
import (
"encoding/json"
"fmt"
"net/url"
"github.com/ssdomei232/goodBaby/internal/meta"
)
type FwalertRuleValidator struct{}
func (v *FwalertRuleValidator) GetType() string {
return RuleTypeFwalert
}
func (v *FwalertRuleValidator) Validate(configJSON string) error {
var config FwAlertRuleConfig
if err := json.Unmarshal([]byte(configJSON), &config); err != nil {
return fmt.Errorf("解析饭碗警告规则配置失败: %v", err)
}
parsed, err := url.Parse(config.WebhookURL)
if err != nil {
return fmt.Errorf("饭碗警告规则配置中 webhook_url 格式不正确: %v", err)
}
if parsed.Scheme != "http" && parsed.Scheme != "https" {
return fmt.Errorf("饭碗警告规则配置中 webhook_url 必须是 HTTP 或 HTTPS URL")
}
if config.Msg == "" {
return fmt.Errorf("饭碗警告规则配置中 msg 不能为空")
}
if len([]rune(config.Msg)) > 1000 {
return fmt.Errorf("饭碗警告内容过长")
}
return nil
}
func (v *FwalertRuleValidator) Meta() meta.RuleMeta {
return meta.RuleMeta{
Type: RuleTypeFwalert,
Label: "发送饭碗警告",
Description: "触发时发送饭碗警告消息。",
Docs: "docs/fwalert-config.md",
AccountType: "",
Fields: []meta.Field{
{Key: "webhook_url", Label: "Webhook URL", Type: meta.FieldString, Required: true, Help: "饭碗警告的触发地址", Placeholder: "https://fwalert.com/xxx-xxx-xxx-xxx-xxx"},
{Key: "msg", Label: "消息内容", Type: meta.FieldTextarea, Required: true, Help: "最大长度 1000 个字符"},
},
}
}
// ParseRuleConfig 解析饭碗警告规则配置
func ParseRuleConfig(configJSON string) (*FwAlertRuleConfig, error) {
var config FwAlertRuleConfig
if err := json.Unmarshal([]byte(configJSON), &config); err != nil {
return nil, fmt.Errorf("解析饭碗警告规则配置失败: %v", err)
}
return &config, nil
}
+29
View File
@@ -0,0 +1,29 @@
package fwalert
import (
"context"
"log"
"github.com/ssdomei232/goodBaby/internal/retry"
"github.com/ssdomei232/goodBaby/model"
)
// FwalertExecutor 饭碗警告执行器
type FwalertExecutor struct{}
func (e *FwalertExecutor) GetType() string {
return RuleTypeFwalert
}
func (e *FwalertExecutor) Execute(ctx context.Context, rule *model.Rule) error {
log.Printf("执行饭碗警告规则: %s (ID: %d)", rule.Name, rule.ID)
config, err := ParseRuleConfig(rule.ConfigJson)
if err != nil {
return err
}
return retry.Do(ctx, func() error {
return send(config)
})
}