优化结构
This commit is contained in:
@@ -10,7 +10,11 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
const IntegrationKindPayment = "payment"
|
||||
const (
|
||||
IntegrationKindPayment = "payment"
|
||||
IntegrationKindMQ = "mq"
|
||||
IntegrationKindWebSocket = "websocket"
|
||||
)
|
||||
|
||||
type IntegrationConfig struct {
|
||||
Kind string
|
||||
@@ -150,10 +154,79 @@ func mergeIntegrationDefaults(defaults, values map[string]any) map[string]any {
|
||||
}
|
||||
|
||||
func ValidateIntegrationConfig(kind, provider string, values map[string]any) error {
|
||||
if normalizeIntegrationPart(kind) != IntegrationKindPayment {
|
||||
kind = normalizeIntegrationPart(kind)
|
||||
provider = normalizeIntegrationPart(provider)
|
||||
switch kind {
|
||||
case IntegrationKindPayment:
|
||||
return validatePaymentIntegrationConfig(provider, values)
|
||||
case IntegrationKindMQ, IntegrationKindWebSocket:
|
||||
return validateCommunicationIntegrationConfig(kind, provider, values)
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
return validatePaymentIntegrationConfig(normalizeIntegrationPart(provider), values)
|
||||
}
|
||||
|
||||
func validateCommunicationIntegrationConfig(kind, provider string, values map[string]any) error {
|
||||
definition, ok := IntegrationDefinition(kind, provider)
|
||||
if !ok {
|
||||
return errors.New("不支持的通信集成")
|
||||
}
|
||||
for _, field := range definition.Fields {
|
||||
if field.Required && integrationText(values, field.Key) == "" {
|
||||
return fmt.Errorf("%s 缺少配置字段 %s", provider, field.Key)
|
||||
}
|
||||
}
|
||||
|
||||
switch kind + "/" + provider {
|
||||
case IntegrationKindMQ + "/emqx":
|
||||
broker := strings.ToLower(integrationText(values, "broker"))
|
||||
if !strings.HasPrefix(broker, "tcp://") && !strings.HasPrefix(broker, "ssl://") && !strings.HasPrefix(broker, "ws://") && !strings.HasPrefix(broker, "wss://") && !strings.HasPrefix(broker, "mqtt://") {
|
||||
return errors.New("emqx broker 必须使用 tcp、ssl、ws、wss 或 mqtt 协议")
|
||||
}
|
||||
if keepAlive := integrationInt64(values, "keep_alive", 0); keepAlive <= 0 {
|
||||
return errors.New("emqx keep_alive 必须大于 0")
|
||||
}
|
||||
if timeout := integrationInt64(values, "connect_timeout", 0); timeout <= 0 {
|
||||
return errors.New("emqx connect_timeout 必须大于 0")
|
||||
}
|
||||
case IntegrationKindMQ + "/rabbitmq":
|
||||
port := integrationInt64(values, "port", 0)
|
||||
if port < 1 || port > 65535 {
|
||||
return errors.New("rabbitmq port 必须在 1-65535 之间")
|
||||
}
|
||||
exchangeType := strings.ToLower(integrationText(values, "exchange_type"))
|
||||
if exchangeType != "direct" && exchangeType != "fanout" && exchangeType != "topic" && exchangeType != "headers" {
|
||||
return errors.New("rabbitmq exchange_type 必须是 direct、fanout、topic 或 headers")
|
||||
}
|
||||
if integrationInt64(values, "prefetch_count", -1) < 0 {
|
||||
return errors.New("rabbitmq prefetch_count 不能小于 0")
|
||||
}
|
||||
if integrationInt64(values, "heartbeat", -1) < 0 {
|
||||
return errors.New("rabbitmq heartbeat 不能小于 0")
|
||||
}
|
||||
if integrationInt64(values, "connect_timeout", 0) <= 0 {
|
||||
return errors.New("rabbitmq connect_timeout 必须大于 0")
|
||||
}
|
||||
case IntegrationKindWebSocket + "/melody":
|
||||
path := integrationText(values, "path")
|
||||
if !strings.HasPrefix(path, "/") {
|
||||
return errors.New("websocket path 必须以 / 开头")
|
||||
}
|
||||
for _, key := range []string{"write_wait", "pong_wait", "ping_period"} {
|
||||
value := integrationText(values, key)
|
||||
if value == "" {
|
||||
continue
|
||||
}
|
||||
duration, err := time.ParseDuration(value)
|
||||
if err != nil || duration <= 0 {
|
||||
return fmt.Errorf("websocket %s 必须是大于 0 的时长", key)
|
||||
}
|
||||
}
|
||||
if integrationInt64(values, "max_message_size", -1) < 0 || integrationInt64(values, "message_buffer_size", -1) < 0 {
|
||||
return errors.New("websocket 消息大小和缓冲区不能小于 0")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validatePaymentIntegrationConfig(provider string, values map[string]any) error {
|
||||
|
||||
Reference in New Issue
Block a user