diff options
author | Matthew Lemon <y@yulqen.org> | 2024-03-11 19:39:00 +0000 |
---|---|---|
committer | Matthew Lemon <y@yulqen.org> | 2024-03-11 19:39:00 +0000 |
commit | bac244e9d1cc2f3af4a63c662f38be60a1bcf7fd (patch) | |
tree | 8be7381f92d9bb3eb9e3a4434ea71ba8000d45e2 /cmd/api/healthcheck.go | |
parent | 02bf649c8981af4b46a18d9643386f7227f156a1 (diff) |
Adds a writeJSON helper
Diffstat (limited to '')
-rw-r--r-- | cmd/api/healthcheck.go | 16 |
1 files changed, 13 insertions, 3 deletions
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)) } |