customize json response

This commit is contained in:
ergz 2023-06-21 22:12:38 -07:00
parent 40b9cf4610
commit 08bd769fdf
5 changed files with 77 additions and 15 deletions

View File

@ -1,30 +1,23 @@
package main
import (
"encoding/json"
"net/http"
)
func (app *application) healthCheckHandler(w http.ResponseWriter, r *http.Request) {
// create a mapping that will be converted to json
data := map[string]string{
"status": "available",
"environment": app.config.env,
"version": version,
envelopedHealth := envelope{
"status": "available",
"system_info": map[string]string{
"environment": app.config.env,
"version": version,
},
}
js, err := json.Marshal(data)
err := app.writeJSON(w, http.StatusOK, envelopedHealth, nil)
if err != nil {
app.logger.Print(err)
http.Error(w, "the server encountered an error and could process your request", http.StatusInternalServerError)
return
}
js = append(js, '\n')
w.Header().Set("Content-Type", "application/json")
w.Write(js)
}

View File

@ -1,6 +1,7 @@
package main
import (
"encoding/json"
"errors"
"net/http"
"strconv"
@ -8,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())
@ -18,3 +21,22 @@ 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)
if err != nil {
return err
}
js = append(js, '\n')
for key, value := range headers {
w.Header()[key] = value
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
w.Write(js)
return nil
}

View File

@ -3,6 +3,9 @@ package main
import (
"fmt"
"net/http"
"time"
"greenlight.ergz/internal/data"
)
func (app *application) createMovieHandler(w http.ResponseWriter, r *http.Request) {
@ -16,5 +19,20 @@ func (app *application) showMovieHandler(w http.ResponseWriter, r *http.Request)
return
}
fmt.Fprintf(w, "show the details of movie %d\n", id)
movie := data.Movie{
ID: id,
CreatedAt: time.Now(),
Title: "Casablanca",
Runtime: 102,
Genres: []string{"drama", "romance", "war"},
Version: 1,
}
envelopedMovie := envelope{"movie": movie}
err = app.writeJSON(w, http.StatusOK, envelopedMovie, nil)
if err != nil {
app.logger.Print(err)
http.Error(w, "the server encountered an error and could process your request", http.StatusInternalServerError)
}
}

13
internal/data/movies.go Normal file
View File

@ -0,0 +1,13 @@
package data
import "time"
type Movie struct {
ID int64 `json:"id"`
CreatedAt time.Time `json:"-"`
Title string `json:"title"`
Year int32 `json:"year,omitempty"`
Runtime Runtime `json:"runtime,omitempty,string"`
Genres []string `json:"gentres,omitempty"`
Version int32 `json:"version"`
}

16
internal/data/runtime.go Normal file
View File

@ -0,0 +1,16 @@
package data
import (
"fmt"
"strconv"
)
type Runtime int32
func (r Runtime) MarshalJSON() ([]byte, error) {
jsonValue := fmt.Sprintf("%d mins", r)
quotedJSONValue := strconv.Quote(jsonValue)
return []byte(quotedJSONValue), nil
}