diff --git a/README.md b/README.md index d458d49..2aaa277 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/faucet.go b/faucet.go index 8e5cff3..0a4bd95 100644 --- a/faucet.go +++ b/faucet.go @@ -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) diff --git a/faucet_test.go b/faucet_test.go index a17db71..e1db3b9 100644 --- a/faucet_test.go +++ b/faucet_test.go @@ -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) { diff --git a/handler.go b/handler.go index 90ba4d6..29d1ea0 100644 --- a/handler.go +++ b/handler.go @@ -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) +}