From 88dfbea1d857c3a7e7d6a10dffc4ca28714ef225 Mon Sep 17 00:00:00 2001 From: Mikael VALLENET Date: Wed, 7 Aug 2024 04:03:15 +0700 Subject: [PATCH] feat: add healthcheck handler & register it as endpoint (#49) * 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 --- README.md | 5 +++++ faucet.go | 3 +++ faucet_test.go | 4 ++-- handler.go | 5 +++++ 4 files changed, 15 insertions(+), 2 deletions(-) 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) +}