Skip to content

Commit

Permalink
feat(api): Add healthz endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Will Cory authored and Will Cory committed Jun 9, 2023
1 parent 661b5d6 commit 4e90ad1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
5 changes: 5 additions & 0 deletions indexer/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ func (a *Api) WithdrawalsHandler(w http.ResponseWriter, r *http.Request) {
jsonResponse(w, response, http.StatusOK)
}

func (a *Api) HealthzHandler(w http.ResponseWriter, r *http.Request) {
jsonResponse(w, "ok", http.StatusOK)
}

func jsonResponse(w http.ResponseWriter, data interface{}, statusCode int) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(statusCode)
Expand All @@ -95,6 +99,7 @@ func NewApi(bv database.BridgeView) *Api {
// with go-ethereum and throw a friendly error message
r.Get("/api/v0/deposits/{address:.+}", api.DepositsHandler)
r.Get("/api/v0/withdrawals/{address:.+}", api.WithdrawalsHandler)
r.Get("/healthz", api.HealthzHandler)

return api

Expand Down
11 changes: 11 additions & 0 deletions indexer/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@ func (mbv *MockBridgeView) WithdrawalsByAddress(address common.Address) ([]*data
}, nil
}

func TestHealthz(t *testing.T) {
api := NewApi(&MockBridgeView{})
request, err := http.NewRequest("GET", "/healthz", nil)
assert.Nil(t, err)

responseRecorder := httptest.NewRecorder()
api.Router.ServeHTTP(responseRecorder, request)

assert.Equal(t, http.StatusOK, responseRecorder.Code)
}

func TestDepositsHandler(t *testing.T) {
api := NewApi(&MockBridgeView{})
request, err := http.NewRequest("GET", "/api/v0/deposits/0x123", nil)
Expand Down

0 comments on commit 4e90ad1

Please sign in to comment.