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

feat: add healthcheck handler & register it as endpoint #49

Merged
Merged
Show file tree
Hide file tree
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
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)
}
Loading