This commit is contained in:
lbenedar
2026-03-10 15:39:45 +03:00
parent 281bc33897
commit 1d7beb7de4
3 changed files with 12 additions and 8 deletions

View File

@@ -5,12 +5,14 @@ import (
) )
func (app *application) healthcheckHandler(w http.ResponseWriter, r *http.Request) { func (app *application) healthcheckHandler(w http.ResponseWriter, r *http.Request) {
data := map[string]string{ env := envelope{
"status": "available", "status": "available",
"environment": app.config.env, "system_info": map[string]string{
"version": version, "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 { if err != nil {
app.logger.Print(err) app.logger.Print(err)
http.Error(w, "The server encountered a problem and could not process your request", http.StatusInternalServerError) http.Error(w, "The server encountered a problem and could not process your request", http.StatusInternalServerError)

View File

@@ -9,6 +9,8 @@ import (
"github.com/julienschmidt/httprouter" "github.com/julienschmidt/httprouter"
) )
type envelope map[string]any
func (app *application) readIDParam(r *http.Request) (int64, error) { func (app *application) readIDParam(r *http.Request) (int64, error) {
params := httprouter.ParamsFromContext(r.Context()) params := httprouter.ParamsFromContext(r.Context())
@@ -19,8 +21,8 @@ func (app *application) readIDParam(r *http.Request) (int64, error) {
return id, nil return id, nil
} }
func (app *application) writeJSON(w http.ResponseWriter, status int, data any, headers http.Header) error { func (app *application) writeJSON(w http.ResponseWriter, status int, data envelope, headers http.Header) error {
js, err := json.Marshal(data) js, err := json.MarshalIndent(data, "", "\t")
if err != nil { if err != nil {
return err return err
} }

View File

@@ -28,7 +28,7 @@ func (app *application) showMovieHandler(w http.ResponseWriter, r *http.Request)
Version: 1, Version: 1,
} }
err = app.writeJSON(w, http.StatusOK, movie, nil) err = app.writeJSON(w, http.StatusOK, envelope{"movie": movie}, nil)
if err != nil { if err != nil {
app.logger.Print(err) app.logger.Print(err)
http.Error(w, "The server encountered a problem and could not process your request", http.StatusInternalServerError) http.Error(w, "The server encountered a problem and could not process your request", http.StatusInternalServerError)