From f8e864efdd16e95d6bc92f66065cc4249896e158 Mon Sep 17 00:00:00 2001 From: Felix Auringer <48409110+felixauringer@users.noreply.github.com> Date: Mon, 26 Apr 2021 12:29:53 +0200 Subject: [PATCH] Add health route --- api/health.go | 23 +++++++++++++++++++++++ api/response_messages.go | 5 +++++ 2 files changed, 28 insertions(+) create mode 100644 api/health.go create mode 100644 api/response_messages.go diff --git a/api/health.go b/api/health.go new file mode 100644 index 0000000..d0743b7 --- /dev/null +++ b/api/health.go @@ -0,0 +1,23 @@ +package api + +import ( + "encoding/json" + "log" + "net/http" +) + +// Health sends the response that the API works. If it +// it is able to do so, it is obviously correct. +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) + http.Error(w, err.Error(), http.StatusInternalServerError) + } + if _, err := w.Write(response); err != nil { + log.Printf("Error handling the health route: %v\n", err) + http.Error(w, err.Error(), http.StatusInternalServerError) + } +} diff --git a/api/response_messages.go b/api/response_messages.go new file mode 100644 index 0000000..dfb13d4 --- /dev/null +++ b/api/response_messages.go @@ -0,0 +1,5 @@ +package api + +type Message struct { + Msg string `json:"msg"` +}