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) }