From bac244e9d1cc2f3af4a63c662f38be60a1bcf7fd Mon Sep 17 00:00:00 2001 From: Matthew Lemon Date: Mon, 11 Mar 2024 19:39:00 +0000 Subject: Adds a writeJSON helper --- cmd/api/healthcheck.go | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) (limited to 'cmd/api/healthcheck.go') diff --git a/cmd/api/healthcheck.go b/cmd/api/healthcheck.go index c4f52f2..c046995 100644 --- a/cmd/api/healthcheck.go +++ b/cmd/api/healthcheck.go @@ -1,13 +1,23 @@ package main import ( - "fmt" + "encoding/json" "net/http" ) func (app *application) healthcheckHandler(w http.ResponseWriter, r *http.Request) { - js := `{"status": "available", "environment": %q, "version": %q}` - js = fmt.Sprintf(js, app.config.env, version) + data := map[string]string{ + "status": "available", + "environment": app.config.env, + "version": version, + } + js, err := json.Marshal(data) + if err != nil { + app.logger.Debug("server error", "err", err) + http.Error(w, "The server encountered a problem and could not process your request", http.StatusInternalServerError) + return + } + js = append(js, '\n') w.Header().Set("Content-Type", "application/json") w.Write([]byte(js)) } -- cgit v1.2.3