From 731652dcb3a897b5fb57cbc2ad537c27ba010fe0 Mon Sep 17 00:00:00 2001 From: Rob Watson Date: Wed, 13 Jul 2022 21:36:38 +0200 Subject: [PATCH] handler: Add health check --- gateway/handler/handler.go | 7 ++++++- gateway/handler/handler_test.go | 7 +++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/gateway/handler/handler.go b/gateway/handler/handler.go index dd10f6a..f882ab1 100644 --- a/gateway/handler/handler.go +++ b/gateway/handler/handler.go @@ -22,6 +22,12 @@ type Handler struct { func New(store Store) *Handler { return &Handler{store: store} } func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { + if r.Method == http.MethodGet && r.URL.Path == "/healthz" { + w.WriteHeader(http.StatusOK) + w.Write([]byte("OK\n")) + return + } + if r.Method != http.MethodPost { http.Error(w, "method not allowed", http.StatusMethodNotAllowed) return @@ -59,7 +65,6 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { return } - w.Header().Set("content-type", "application/json") w.WriteHeader(http.StatusOK) w.Write([]byte("OK\n")) } diff --git a/gateway/handler/handler_test.go b/gateway/handler/handler_test.go index 71fc03e..3efb821 100644 --- a/gateway/handler/handler_test.go +++ b/gateway/handler/handler_test.go @@ -32,6 +32,13 @@ func TestHandler(t *testing.T) { wantStatusCode int wantBody string }{ + { + name: "healthz", + httpMethod: http.MethodGet, + path: "/healthz", + wantStatusCode: http.StatusOK, + wantBody: "OK\n", + }, { name: "method not allowed", httpMethod: http.MethodGet,