From 712d0e64207c635fbd85e42e3261ee807deaf400 Mon Sep 17 00:00:00 2001 From: Felix Auringer <48409110+felixauringer@users.noreply.github.com> Date: Tue, 27 Apr 2021 08:47:11 +0200 Subject: [PATCH] Cleanup code This changes variable names that were abbreviations, uses more constants from the net/http package and improves the json decoding / encoding. --- api/api.go | 9 ++++++--- api/health.go | 6 +++--- api/health_test.go | 14 +++++++------- api/response_messages.go | 2 +- 4 files changed, 17 insertions(+), 14 deletions(-) diff --git a/api/api.go b/api/api.go index bec7d5d..8153a6c 100644 --- a/api/api.go +++ b/api/api.go @@ -1,8 +1,11 @@ package api -import "github.com/gorilla/mux" +import ( + "github.com/gorilla/mux" + "net/http" +) -// NewRouter returns an HTTP handler (net.Handler) which can then +// NewRouter returns an HTTP handler (http.Handler) which can then // be used by the net/http package to serve the api of our API. // We use gorilla/mux because it is more convenient than net/http, // e.g. when extracting path parameters. @@ -11,7 +14,7 @@ func NewRouter() *mux.Router { // this can later be restricted to a specific host with // `router.Host(...)` and to HTTPS with `router.Schemes("https")` v1 := router.PathPrefix("/api/v1").Subrouter() - v1.HandleFunc("/health", Health).Methods("GET") + v1.HandleFunc("/health", Health).Methods(http.MethodGet) return v1 } diff --git a/api/health.go b/api/health.go index d0743b7..0a403df 100644 --- a/api/health.go +++ b/api/health.go @@ -6,14 +6,14 @@ import ( "net/http" ) -// Health sends the response that the API works. If it -// it is able to do so, it is obviously correct. +// Health tries to respond that the server is alive. +// If it is not, the response won't reach the client. func Health(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) w.Header().Set("Content-Type", "application/json") response, err := json.Marshal(Message{"I'm alive!"}) if err != nil { - log.Printf("Error formatting the health route: %v\n", err) + log.Printf("JSON marshal error in health route: %v\n", err) http.Error(w, err.Error(), http.StatusInternalServerError) } if _, err := w.Write(response); err != nil { diff --git a/api/health_test.go b/api/health_test.go index f378ce8..62d22ed 100644 --- a/api/health_test.go +++ b/api/health_test.go @@ -9,15 +9,15 @@ import ( ) func TestHealthRoute(t *testing.T) { - req, err := http.NewRequest("GET", "/health", nil) + request, err := http.NewRequest(http.MethodGet, "/health", nil) if err != nil { t.Fatal(err) } - rec := httptest.NewRecorder() - http.HandlerFunc(Health).ServeHTTP(rec, req) - res := &Message{} - _ = json.NewDecoder(rec.Body).Decode(res) + recorder := httptest.NewRecorder() + http.HandlerFunc(Health).ServeHTTP(recorder, request) + result := Message{} + _ = json.Unmarshal(recorder.Body.Bytes(), &result) - assert.Equal(t, http.StatusOK, rec.Code) - assert.Equal(t, "I'm alive!", res.Msg) + assert.Equal(t, http.StatusOK, recorder.Code) + assert.Equal(t, "I'm alive!", result.Message) } diff --git a/api/response_messages.go b/api/response_messages.go index dfb13d4..a3de614 100644 --- a/api/response_messages.go +++ b/api/response_messages.go @@ -1,5 +1,5 @@ package api type Message struct { - Msg string `json:"msg"` + Message string `json:"msg"` }