forked from rs/rest-layer
/
main.go
321 lines (296 loc) · 9.24 KB
/
main.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
// +build go1.7
package main
import (
"context"
"errors"
"fmt"
"log"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"github.com/justinas/alice"
"github.com/rs/rest-layer-mem"
"github.com/rs/rest-layer/graphql"
"github.com/rs/rest-layer/resource"
"github.com/rs/rest-layer/rest"
"github.com/rs/rest-layer/schema"
"github.com/rs/xaccess"
"github.com/rs/xlog"
)
var (
user = schema.Schema{
Description: "Defines user information",
Fields: schema.Fields{
"id": {
Required: true,
ReadOnly: true,
Filterable: true,
Sortable: true,
Validator: &schema.String{
Regexp: "^[0-9a-z_-]{2,150}$",
},
},
"created": schema.CreatedField,
"updated": schema.UpdatedField,
"name": {},
"admin": {
Filterable: true,
Validator: &schema.Bool{},
},
"ip": {Validator: &schema.IP{StoreBinary: true}},
"password": schema.PasswordField,
},
}
postFollower = schema.Schema{
Description: "Link a post to its followers",
Fields: schema.Fields{
"id": schema.IDField,
"post": {
Validator: &schema.Reference{Path: "posts"},
},
"user": {
Filterable: true,
Sortable: true,
Validator: &schema.Reference{Path: "users"},
},
},
}
post = schema.Schema{
Description: "Defines a blog post",
Fields: schema.Fields{
"id": schema.IDField,
"created": schema.CreatedField,
"updated": schema.UpdatedField,
"user": {
Validator: &schema.Reference{Path: "users"},
},
"thumbnail_url": {
Description: "Resizable thumbnail URL for a post. Use width and height parameters to get a specific size.",
Params: schema.Params{
"width": {
Description: "Change the width of the thumbnail to the value in pixels",
Validator: schema.Integer{
Boundaries: &schema.Boundaries{Max: 1000},
},
},
"height": {
Description: "Change the height of the thumbnail to the value in pixels",
Validator: schema.Integer{
Boundaries: &schema.Boundaries{Max: 1000},
},
},
},
// Appends a "w" and/or "h" query string parameter(s) to the value (URL) if width or height params passed
Handler: func(ctx context.Context, value interface{}, params map[string]interface{}) (interface{}, error) {
str, ok := value.(string)
if !ok {
return nil, errors.New("not a string")
}
sep := "?"
if strings.IndexByte(str, '?') > 0 {
sep = "&"
}
if width, found := params["width"]; found {
str = fmt.Sprintf("%s%sw=%d", str, sep, width)
sep = "&"
}
if height, found := params["height"]; found {
str = fmt.Sprintf("%s%sy=%d", str, sep, height)
}
return str, nil
},
},
"meta": {
Schema: &schema.Schema{
Fields: schema.Fields{
"title": {},
"body": {},
},
},
},
},
}
)
func main() {
index := resource.NewIndex()
users := index.Bind("users", user, mem.NewHandler(), resource.Conf{
AllowedModes: resource.ReadWrite,
})
users.Alias("admin", url.Values{"filter": []string{`{"admin": true}`}})
posts := index.Bind("posts", post, mem.NewHandler(), resource.Conf{
AllowedModes: resource.ReadWrite,
})
posts.Bind("followers", "post", postFollower, mem.NewHandler(), resource.Conf{
AllowedModes: resource.ReadWrite,
})
// Create API HTTP handler for the resource graph
api, err := rest.NewHandler(index)
if err != nil {
log.Fatalf("Invalid API configuration: %s", err)
}
// Setup logger
c := alice.New()
c = c.Append(xlog.NewHandler(xlog.Config{}))
c = c.Append(xaccess.NewHandler())
c = c.Append(xlog.RequestHandler("req"))
c = c.Append(xlog.RemoteAddrHandler("ip"))
c = c.Append(xlog.UserAgentHandler("ua"))
c = c.Append(xlog.RefererHandler("ref"))
c = c.Append(xlog.RequestIDHandler("req_id", "Request-Id"))
resource.LoggerLevel = resource.LogLevelDebug
resource.Logger = func(ctx context.Context, level resource.LogLevel, msg string, fields map[string]interface{}) {
xlog.FromContext(ctx).OutputF(xlog.Level(level), 2, msg, fields)
}
// Bind the API under /api/ path
http.Handle("/api/", http.StripPrefix("/api/", c.Then(api)))
// Create and bind the graphql endpoint
graphql, err := graphql.NewHandler(index)
if err != nil {
log.Fatal(err)
}
http.Handle("/graphql", c.Then(graphql))
http.HandleFunc("/graphiql", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`
<!DOCTYPE html>
<html>
<head>
<style>
html, body {height: 100%; margin: 0; overflow: hidden; width: 100%;}
</style>
<link href="//cdn.jsdelivr.net/graphiql/0.4.9/graphiql.css" rel="stylesheet" />
<script src="//cdn.jsdelivr.net/fetch/0.9.0/fetch.min.js"></script>
<script src="//cdn.jsdelivr.net/react/0.14.7/react.min.js"></script>
<script src="//cdn.jsdelivr.net/react/0.14.7/react-dom.min.js"></script>
<script src="//cdn.jsdelivr.net/graphiql/0.4.9/graphiql.min.js"></script>
</head>
<body>
<script>
// Collect the URL parameters
var parameters = {};
window.location.search.substr(1).split('&').forEach(function (entry) {
var eq = entry.indexOf('=');
if (eq >= 0) {
parameters[decodeURIComponent(entry.slice(0, eq))] =
decodeURIComponent(entry.slice(eq + 1));
}
});
// Produce a Location query string from a parameter object.
function locationQuery(params) {
return '/graphql?' + Object.keys(params).map(function (key) {
return encodeURIComponent(key) + '=' +
encodeURIComponent(params[key]);
}).join('&');
}
// Derive a fetch URL from the current URL, sans the GraphQL parameters.
var graphqlParamNames = {
query: true,
variables: true,
operationName: true
};
var otherParams = {};
for (var k in parameters) {
if (parameters.hasOwnProperty(k) && graphqlParamNames[k] !== true) {
otherParams[k] = parameters[k];
}
}
var fetchURL = locationQuery(otherParams);
// Defines a GraphQL fetcher using the fetch API.
function graphQLFetcher(graphQLParams) {
return fetch(fetchURL, {
method: 'post',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(graphQLParams),
credentials: 'include',
}).then(function (response) {
return response.text();
}).then(function (responseBody) {
try {
return JSON.parse(responseBody);
} catch (error) {
return responseBody;
}
});
}
// When the query and variables string is edited, update the URL bar so
// that it can be easily shared.
function onEditQuery(newQuery) {
parameters.query = newQuery;
updateURL();
}
function onEditVariables(newVariables) {
parameters.variables = newVariables;
updateURL();
}
function updateURL() {
history.replaceState(null, null, locationQuery(parameters));
}
// Render <GraphiQL /> into the body.
React.render(
React.createElement(GraphiQL, {
fetcher: graphQLFetcher,
onEditQuery: onEditQuery,
onEditVariables: onEditVariables,
defaultQuery: "{\
postsList{\
i: id,\
m: meta{\
t: title,\
b: body},\
thumb_small_url: thumbnail_url(height:80)\
}\
}",
}),
document.body
);
</script>
</body>
</html>`))
})
// Inject some fixtures
fixtures := [][]string{
{"PUT", "/users/johndoe", `{"name": "John Doe", "ip": "1.2.3.4", "password": "secret", "admin": true}`},
{"PUT", "/users/fan1", `{"name": "Fan 1", "ip": "1.2.3.4", "password": "secret"}}`},
{"PUT", "/users/fan2", `{"name": "Fan 2", "ip": "1.2.3.4", "password": "secret"}}`},
{"PUT", "/users/fan3", `{"name": "Fan 3", "ip": "1.2.3.4", "password": "secret"}}`},
{"PUT", "/users/fan4", `{"name": "Fan 4", "ip": "1.2.3.4", "password": "secret"}}`},
{"PUT", "/posts/ar5qrgukj5l7a6eq2ps0",
`{
"user": "johndoe",
"thumbnail_url": "http://dom.com/image.png",
"meta": {
"title": "First Post",
"body": "This is my first post"
}
}`},
{"POST", "/posts/ar5qrgukj5l7a6eq2ps0/followers", `{"user": "fan1"}`},
{"POST", "/posts/ar5qrgukj5l7a6eq2ps0/followers", `{"user": "fan2"}`},
{"POST", "/posts/ar5qrgukj5l7a6eq2ps0/followers", `{"user": "fan3"}`},
}
for _, fixture := range fixtures {
req, err := http.NewRequest(fixture[0], fixture[1], strings.NewReader(fixture[2]))
if err != nil {
log.Fatal(err)
}
w := httptest.NewRecorder()
api.ServeHTTP(w, req)
if w.Code >= 400 {
log.Fatalf("Error returned for `%s %s`: %v", fixture[0], fixture[1], w)
}
}
// Serve it
log.Print("Serving API on http://localhost:8080")
log.Print("Visit http://localhost:8080/graphiql for a GraphiQL UI")
log.Println("Play with (httpie):\n",
"- http :8080/graphql query=='{postsList{id,thumb_s_url:thumbnail_url(height:80)}}'\n",
"- http :8080/graphql query=='{postsList{i:id,m:meta{t:title, b:body},thumb_small_url:thumbnail_url(height:80)}}'\n",
"- http :8080/graphql query=='{postsList{id,meta{title},user{id,name}}}'\n",
"- http :8080/graphql query=='{posts(id:\"ar5qrgukj5l7a6eq2ps0\"){followers{post{id,meta{title}},user{id,name}}}}'\n",
"- http :8080/graphql query=='{posts(id:\"ar5qrgukj5l7a6eq2ps0\"){id,meta{title},followers(limit:2){user{id,name}}}}'")
if err := http.ListenAndServe(":8080", nil); err != nil {
log.Fatal(err)
}
}