From baaea9dc4187f6a061a1938c3633a5b465e6fc6c Mon Sep 17 00:00:00 2001 From: Nickolay Kondratenko Date: Sun, 25 May 2025 23:38:18 +0300 Subject: [PATCH 1/2] add health probe endpoint --- internal/router/router.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/internal/router/router.go b/internal/router/router.go index 31dd492..3a3160c 100644 --- a/internal/router/router.go +++ b/internal/router/router.go @@ -2,6 +2,7 @@ package router import ( "bytes" + "fmt" "io" "net/http" @@ -13,6 +14,7 @@ const ( connectPath = "/connect" disconnectPath = "/disconnect" heartbeatPath = "/heartbeat" + healthPath = "/health" ) type APIHandlers interface { @@ -68,6 +70,10 @@ func LoggingMiddleware(logger *zap.Logger, next http.Handler) http.Handler { }) } +func healthCheckHandler(w http.ResponseWriter, r *http.Request) { + fmt.Fprint(w, "OK") +} + func New(config Config) *http.ServeMux { router := http.NewServeMux() @@ -75,5 +81,7 @@ func New(config Config) *http.ServeMux { router.Handle(apiPath+disconnectPath, LoggingMiddleware(config.Log, http.HandlerFunc(config.APIHandlers.Disconnect))) router.Handle(apiPath+heartbeatPath, LoggingMiddleware(config.Log, http.HandlerFunc(config.APIHandlers.Heartbeat))) + router.Handle(healthPath, http.HandlerFunc(healthCheckHandler)) + return router } From bfa478ce43ac661488d0fea75c496cc15a561fbd Mon Sep 17 00:00:00 2001 From: Nickolay Kondratenko Date: Sun, 25 May 2025 23:47:28 +0300 Subject: [PATCH 2/2] add the tests --- internal/router/router_test.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/internal/router/router_test.go b/internal/router/router_test.go index e095215..a17a9d5 100644 --- a/internal/router/router_test.go +++ b/internal/router/router_test.go @@ -41,3 +41,21 @@ func TestRouter(t *testing.T) { assert.True(t, mock.called[tt.expected], "Handler not called for "+tt.path) } } + +func TestHealth(t *testing.T) { + mock := newMockHandler() + log := zap.NewNop() + + r := New(Config{ + APIHandlers: mock, + Log: log, + }) + + req := httptest.NewRequest(http.MethodGet, "/health", bytes.NewBufferString("")) + rec := httptest.NewRecorder() + + r.ServeHTTP(rec, req) + + assert.Equal(t, http.StatusOK, rec.Code) + assert.Equal(t, "OK", rec.Body.String()) +}