Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions internal/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package router

import (
"bytes"
"fmt"
"io"
"net/http"

Expand All @@ -13,6 +14,7 @@ const (
connectPath = "/connect"
disconnectPath = "/disconnect"
heartbeatPath = "/heartbeat"
healthPath = "/health"
)

type APIHandlers interface {
Expand Down Expand Up @@ -68,12 +70,18 @@ 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()

router.Handle(apiPath+connectPath, LoggingMiddleware(config.Log, http.HandlerFunc(config.APIHandlers.Connect)))
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
}
18 changes: 18 additions & 0 deletions internal/router/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}