From 1d7beb7de40d92fb5b5fa9a8ca72d2846863bbd8 Mon Sep 17 00:00:00 2001 From: lbenedar Date: Tue, 10 Mar 2026 15:39:45 +0300 Subject: [PATCH] ch3.4 --- cmd/api/healthcheck.go | 12 +++++++----- cmd/api/helpers.go | 6 ++++-- cmd/api/movies.go | 2 +- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/cmd/api/healthcheck.go b/cmd/api/healthcheck.go index e6876be..0f0a316 100644 --- a/cmd/api/healthcheck.go +++ b/cmd/api/healthcheck.go @@ -5,12 +5,14 @@ import ( ) func (app *application) healthcheckHandler(w http.ResponseWriter, r *http.Request) { - data := map[string]string{ - "status": "available", - "environment": app.config.env, - "version": version, + env := envelope{ + "status": "available", + "system_info": map[string]string{ + "environment": app.config.env, + "version": version, + }, } - err := app.writeJSON(w, http.StatusOK, data, nil) + err := app.writeJSON(w, http.StatusOK, env, nil) if err != nil { app.logger.Print(err) http.Error(w, "The server encountered a problem and could not process your request", http.StatusInternalServerError) diff --git a/cmd/api/helpers.go b/cmd/api/helpers.go index 072e7fa..f8f7829 100644 --- a/cmd/api/helpers.go +++ b/cmd/api/helpers.go @@ -9,6 +9,8 @@ import ( "github.com/julienschmidt/httprouter" ) +type envelope map[string]any + func (app *application) readIDParam(r *http.Request) (int64, error) { params := httprouter.ParamsFromContext(r.Context()) @@ -19,8 +21,8 @@ func (app *application) readIDParam(r *http.Request) (int64, error) { return id, nil } -func (app *application) writeJSON(w http.ResponseWriter, status int, data any, headers http.Header) error { - js, err := json.Marshal(data) +func (app *application) writeJSON(w http.ResponseWriter, status int, data envelope, headers http.Header) error { + js, err := json.MarshalIndent(data, "", "\t") if err != nil { return err } diff --git a/cmd/api/movies.go b/cmd/api/movies.go index e1ffc6f..585e96d 100644 --- a/cmd/api/movies.go +++ b/cmd/api/movies.go @@ -28,7 +28,7 @@ func (app *application) showMovieHandler(w http.ResponseWriter, r *http.Request) Version: 1, } - err = app.writeJSON(w, http.StatusOK, movie, nil) + err = app.writeJSON(w, http.StatusOK, envelope{"movie": movie}, nil) if err != nil { app.logger.Print(err) http.Error(w, "The server encountered a problem and could not process your request", http.StatusInternalServerError)