From 8b05856fd9f24f4445737252922a7dda3d1f9eff Mon Sep 17 00:00:00 2001 From: Martin Gallauner Date: Thu, 9 May 2024 08:37:52 +0200 Subject: [PATCH] adds new healthz endpoint --- http/bookclub.http | 6 +++++- internal/handler_readiness.go | 9 +++++++++ internal/server.go | 7 ++++--- internal/user_service_test.go | 4 ++-- 4 files changed, 20 insertions(+), 6 deletions(-) create mode 100644 internal/handler_readiness.go diff --git a/http/bookclub.http b/http/bookclub.http index aa2fe03..590b4ac 100644 --- a/http/bookclub.http +++ b/http/bookclub.http @@ -73,4 +73,8 @@ GET http://localhost:8080/auth/google ### # group: auth # @name logout -GET http://localhost:8080/auth/google/logout \ No newline at end of file +GET http://localhost:8080/auth/google/logout + +### +# @name healthz +GET http://localhost:8080/api/healthz \ No newline at end of file diff --git a/internal/handler_readiness.go b/internal/handler_readiness.go new file mode 100644 index 0000000..f8444d5 --- /dev/null +++ b/internal/handler_readiness.go @@ -0,0 +1,9 @@ +package internal + +import "net/http" + +func handlerReadiness(w http.ResponseWriter, r *http.Request) { + w.Header().Add("Content-Type", "text/plain; charset=utf-8") + w.WriteHeader(http.StatusOK) + w.Write([]byte(http.StatusText(http.StatusOK))) +} diff --git a/internal/server.go b/internal/server.go index 8f7d1d5..0400688 100644 --- a/internal/server.go +++ b/internal/server.go @@ -45,10 +45,11 @@ func NewBookclubServer(client Client, repository BookRepository, userRepository router.Handle("/api/users", http.HandlerFunc(s.handlerCreateUser)) router.Handle("/api/links/{id}", http.HandlerFunc(s.handlerGetLinks)) router.Handle("/api/links", http.HandlerFunc(s.handlerCreateLink)) - router.Handle("/auth/{provider}/callback", http.HandlerFunc(s.handlerCallback)) - router.Handle("/auth/{provider}/logout", http.HandlerFunc(s.handlerLogout)) - router.Handle("/auth/{provider}", http.HandlerFunc(s.handlerLogin)) + router.Handle("/api/auth/{provider}/callback", http.HandlerFunc(s.handlerCallback)) + router.Handle("/api/auth/{provider}/logout", http.HandlerFunc(s.handlerLogout)) + router.Handle("/api/auth/{provider}", http.HandlerFunc(s.handlerLogin)) router.Handle("/swagger/*", httpSwagger.Handler(httpSwagger.URL("http://localhost:8080/swagger/doc.json"))) + router.Handle("/api/healthz", http.HandlerFunc(handlerReadiness)) s.Handler = router return s diff --git a/internal/user_service_test.go b/internal/user_service_test.go index 1def3bd..1b73e1b 100644 --- a/internal/user_service_test.go +++ b/internal/user_service_test.go @@ -181,7 +181,7 @@ func TestLogin(t *testing.T) { //when - request, _ := http.NewRequest(http.MethodPost, "/auth/login", nil) + request, _ := http.NewRequest(http.MethodPost, "/api/auth/login", nil) response := httptest.NewRecorder() s.ServeHTTP(response, request) @@ -213,7 +213,7 @@ func TestLoginOfNewUser(t *testing.T) { } //when - request, _ := http.NewRequest(http.MethodPost, "/auth/login", nil) + request, _ := http.NewRequest(http.MethodPost, "/api/auth/login", nil) response := httptest.NewRecorder() s.ServeHTTP(response, request)