From 847ef2e95c64fb27b004ab6be1d26f95ae2ea4dc Mon Sep 17 00:00:00 2001 From: mr Date: Thu, 17 Oct 2024 14:14:04 +0200 Subject: [PATCH] nats discovery --- tools/api.go | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/tools/api.go b/tools/api.go index f54fa61..8eb2433 100644 --- a/tools/api.go +++ b/tools/api.go @@ -3,6 +3,7 @@ package tools import ( "encoding/json" "errors" + "strings" "cloud.o-forge.io/core/oc-lib/config" "cloud.o-forge.io/core/oc-lib/dbs/mongo" @@ -50,6 +51,16 @@ func (s State) String() string { type API struct{} +func (a *API) Discovered() { + respondToDiscovery := func(m map[string]interface{}) { + if len(m) == 0 { + a.SubscribeRouter() + } + } + a.ListenRouter(respondToDiscovery) + a.SubscribeRouter() +} + // GetState returns the state of the API func (a *API) GetState() (State, int, error) { // Check if the database is up @@ -72,8 +83,7 @@ func (a *API) GetState() (State, int, error) { } func (a *API) ListenRouter(exec func(msg map[string]interface{})) { - nats := NewNATSCaller() - go nats.ListenNats(DISCOVERY.GenerateKey("api"), exec) + go NewNATSCaller().ListenNats(DISCOVERY.GenerateKey("api"), exec) } func (a *API) SubscribeRouter() { @@ -84,7 +94,8 @@ func (a *API) SubscribeRouter() { for k := range info.GetMethod() { methods = append(methods, k) } - discovery[info.GetPattern()] = methods + path := strings.ReplaceAll(info.GetPattern(), "/oc", "/"+config.GetAppName()) + discovery[path] = methods } nats.SetNATSPub("api", DISCOVERY, discovery) }