Skip to content

Commit

Permalink
feat: add healthcheck handler & register it as endpoint (#49)
Browse files Browse the repository at this point in the history
* feat: add healthcheck handler & register it

* doc: add health endpoint in the readme

* feat: add option to enable/disable healthcheck endpoint & add tests

* fix: remove optional healthcheck endpoint & fix lint errors

* fix: set the healthcheck endpoint before user-set requests handlers

* doc: update faucet test comment
  • Loading branch information
MikaelVallenet committed Aug 6, 2024
1 parent 0a712ca commit 88dfbea
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 2 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ It should print something like the following. (Note the port number at the end.)
curl --location --request POST 'http://localhost:8545' --header 'Content-Type: application/json' --data '{"To": "g1juz2yxmdsa6audkp6ep9vfv80c8p5u76e03vvh"}'
```

5. To ensure the faucet is listening to requests, you can ping the health endpoint:
```bash
curl --location --request GET 'http://localhost:8545/health'
```

### As a library

To add `faucet` to your Go project, simply run:
Expand Down
3 changes: 3 additions & 0 deletions faucet.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ func NewFaucet(
f.mux.Use(middleware)
}

// Register the health check handler
f.mux.Get("/health", f.healthcheckHandler)

// Set up the request handlers
for _, handler := range f.handlers {
f.mux.Post(handler.Pattern, handler.HandlerFunc)
Expand Down
4 changes: 2 additions & 2 deletions faucet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@ func TestFaucet_NewFaucet(t *testing.T) {

// Make sure the handler was set
routes := f.mux.Routes()
require.Len(t, routes, len(handlers)+1) // base "/" handler as well
require.Len(t, routes, len(handlers)+2) // base "/" & "/health" handlers as well

assert.Equal(t, handlers[0].Pattern, routes[1].Pattern)
assert.Equal(t, handlers[0].Pattern, routes[2].Pattern)
})

t.Run("with logger", func(t *testing.T) {
Expand Down
5 changes: 5 additions & 0 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,8 @@ func extractSendAmount(request Request) (std.Coins, error) {

return amount, nil
}

// healthcheckHandler is the default health check handler for the faucet
func (f *Faucet) healthcheckHandler(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
}

0 comments on commit 88dfbea

Please sign in to comment.