Creating writeJSON method inside helpers.go file. Using writeJSON helper inside healthcheckHandler.

This commit is contained in:
Maxime Delporte
2025-10-11 21:15:50 +02:00
parent 0db52ad9de
commit cb3691ce04
2 changed files with 38 additions and 19 deletions

View File

@@ -1,7 +1,6 @@
package main
import (
"encoding/json"
"net/http"
)
@@ -20,25 +19,9 @@ func (app *application) healthcheckHandler(w http.ResponseWriter, r *http.Reques
"version": version,
}
/*
Pass the map to the json.Marshal() function. This returns a []byte slice
containing the encoded JSON. If there was an error, we log it and send the client
a generic error message.
*/
js, err := json.Marshal(data)
err := app.writeJSON(w, http.StatusOK, data, nil)
if err != nil {
app.logger.Error(err.Error())
http.Error(w, "The server encountered a problem and could not process your request.", http.StatusInternalServerError)
http.Error(w, "The server encountered a problem and could not process your request", http.StatusInternalServerError)
}
// Append a newline to the JSON. This is just a small nicety to make it easier to
// view in terminal applications.
js = append(js, '\n')
// At this point, we know that encoding the data worked without any problem, so we
// can safely set any necessary HTTP headers for a successful response.
w.Header().Set("Content-Type", "application/json")
// Use w.Write() to send the []byte slice containing the JSON as the response body.
w.Write(js)
}