customize json response
This commit is contained in:
parent
40b9cf4610
commit
08bd769fdf
|
@ -1,30 +1,23 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (app *application) healthCheckHandler(w http.ResponseWriter, r *http.Request) {
|
func (app *application) healthCheckHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
// create a mapping that will be converted to json
|
envelopedHealth := envelope{
|
||||||
data := map[string]string{
|
"status": "available",
|
||||||
"status": "available",
|
"system_info": map[string]string{
|
||||||
"environment": app.config.env,
|
"environment": app.config.env,
|
||||||
"version": version,
|
"version": version,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
js, err := json.Marshal(data)
|
err := app.writeJSON(w, http.StatusOK, envelopedHealth, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
app.logger.Print(err)
|
app.logger.Print(err)
|
||||||
http.Error(w, "the server encountered an error and could process your request", http.StatusInternalServerError)
|
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)
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
@ -8,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())
|
||||||
|
|
||||||
|
@ -18,3 +21,22 @@ 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 {
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
|
@ -3,6 +3,9 @@ package main
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"greenlight.ergz/internal/data"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (app *application) createMovieHandler(w http.ResponseWriter, r *http.Request) {
|
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
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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"`
|
||||||
|
}
|
|
@ -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
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue