From 12bc12fa7efcca9b438320e2d2be4ee622cc938d Mon Sep 17 00:00:00 2001 From: Maxim Baz Date: Mon, 30 Jul 2018 22:32:42 +0200 Subject: [PATCH] Add /health endpoint --- http/http.go | 9 +++++++++ http/http_test.go | 1 + 2 files changed, 10 insertions(+) diff --git a/http/http.go b/http/http.go index cac1010..5639239 100644 --- a/http/http.go +++ b/http/http.go @@ -159,6 +159,12 @@ func (s *Server) JSONHandler(w http.ResponseWriter, r *http.Request) *appError { return nil } +func (s *Server) HealthHandler(w http.ResponseWriter, r *http.Request) *appError { + w.Header().Set("Content-Type", jsonMediaType) + w.Write([]byte(`{"status":"OK"}`)) + return nil +} + func (s *Server) PortHandler(w http.ResponseWriter, r *http.Request) *appError { response, err := s.newPortResponse(r) if err != nil { @@ -247,6 +253,9 @@ func (fn appHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (s *Server) Handler() http.Handler { r := NewRouter() + // Health + r.Route("GET", "/health", s.HealthHandler) + // JSON r.Route("GET", "/", s.JSONHandler).Header("Accept", jsonMediaType) r.Route("GET", "/json", s.JSONHandler) diff --git a/http/http_test.go b/http/http_test.go index 206d9ff..d55fba7 100644 --- a/http/http_test.go +++ b/http/http_test.go @@ -131,6 +131,7 @@ func TestJSONHandlers(t *testing.T) { {s.URL + "/port/65356", `{"error":"Invalid port: 65356"}`, 400}, {s.URL + "/port/31337", `{"ip":"127.0.0.1","port":31337,"reachable":true}`, 200}, {s.URL + "/foo", `{"error":"404 page not found"}`, 404}, + {s.URL + "/health", `{"status":"OK"}`, 200}, } for _, tt := range tests {