-
Notifications
You must be signed in to change notification settings - Fork 210
/
app.go
135 lines (97 loc) · 3.73 KB
/
app.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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"time"
"github.com/google/jsonapi"
)
func main() {
jsonapi.Instrumentation = func(r *jsonapi.Runtime, eventType jsonapi.Event, callGUID string, dur time.Duration) {
metricPrefix := r.Value("instrument").(string)
if eventType == jsonapi.UnmarshalStart {
fmt.Printf("%s: id, %s, started at %v\n", metricPrefix+".jsonapi_unmarshal_time", callGUID, time.Now())
}
if eventType == jsonapi.UnmarshalStop {
fmt.Printf("%s: id, %s, stopped at, %v , and took %v to unmarshal payload\n", metricPrefix+".jsonapi_unmarshal_time", callGUID, time.Now(), dur)
}
if eventType == jsonapi.MarshalStart {
fmt.Printf("%s: id, %s, started at %v\n", metricPrefix+".jsonapi_marshal_time", callGUID, time.Now())
}
if eventType == jsonapi.MarshalStop {
fmt.Printf("%s: id, %s, stopped at, %v , and took %v to marshal payload\n", metricPrefix+".jsonapi_marshal_time", callGUID, time.Now(), dur)
}
}
exampleHandler := &ExampleHandler{}
http.HandleFunc("/blogs", exampleHandler.ServeHTTP)
exerciseHandler()
}
func exerciseHandler() {
// list
req, _ := http.NewRequest(http.MethodGet, "/blogs", nil)
req.Header.Set(headerAccept, jsonapi.MediaType)
w := httptest.NewRecorder()
fmt.Println("============ start list ===========")
http.DefaultServeMux.ServeHTTP(w, req)
fmt.Println("============ stop list ===========")
jsonReply, _ := ioutil.ReadAll(w.Body)
fmt.Println("============ jsonapi response from list ===========")
fmt.Println(string(jsonReply))
fmt.Println("============== end raw jsonapi from list =============")
// show
req, _ = http.NewRequest(http.MethodGet, "/blogs?id=1", nil)
req.Header.Set(headerAccept, jsonapi.MediaType)
w = httptest.NewRecorder()
fmt.Println("============ start show ===========")
http.DefaultServeMux.ServeHTTP(w, req)
fmt.Println("============ stop show ===========")
jsonReply, _ = ioutil.ReadAll(w.Body)
fmt.Println("============ jsonapi response from show ===========")
fmt.Println(string(jsonReply))
fmt.Println("============== end raw jsonapi from show =============")
// create
blog := fixtureBlogCreate(1)
in := bytes.NewBuffer(nil)
jsonapi.MarshalOnePayloadEmbedded(in, blog)
req, _ = http.NewRequest(http.MethodPost, "/blogs", in)
req.Header.Set(headerAccept, jsonapi.MediaType)
w = httptest.NewRecorder()
fmt.Println("============ start create ===========")
http.DefaultServeMux.ServeHTTP(w, req)
fmt.Println("============ stop create ===========")
buf := bytes.NewBuffer(nil)
io.Copy(buf, w.Body)
fmt.Println("============ jsonapi response from create ===========")
fmt.Println(buf.String())
fmt.Println("============== end raw jsonapi response =============")
// echo
blogs := []interface{}{
fixtureBlogCreate(1),
fixtureBlogCreate(2),
fixtureBlogCreate(3),
}
in = bytes.NewBuffer(nil)
jsonapi.MarshalPayload(in, blogs)
req, _ = http.NewRequest(http.MethodPut, "/blogs", in)
req.Header.Set(headerAccept, jsonapi.MediaType)
w = httptest.NewRecorder()
fmt.Println("============ start echo ===========")
http.DefaultServeMux.ServeHTTP(w, req)
fmt.Println("============ stop echo ===========")
buf = bytes.NewBuffer(nil)
io.Copy(buf, w.Body)
fmt.Println("============ jsonapi response from create ===========")
fmt.Println(buf.String())
fmt.Println("============== end raw jsonapi response =============")
responseBlog := new(Blog)
jsonapi.UnmarshalPayload(buf, responseBlog)
out := bytes.NewBuffer(nil)
json.NewEncoder(out).Encode(responseBlog)
fmt.Println("================ Viola! Converted back our Blog struct =================")
fmt.Println(string(out.Bytes()))
fmt.Println("================ end marshal materialized Blog struct =================")
}