优化结构

This commit is contained in:
Yvan
2026-08-29 08:40:15 +08:00
parent b6d25c1a0f
commit 509df03d61
17 changed files with 234 additions and 40 deletions
@@ -6,6 +6,7 @@ import (
"errors"
"fmt"
bizpayment "kra/internal/biz/payment"
"net"
"sort"
"strconv"
"strings"
@@ -285,6 +286,44 @@ func validateCommunicationIntegrationConfig(kind, provider string, values map[st
if integrationInt64(values, "reconnect_interval", 0) <= 0 {
return errors.New("rabbitmq reconnect_interval 必须大于 0")
}
case IntegrationKindMQ + "/kafka":
brokers := integrationStrings(values, "brokers")
if len(brokers) == 0 {
return errors.New("kafka brokers 不能为空")
}
for _, broker := range brokers {
host, portText, err := net.SplitHostPort(broker)
port, parseErr := strconv.Atoi(portText)
if err != nil || parseErr != nil || strings.TrimSpace(host) == "" || port < 1 || port > 65535 {
return fmt.Errorf("kafka broker %q 必须是有效的 host:port 地址", broker)
}
}
username := integrationText(values, "username")
password := integrationText(values, "password")
if (username == "") != (password == "") {
return errors.New("kafka username 和 password 必须同时配置")
}
startOffset := strings.ToLower(integrationText(values, "start_offset"))
if startOffset != "earliest" && startOffset != "latest" {
return errors.New("kafka start_offset 必须是 earliest 或 latest")
}
minBytes := integrationInt64(values, "min_bytes", 0)
maxBytes := integrationInt64(values, "max_bytes", 0)
if minBytes <= 0 || maxBytes < minBytes {
return errors.New("kafka max_bytes 必须大于等于 min_bytes,且二者必须大于 0")
}
if integrationInt64(values, "max_wait", 0) <= 0 {
return errors.New("kafka max_wait 必须大于 0")
}
if integrationInt64(values, "connect_timeout", 0) <= 0 {
return errors.New("kafka connect_timeout 必须大于 0")
}
if integrationInt64(values, "reconnect_interval", 0) <= 0 {
return errors.New("kafka reconnect_interval 必须大于 0")
}
if integrationBool(values, "tls_skip_verify") && !integrationBool(values, "tls") {
return errors.New("kafka tls_skip_verify 仅能在启用 TLS 时使用")
}
case IntegrationKindWebSocket + "/melody":
path := integrationText(values, "path")
if !strings.HasPrefix(path, "/") {
@@ -391,6 +430,34 @@ func integrationText(values map[string]any, key string) string {
return strings.TrimSpace(fmt.Sprint(value))
}
func integrationStrings(values map[string]any, key string) []string {
switch items := values[key].(type) {
case []string:
result := make([]string, 0, len(items))
for _, item := range items {
if item = strings.TrimSpace(item); item != "" {
result = append(result, item)
}
}
return result
case []any:
result := make([]string, 0, len(items))
for _, item := range items {
if value := strings.TrimSpace(fmt.Sprint(item)); value != "" {
result = append(result, value)
}
}
return result
default:
return nil
}
}
func integrationBool(values map[string]any, key string) bool {
value, _ := values[key].(bool)
return value
}
func integrationFirst(values map[string]any, keys ...string) string {
for _, key := range keys {
if value := integrationText(values, key); value != "" {
@@ -110,6 +110,26 @@ var integrationDefinitions = map[string][]IntegrationConfigDefinition{
{Key: "reconnect_interval", Label: "重连退避上限(秒)", Type: "number", Required: true, Description: "网络中断后自动重连的最大退避间隔。"},
},
},
{
Kind: IntegrationKindMQ, Provider: "kafka", Name: "Kafka", Description: "Apache Kafka 分布式事件队列",
Defaults: map[string]any{"brokers": []string{"127.0.0.1:9092"}, "client_id": "kra", "group_id": "kra", "username": "", "password": "", "tls": false, "tls_skip_verify": false, "start_offset": "earliest", "min_bytes": 1, "max_bytes": 10485760, "max_wait": 1, "connect_timeout": 10, "reconnect_interval": 5, "allow_auto_topic_creation": false},
Fields: []IntegrationConfigField{
{Key: "brokers", Label: "Broker 地址", Type: "string-list", Required: true, Placeholder: "127.0.0.1:9092", Description: "每行一个 host:port 地址。"},
{Key: "client_id", Label: "客户端 ID", Type: "text", Required: true, Placeholder: "kra"},
{Key: "group_id", Label: "消费组 ID", Type: "text", Required: true, Placeholder: "kra"},
{Key: "username", Label: "SASL 用户名", Type: "text"},
{Key: "password", Label: "SASL 密码", Type: "password", Secret: true},
{Key: "tls", Label: "启用 TLS", Type: "switch"},
{Key: "tls_skip_verify", Label: "跳过 TLS 证书校验", Type: "switch", Description: "仅用于受控测试环境。"},
{Key: "start_offset", Label: "初始消费位置", Type: "select", Required: true, Options: []IntegrationConfigOption{{Label: "earliest", Value: "earliest"}, {Label: "latest", Value: "latest"}}},
{Key: "min_bytes", Label: "最小拉取字节数", Type: "number", Required: true},
{Key: "max_bytes", Label: "最大拉取字节数", Type: "number", Required: true},
{Key: "max_wait", Label: "最大拉取等待(秒)", Type: "number", Required: true},
{Key: "connect_timeout", Label: "连接超时(秒)", Type: "number", Required: true},
{Key: "reconnect_interval", Label: "重连间隔(秒)", Type: "number", Required: true},
{Key: "allow_auto_topic_creation", Label: "允许自动创建 Topic", Type: "switch"},
},
},
{
Kind: IntegrationKindMQ, Provider: "rabbitmq", Name: "RabbitMQ", Description: "RabbitMQ AMQP 消息队列",
Defaults: map[string]any{"host": "127.0.0.1", "port": 5672, "username": "guest", "password": "guest", "vhost": "/", "exchange": "kra", "exchange_type": "topic", "queue": "kra", "routing_key": "#", "durable": true, "auto_delete": false, "prefetch_count": 10, "heartbeat": 10, "connect_timeout": 10, "reconnect_interval": 5, "tls": false},