nats discovery

This commit is contained in:
mr 2024-10-17 14:14:04 +02:00
parent dbcd9cf004
commit 847ef2e95c

View File

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