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