Add health route

This commit is contained in:
Felix Auringer
2021-04-26 12:29:53 +02:00
parent 38ecc02a79
commit f8e864efdd
2 changed files with 28 additions and 0 deletions

23
api/health.go Normal file
View File

@ -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)
}
}

5
api/response_messages.go Normal file
View File

@ -0,0 +1,5 @@
package api
type Message struct {
Msg string `json:"msg"`
}