From a42b6198cb8e2ff19f6499260a7c090dc3411087 Mon Sep 17 00:00:00 2001 From: mr Date: Thu, 22 Aug 2024 15:18:59 +0200 Subject: [PATCH] Discovery Nats automate --- models/peer/peer_cache.go | 37 ++++++++++++++++--------------------- 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/models/peer/peer_cache.go b/models/peer/peer_cache.go index 6ae2440..4baeaec 100644 --- a/models/peer/peer_cache.go +++ b/models/peer/peer_cache.go @@ -33,6 +33,20 @@ type PeerCache struct { Executions []*PeerExecution } +func (p *PeerCache) urlFormat(url string) string { + if strings.Contains(url, "localhost") || strings.Contains(url, "127.0.0.1") { + url = strings.ReplaceAll(url, "localhost", utils.PEER.API()) + url = strings.ReplaceAll(url, "127.0.0.1", utils.PEER.API()) + r := regexp.MustCompile("(:[0-9]+)") + t := r.FindString(url) + if t != "" { + url = strings.Replace(url, t, ":8080", -1) + } + r.ReplaceAllString(url, ":8080") + } + return url +} + func (p *PeerCache) checkPeerStatus(peerID string, caller *tools.HTTPCaller) bool { api := tools.API{} access := (&Peer{}).GetAccessor(nil) @@ -51,17 +65,7 @@ func (p *PeerCache) checkPeerStatus(peerID string, caller *tools.HTTPCaller) boo if meth == "" { return false } - url := res.(*Peer).Url + meth - if strings.Contains(url, "localhost") || strings.Contains(url, "127.0.0.1") { - url = strings.ReplaceAll(url, "localhost", utils.PEER.API()) - url = strings.ReplaceAll(url, "127.0.0.1", utils.PEER.API()) - r := regexp.MustCompile("(:[0-9]+)") - t := r.FindString(url) - if t != "" { - url = strings.Replace(url, t, ":8080", -1) - } - r.ReplaceAllString(url, ":8080") - } + url := p.urlFormat(res.(*Peer).Url + meth) fmt.Println("checkPeerStatus AFT 4", url) state := api.CheckRemotePeer(url) fmt.Println("checkPeerStatus AFT 4", state, tools.DEAD) @@ -78,16 +82,7 @@ func (p *PeerCache) GetAccessor(caller *tools.HTTPCaller) utils.Accessor { func (p *PeerCache) LaunchPeerExecution(peerID string, isMySelf bool, dataID string, url string, dt utils.DataType, method tools.METHOD, body map[string]interface{}, caller *tools.HTTPCaller) (*PeerExecution, error) { fmt.Println("LaunchPeerExecution", url, strings.Contains(url, "localhost") || strings.Contains(url, "127.0.0.1")) - if strings.Contains(url, "localhost") || strings.Contains(url, "127.0.0.1") { - url = strings.ReplaceAll(url, "localhost", dt.API()) - url = strings.ReplaceAll(url, "127.0.0.1", dt.API()) - r := regexp.MustCompile("(:[0-9]+)") - t := r.FindString(url) - if t != "" { - url = strings.Replace(url, t, ":8080", -1) - } - r.ReplaceAllString(url, ":8080") - } + url = p.urlFormat(url) fmt.Println("LaunchPeerExecution AFT", url, dt.API()) var err error b := []byte{}