1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
|
package main
import (
"net/http"
"strconv"
"github.com/google/jsonapi"
)
const (
headerAccept = "Accept"
headerContentType = "Content-Type"
)
// ExampleHandler is the handler we are using to demonstrate building an HTTP
// server with the jsonapi library.
type ExampleHandler struct{}
func (h *ExampleHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.Header.Get(headerAccept) != jsonapi.MediaType {
http.Error(w, "Unsupported Media Type", http.StatusUnsupportedMediaType)
}
var methodHandler http.HandlerFunc
switch r.Method {
case http.MethodPost:
methodHandler = h.createBlog
case http.MethodPut:
methodHandler = h.echoBlogs
case http.MethodGet:
if r.FormValue("id") != "" {
methodHandler = h.showBlog
} else {
methodHandler = h.listBlogs
}
default:
http.Error(w, "Not Found", http.StatusNotFound)
return
}
methodHandler(w, r)
}
func (h *ExampleHandler) createBlog(w http.ResponseWriter, r *http.Request) {
jsonapiRuntime := jsonapi.NewRuntime().Instrument("blogs.create")
blog := new(Blog)
if err := jsonapiRuntime.UnmarshalPayload(r.Body, blog); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// ...do stuff with your blog...
w.WriteHeader(http.StatusCreated)
w.Header().Set(headerContentType, jsonapi.MediaType)
if err := jsonapiRuntime.MarshalPayload(w, blog); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func (h *ExampleHandler) echoBlogs(w http.ResponseWriter, r *http.Request) {
jsonapiRuntime := jsonapi.NewRuntime().Instrument("blogs.list")
// ...fetch your blogs, filter, offset, limit, etc...
// but, for now
blogs := fixtureBlogsList()
w.WriteHeader(http.StatusOK)
w.Header().Set(headerContentType, jsonapi.MediaType)
if err := jsonapiRuntime.MarshalPayload(w, blogs); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func (h *ExampleHandler) showBlog(w http.ResponseWriter, r *http.Request) {
id := r.FormValue("id")
// ...fetch your blog...
intID, err := strconv.Atoi(id)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
jsonapiRuntime := jsonapi.NewRuntime().Instrument("blogs.show")
// but, for now
blog := fixtureBlogCreate(intID)
w.WriteHeader(http.StatusOK)
w.Header().Set(headerContentType, jsonapi.MediaType)
if err := jsonapiRuntime.MarshalPayload(w, blog); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func (h *ExampleHandler) listBlogs(w http.ResponseWriter, r *http.Request) {
jsonapiRuntime := jsonapi.NewRuntime().Instrument("blogs.list")
// ...fetch your blogs, filter, offset, limit, etc...
// but, for now
blogs := fixtureBlogsList()
w.Header().Set("Content-Type", jsonapi.MediaType)
w.WriteHeader(http.StatusOK)
if err := jsonapiRuntime.MarshalPayload(w, blogs); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
|