45 lines
1.6 KiB
Go
45 lines
1.6 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
)
|
|
|
|
/*
|
|
Tips: The important thing to point out here is that healthcheckHandler is implemented as a method
|
|
on our application struct. This is an effective and idiomatic way to make dependencies available
|
|
to our handlers without resorting to global variables or closures - any dependency that the
|
|
healthcheckHandler needs can simply be included as a field in the application struct when we
|
|
initialize it in main()
|
|
*/
|
|
func (app *application) healthcheckHandler(w http.ResponseWriter, r *http.Request) {
|
|
// Create a map which holds the information that we want to send in the response.
|
|
data := map[string]string{
|
|
"status": "available",
|
|
"environment": app.config.env,
|
|
"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)
|
|
if err != nil {
|
|
app.logger.Error(err.Error())
|
|
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)
|
|
}
|