Skip to content

Commit ab85811

Browse files
authored
Allow a server api to implement a custom healthcheck (#109)
1 parent 0aa04a3 commit ab85811

File tree

3 files changed

+49
-8
lines changed

3 files changed

+49
-8
lines changed

router/middleware.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,7 @@ func HealthCheck(route string, f APIHandler) Middleware {
9090
w.WriteHeader(http.StatusOK)
9191
return
9292
}
93-
if err := f(w, r); err != nil {
94-
HandleError(f(w, r), w, r)
95-
}
93+
HandleError(f(w, r), w, r)
9694
return
9795
}
9896
next.ServeHTTP(w, r)

server/server.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,22 @@ type APIDefinition interface {
3333
Stop()
3434
}
3535

36+
// HealthChecker is used to run a custom health check
37+
// Implement it on your API if you want it to be checked
38+
// when the healthcheck is called
39+
type HealthChecker interface {
40+
Healthy(w http.ResponseWriter, r *http.Request) *router.HTTPError
41+
}
42+
3643
func New(log logrus.FieldLogger, projectName string, config Config, api APIDefinition) (*Server, error) {
44+
var healthHandler router.APIHandler
45+
if checker, ok := api.(HealthChecker); ok {
46+
healthHandler = checker.Healthy
47+
}
48+
3749
r := router.New(
3850
log,
39-
router.OptHealthCheck(config.HealthPath, nil),
51+
router.OptHealthCheck(config.HealthPath, healthHandler),
4052
router.OptTracingMiddleware(log, projectName),
4153
)
4254

server/server_test.go

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
package server
22

33
import (
4-
"github.com/netlify/netlify-commons/router"
5-
"github.com/sirupsen/logrus"
6-
"github.com/stretchr/testify/assert"
7-
"github.com/stretchr/testify/require"
84
"net/http"
95
"net/http/httptest"
106
"os"
117
"strings"
128
"testing"
9+
10+
"github.com/netlify/netlify-commons/router"
11+
"github.com/sirupsen/logrus"
12+
"github.com/stretchr/testify/assert"
13+
"github.com/stretchr/testify/require"
1314
)
1415

1516
func init() {
@@ -42,6 +43,36 @@ func TestServerHealth(t *testing.T) {
4243
assert.Equal(t, http.StatusOK, rsp.StatusCode)
4344
}
4445

46+
type testAPICustomHealth struct{}
47+
48+
func (a *testAPICustomHealth) Start(r router.Router) error {
49+
r.Get("/", func(w http.ResponseWriter, r *http.Request) *router.HTTPError {
50+
return nil
51+
})
52+
return nil
53+
}
54+
55+
func (a *testAPICustomHealth) Stop() {}
56+
57+
func (a *testAPICustomHealth) Healthy(w http.ResponseWriter, r *http.Request) *router.HTTPError {
58+
return router.InternalServerError("healthcheck failed")
59+
}
60+
61+
func TestServerCustomHealth(t *testing.T) {
62+
apiDef := new(testAPICustomHealth)
63+
64+
cfg := testConfig()
65+
svr, err := New(tl(t), "testing", cfg, apiDef)
66+
require.NoError(t, err)
67+
68+
testSvr := httptest.NewServer(svr.svr.Handler)
69+
defer testSvr.Close()
70+
71+
rsp, err := http.Get(testSvr.URL + cfg.HealthPath)
72+
require.NoError(t, err)
73+
assert.Equal(t, http.StatusInternalServerError, rsp.StatusCode)
74+
}
75+
4576
func tl(t *testing.T) *logrus.Entry {
4677
return logrus.WithField("test", t.Name())
4778
}

0 commit comments

Comments
 (0)