Files
poseidon/api/health.go
Felix Auringer 712d0e6420 Cleanup code
This changes variable names that were abbreviations, uses more constants
from the net/http package and improves the json decoding / encoding.
2021-04-27 08:57:31 +02:00

24 lines
672 B
Go

package api
import (
"encoding/json"
"log"
"net/http"
)
// 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("JSON marshal error in 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)
}
}