/
handlers.go
83 lines (74 loc) · 2.32 KB
/
handlers.go
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
package main
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"github.com/julienschmidt/httprouter"
)
func Index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
fmt.Fprint(w, "Welcome!\n")
}
// Handler for the books Create action
// POST /books
func BookCreate(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
book := &Book{}
if err := populateModelFromHandler(w, r, params, book); err != nil {
writeErrorResponse(w, http.StatusUnprocessableEntity, "Unprocessible Entity")
return
}
bookstore[book.ISDN] = book
writeOKResponse(w, book)
}
// Handler for the books index action
// GET /books
func BookIndex(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
books := []*Book{}
for _, book := range bookstore {
books = append(books, book)
}
writeOKResponse(w, books)
}
// Handler for the books Show action
// GET /books/:isdn
func BookShow(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
isdn := params.ByName("isdn")
book, ok := bookstore[isdn]
if !ok {
// No book with the isdn in the url has been found
writeErrorResponse(w, http.StatusNotFound, "Record Not Found")
return
}
writeOKResponse(w, book)
}
// Writes the response as a standard JSON response with StatusOK
func writeOKResponse(w http.ResponseWriter, m interface{}) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(&JsonResponse{Data: m}); err != nil {
writeErrorResponse(w, http.StatusInternalServerError, "Internal Server Error")
}
}
// Writes the error response as a Standard API JSON response with a response code
func writeErrorResponse(w http.ResponseWriter, errorCode int, errorMsg string) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(errorCode)
json.
NewEncoder(w).
Encode(&JsonErrorResponse{Error: &ApiError{Status: errorCode, Title: errorMsg}})
}
//Populates a model from the params in the Handler
func populateModelFromHandler(w http.ResponseWriter, r *http.Request, params httprouter.Params, model interface{}) error {
body, err := ioutil.ReadAll(io.LimitReader(r.Body, 1048576))
if err != nil {
return err
}
if err := r.Body.Close(); err != nil {
return err
}
if err := json.Unmarshal(body, model); err != nil {
return err
}
return nil
}