44 lines
1.3 KiB
Go
44 lines
1.3 KiB
Go
package mesh
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"strings"
|
|
)
|
|
|
|
type ProductionForwardTransport interface {
|
|
SendProduction(ctx context.Context, nextNodeID string, envelope ProductionEnvelope) (ProductionForwardResult, error)
|
|
}
|
|
|
|
type HTTPProductionForwardTransport struct {
|
|
PeerURLs map[string]string
|
|
HTTPClient *http.Client
|
|
}
|
|
|
|
func NewHTTPProductionForwardTransport(peerURLs map[string]string) *HTTPProductionForwardTransport {
|
|
normalized := make(map[string]string, len(peerURLs))
|
|
for nodeID, baseURL := range peerURLs {
|
|
nodeID = strings.TrimSpace(nodeID)
|
|
baseURL = strings.TrimRight(strings.TrimSpace(baseURL), "/")
|
|
if nodeID != "" && baseURL != "" {
|
|
normalized[nodeID] = baseURL
|
|
}
|
|
}
|
|
return &HTTPProductionForwardTransport{PeerURLs: normalized}
|
|
}
|
|
|
|
func (t *HTTPProductionForwardTransport) SendProduction(ctx context.Context, nextNodeID string, envelope ProductionEnvelope) (ProductionForwardResult, error) {
|
|
if t == nil {
|
|
return ProductionForwardResult{}, ErrForwardPeerUnavailable
|
|
}
|
|
baseURL := strings.TrimRight(strings.TrimSpace(t.PeerURLs[nextNodeID]), "/")
|
|
if baseURL == "" {
|
|
return ProductionForwardResult{}, ErrForwardPeerUnavailable
|
|
}
|
|
client := NewClient(baseURL)
|
|
if t.HTTPClient != nil {
|
|
client.HTTPClient = t.HTTPClient
|
|
}
|
|
return client.SendProduction(ctx, envelope)
|
|
}
|