Files
greenlight/cmd/api/healthcheck.go
2025-10-11 11:22:59 +02:00

35 lines
1.2 KiB
Go

package main
import (
"fmt"
"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 fixed-format JSON response from a string. Notice how we're using a raw
string literal (enclosed with backticks) so that we can include double-quote
characters in the JSON without needing to espace them? We also use the %q verb to
wrap the interpolated values in double-quotes.
*/
js := `{"status": "available", "environment": %q, "version": %q}`
js = fmt.Sprintf(js, app.config.env, version)
/*
Set the "Content-Type: application/json" header on the response. If you forget to
this, Go will default to sending a "Content-Type: text/plain; charset=utf-8"
header instead.
*/
w.Header().Set("Content-Type", "application/json")
// Write the JSON as the HTTP response body.
w.Write([]byte(js))
}