diff options
Diffstat (limited to 'cmd/api/healthcheck.go')
-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)) } |