Skip to content

Commit

Permalink
Merge pull request #16 from mrpalide/feat/add-dmsghttp-to-config-service
Browse files Browse the repository at this point in the history
dmsghttp config endpoint
  • Loading branch information
jdknives authored Nov 18, 2023
2 parents 16664e3 + df1e45d commit f2b804f
Showing 1 changed file with 66 additions and 5 deletions.
71 changes: 66 additions & 5 deletions pkg/config-bootstrapper/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package api

import (
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"sync"
Expand All @@ -29,6 +31,9 @@ type API struct {

services *visorconfig.Services

dmsghttpConf httputil.DMSGHTTPConf
dmsghttpConfTs time.Time

closeOnce sync.Once
closeC chan struct{}
}
Expand Down Expand Up @@ -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()
Expand All @@ -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

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

Expand All @@ -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
}

0 comments on commit f2b804f

Please sign in to comment.