Skip to content

Commit

Permalink
Merge pull request #6 from skycoin/feat/add-dmsg-address-field-to-hea…
Browse files Browse the repository at this point in the history
…lth-endpoint

improve health endpoint
  • Loading branch information
jdknives authored Jun 19, 2023
2 parents b2b3fca + fc3bca0 commit 756760d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
9 changes: 8 additions & 1 deletion cmd/uptime-tracker/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ var (
testing bool
dmsgDisc string
sk cipher.SecKey
dmsgPort uint16
storeDataCutoff int
storeDataPath string
)
Expand All @@ -77,6 +78,7 @@ func init() {
rootCmd.Flags().BoolVarP(&testing, "testing", "t", false, "enable testing to start without redis")
rootCmd.Flags().StringVar(&dmsgDisc, "dmsg-disc", "http://dmsgd.skywire.skycoin.com", "url of dmsg-discovery")
rootCmd.Flags().Var(&sk, "sk", "dmsg secret key")
rootCmd.Flags().Uint16Var(&dmsgPort, "dmsgPort", dmsg.DefaultDmsgHTTPPort, "dmsg port value\r")
}

var rootCmd = &cobra.Command{
Expand Down Expand Up @@ -156,8 +158,13 @@ var rootCmd = &cobra.Command{
m = utmetrics.NewVictoriaMetrics()
}

var dmsgAddr string
if !pk.Null() {
dmsgAddr = fmt.Sprintf("%s:%d", pk.Hex(), dmsgPort)
}

enableMetrics := metricsAddr != ""
utAPI := api.New(logger, s, nonceStore, locDetails, enableLoadTesting, enableMetrics, m, storeDataCutoff, storeDataPath)
utAPI := api.New(logger, s, nonceStore, locDetails, enableLoadTesting, enableMetrics, m, storeDataCutoff, storeDataPath, dmsgAddr)

utPAPI := api.NewPrivate(logger, s)

Expand Down
7 changes: 6 additions & 1 deletion pkg/uptime-tracker/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ type API struct {
dailyUptimeCacheMu sync.RWMutex
storeUptimesCutoff int
storeUptimesPath string

dmsgAddr string
}

// PrivateAPI register all the PrivateAPI endpoints.
Expand All @@ -71,11 +73,12 @@ type PrivateAPI struct {
type HealthCheckResponse struct {
BuildInfo *buildinfo.Info `json:"build_info,omitempty"`
StartedAt time.Time `json:"started_at,omitempty"`
DmsgAddr string `json:"dmsg_address,omitempty"`
}

// New constructs a new API instance.
func New(log logrus.FieldLogger, s store.Store, nonceStore httpauth.NonceStore, locDetails geo.LocationDetails,
enableLoadTesting, enableMetrics bool, m utmetrics.Metrics, storeDataCutoff int, storeDataPath string) *API {
enableLoadTesting, enableMetrics bool, m utmetrics.Metrics, storeDataCutoff int, storeDataPath, dmsgAddr string) *API {
if log == nil {
log = logging.MustGetLogger("uptime_tracker")
}
Expand All @@ -88,6 +91,7 @@ func New(log logrus.FieldLogger, s store.Store, nonceStore httpauth.NonceStore,
startedAt: time.Now(),
storeUptimesCutoff: storeDataCutoff,
storeUptimesPath: storeDataPath,
dmsgAddr: dmsgAddr,
}

r := chi.NewRouter()
Expand Down Expand Up @@ -552,6 +556,7 @@ func (api *API) health(w http.ResponseWriter, r *http.Request) {
api.writeJSON(w, r, http.StatusOK, HealthCheckResponse{
BuildInfo: info,
StartedAt: api.startedAt,
DmsgAddr: api.dmsgAddr,
})
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/uptime-tracker/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestHandleUptimes(t *testing.T) {
nonceMock, err := httpauth.NewNonceStore(ctx, storeconfig.Config{Type: storeconfig.Memory}, "")
require.NoError(t, err)
api := New(nil, mock, nonceMock, geoFunc, false, false,
utmetrics.NewEmpty(), 0, "")
utmetrics.NewEmpty(), 0, "", "")

pk, _ := cipher.GenerateKeyPair()

Expand Down Expand Up @@ -75,7 +75,7 @@ func TestAPI_handleUpdate(t *testing.T) {
nonceMock, err := httpauth.NewNonceStore(ctx, storeconfig.Config{Type: storeconfig.Memory}, "")
require.NoError(t, err)
api := New(nil, mock, nonceMock, geoFunc, false, false,
utmetrics.NewEmpty(), 0, "")
utmetrics.NewEmpty(), 0, "", "")

t.Run("StatusOK", func(t *testing.T) {
w := httptest.NewRecorder()
Expand Down Expand Up @@ -106,7 +106,7 @@ func TestApi_UpdateRemovedMethod(t *testing.T) {
nonceMock, err := httpauth.NewNonceStore(ctx, storeconfig.Config{Type: storeconfig.Memory}, "")
require.NoError(t, err)
api := New(nil, mock, nonceMock, geoFunc, false, false,
utmetrics.NewEmpty(), 0, "")
utmetrics.NewEmpty(), 0, "", "")

t.Run("StatusGone", func(t *testing.T) {
w := httptest.NewRecorder()
Expand Down

0 comments on commit 756760d

Please sign in to comment.