From 950bbf1d7bf266292b3c31875a895e6549292f6c Mon Sep 17 00:00:00 2001 From: mr Date: Wed, 21 Aug 2024 16:03:56 +0200 Subject: [PATCH] Booking trace API --- entrypoint.go | 8 ++++++-- models/peer/peer_cache.go | 14 +++++++++++--- tools/api.go | 2 +- tools/remote_caller.go | 2 +- 4 files changed, 19 insertions(+), 7 deletions(-) diff --git a/entrypoint.go b/entrypoint.go index c03427c..b1b54ac 100644 --- a/entrypoint.go +++ b/entrypoint.go @@ -69,10 +69,14 @@ type LibData struct { Err string `bson:"error" json:"error"` } -var paths map[LibDataEnum]interface{} = map[LibDataEnum]interface{}{} +var paths map[LibDataEnum]string = map[LibDataEnum]string{} + +func GetPaths() map[LibDataEnum]string { + return paths +} func GetPath(collection LibDataEnum) string { - return paths[collection].(string) + return paths[collection] } func AddPath(collection LibDataEnum, path string) { diff --git a/models/peer/peer_cache.go b/models/peer/peer_cache.go index ef4c664..8ae81af 100644 --- a/models/peer/peer_cache.go +++ b/models/peer/peer_cache.go @@ -32,7 +32,7 @@ type PeerCache struct { Executions []*PeerExecution } -func (p *PeerCache) checkPeerStatus(peerID string) bool { +func (p *PeerCache) checkPeerStatus(peerID string, caller *tools.HTTPCaller) bool { api := tools.API{} access := (&Peer{}).GetAccessor(nil) res, code, _ := access.LoadOne(peerID) @@ -44,7 +44,15 @@ func (p *PeerCache) checkPeerStatus(peerID string) bool { url = strings.ReplaceAll(url, "localhost", utils.PEER.API()) } - state := api.CheckRemotePeer(url + ":8093") + methods := caller.URLS[utils.PEER.String()] + if methods == nil { + return false + } + meth := methods[tools.POST] + if meth == "" { + return false + } + state := api.CheckRemotePeer(url + meth) return state != tools.DEAD } @@ -69,7 +77,7 @@ func (p *PeerCache) LaunchPeerExecution(peerID string, isMySelf bool, dataID str if _, ok := methods[method]; !ok { return nil, errors.New("no path found") } - if !p.checkPeerStatus(peerID) { + if !p.checkPeerStatus(peerID, caller) { return nil, err } if method == tools.POST { diff --git a/tools/api.go b/tools/api.go index 354926a..1ad17b0 100644 --- a/tools/api.go +++ b/tools/api.go @@ -60,7 +60,7 @@ func (a *API) CheckRemotePeer(url string) State { // Check if the database is up caller := NewHTTPCaller(map[string]map[METHOD]string{}) var resp APIStatusResponse - b, err := caller.CallGet(url, "/status") + b, err := caller.CallPost(url, "/status", []string{}) if err != nil { return DEAD } diff --git a/tools/remote_caller.go b/tools/remote_caller.go index 95856e9..320d690 100644 --- a/tools/remote_caller.go +++ b/tools/remote_caller.go @@ -46,7 +46,7 @@ func (caller *HTTPCaller) CallDelete(url string, subpath string) ([]byte, error) return io.ReadAll(resp.Body) } -func (caller *HTTPCaller) CallPost(url string, subpath string, body map[string]interface{}) ([]byte, error) { +func (caller *HTTPCaller) CallPost(url string, subpath string, body interface{}) ([]byte, error) { postBody, _ := json.Marshal(body) responseBody := bytes.NewBuffer(postBody) resp, err := http.Post(url+subpath, "application/json", responseBody)