From df1e45dd41a22b3eedb2269e8cfa6f77aff96642 Mon Sep 17 00:00:00 2001 From: Mohammed Date: Tue, 20 Jun 2023 12:17:54 +0330 Subject: [PATCH] add /dmsghttp config to config bootstrap --- pkg/config-bootstrapper/api/api.go | 71 +++++++++++++++++++++++++++--- 1 file changed, 66 insertions(+), 5 deletions(-) diff --git a/pkg/config-bootstrapper/api/api.go b/pkg/config-bootstrapper/api/api.go index 3e84b725..fe2e23c1 100644 --- a/pkg/config-bootstrapper/api/api.go +++ b/pkg/config-bootstrapper/api/api.go @@ -3,6 +3,8 @@ package api import ( "encoding/json" + "fmt" + "io" "net/http" "strings" "sync" @@ -29,6 +31,9 @@ type API struct { services *visorconfig.Services + dmsghttpConf httputil.DMSGHTTPConf + dmsghttpConfTs time.Time + closeOnce sync.Once closeC chan struct{} } @@ -75,10 +80,11 @@ func New(log *logging.Logger, conf Config, domain string) *API { } api := &API{ - log: log, - startedAt: time.Now(), - services: services, - closeC: make(chan struct{}), + log: log, + startedAt: time.Now(), + services: services, + dmsghttpConfTs: time.Now().Add(-5 * time.Minute), + closeC: make(chan struct{}), } r := chi.NewRouter() @@ -90,6 +96,7 @@ func New(log *logging.Logger, conf Config, domain string) *API { r.Use(httputil.SetLoggerMiddleware(log)) r.Get("/health", api.health) r.Get("/", api.config) + r.Get("/dmsghttp", api.dmsghttp) api.Handler = r @@ -116,7 +123,6 @@ func (a *API) health(w http.ResponseWriter, r *http.Request) { } func (a *API) config(w http.ResponseWriter, r *http.Request) { - a.writeJSON(w, r, http.StatusOK, a.services) } @@ -137,3 +143,58 @@ func (a *API) writeJSON(w http.ResponseWriter, r *http.Request, code int, object a.logger(r).WithError(err).Errorf("failed to write json response") } } + +func (a *API) dmsghttp(w http.ResponseWriter, r *http.Request) { + if time.Now().Add(-5 * time.Minute).After(a.dmsghttpConfTs) { + a.dmsghttpConf = a.dmsghttpConfGen() + a.dmsghttpConfTs = time.Now() + } + a.writeJSON(w, r, http.StatusOK, a.dmsghttpConf) +} + +func (a *API) dmsghttpConfGen() httputil.DMSGHTTPConf { + var dmsghttpConf httputil.DMSGHTTPConf + dmsghttpConf.DMSGServers = fetchDMSGServers(a.services.DmsgDiscovery) + dmsghttpConf.AddressResolver = fetchDMSGAddress(a.services.AddressResolver) + dmsghttpConf.DMSGDiscovery = fetchDMSGAddress(a.services.DmsgDiscovery) + dmsghttpConf.RouteFinder = fetchDMSGAddress(a.services.RouteFinder) + dmsghttpConf.ServiceDiscovery = fetchDMSGAddress(a.services.ServiceDiscovery) + dmsghttpConf.TranspordDiscovery = fetchDMSGAddress(a.services.TransportDiscovery) + dmsghttpConf.UptimeTracker = fetchDMSGAddress(a.services.UptimeTracker) + + return dmsghttpConf +} + +func fetchDMSGAddress(url string) string { + resp, err := http.Get(fmt.Sprintf("%s/health", url)) + if err != nil { + return "" + } + body, err := io.ReadAll(resp.Body) + if err != nil { + return "" + } + var healthResponse httputil.HealthCheckResponse + err = json.Unmarshal(body, &healthResponse) + if err != nil { + return "" + } + return healthResponse.DmsgAddr +} + +func fetchDMSGServers(url string) []httputil.DMSGServersConf { + var dmsgServersList []httputil.DMSGServersConf + resp, err := http.Get(fmt.Sprintf("%s/dmsg-discovery/all_servers", url)) + if err != nil { + return dmsgServersList + } + body, err := io.ReadAll(resp.Body) + if err != nil { + return dmsgServersList + } + err = json.Unmarshal(body, &dmsgServersList) + if err != nil { + return dmsgServersList + } + return dmsgServersList +}