Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dmsghttp config endpoint #16

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
Loading