Skip to content

Commit 88dfbea

Browse files
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
1 parent 0a712ca commit 88dfbea

File tree

4 files changed

+15
-2
lines changed

4 files changed

+15
-2
lines changed

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ It should print something like the following. (Note the port number at the end.)
6363
curl --location --request POST 'http://localhost:8545' --header 'Content-Type: application/json' --data '{"To": "g1juz2yxmdsa6audkp6ep9vfv80c8p5u76e03vvh"}'
6464
```
6565

66+
5. To ensure the faucet is listening to requests, you can ping the health endpoint:
67+
```bash
68+
curl --location --request GET 'http://localhost:8545/health'
69+
```
70+
6671
### As a library
6772

6873
To add `faucet` to your Go project, simply run:

faucet.go

+3
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ func NewFaucet(
9898
f.mux.Use(middleware)
9999
}
100100

101+
// Register the health check handler
102+
f.mux.Get("/health", f.healthcheckHandler)
103+
101104
// Set up the request handlers
102105
for _, handler := range f.handlers {
103106
f.mux.Post(handler.Pattern, handler.HandlerFunc)

faucet_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,9 @@ func TestFaucet_NewFaucet(t *testing.T) {
122122

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

127-
assert.Equal(t, handlers[0].Pattern, routes[1].Pattern)
127+
assert.Equal(t, handlers[0].Pattern, routes[2].Pattern)
128128
})
129129

130130
t.Run("with logger", func(t *testing.T) {

handler.go

+5
Original file line numberDiff line numberDiff line change
@@ -202,3 +202,8 @@ func extractSendAmount(request Request) (std.Coins, error) {
202202

203203
return amount, nil
204204
}
205+
206+
// healthcheckHandler is the default health check handler for the faucet
207+
func (f *Faucet) healthcheckHandler(w http.ResponseWriter, _ *http.Request) {
208+
w.WriteHeader(http.StatusOK)
209+
}

0 commit comments

Comments
 (0)