ch3.4
This commit is contained in:
@@ -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",
|
||||||
|
"system_info": map[string]string{
|
||||||
"environment": app.config.env,
|
"environment": app.config.env,
|
||||||
"version": version,
|
"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)
|
||||||
|
|||||||
@@ -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
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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)
|
||||||
|
|||||||
Reference in New Issue
Block a user