-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
459 lines (425 loc) · 31.3 KB
/
index.html
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8" /><meta name="author" content="Under the hood." /><title>Ratpack</title><meta content="yes" name="apple-mobile-web-app-capable" /><meta content="black-translucent" name="apple-mobile-web-app-status-bar-style" /><meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, minimal-ui" name="viewport" /><link href="reveal.js/css/reveal.css" rel="stylesheet" /><link rel="stylesheet" href="styles/theme.css" id="theme" /><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.3.0/css/font-awesome.min.css" /><style>/* Stylesheet for CodeRay to match GitHub theme | MIT License | http://foundation.zurb.com */
/*pre.CodeRay {background-color:#f7f7f8;}*/
.CodeRay .line-numbers{border-right:1px solid #d8d8d8;padding:0 0.5em 0 .25em}
.CodeRay span.line-numbers{display:inline-block;margin-right:.5em;color:rgba(0,0,0,.3)}
.CodeRay .line-numbers strong{font-weight: normal}
table.CodeRay{border-collapse:separate;border-spacing:0;margin-bottom:0;border:0;background:none}
table.CodeRay td{vertical-align: top}
table.CodeRay td.line-numbers{text-align:right}
table.CodeRay td.line-numbers>pre{padding:0;color:rgba(0,0,0,.3)}
table.CodeRay td.code{padding:0 0 0 .5em}
table.CodeRay td.code>pre{padding:0}
.CodeRay .debug{color:#fff !important;background:#000080 !important}
.CodeRay .annotation{color:#007}
.CodeRay .attribute-name{color:#000080}
.CodeRay .attribute-value{color:#700}
.CodeRay .binary{color:#509}
.CodeRay .comment{color:#998;font-style:italic}
.CodeRay .char{color:#04d}
.CodeRay .char .content{color:#04d}
.CodeRay .char .delimiter{color:#039}
.CodeRay .class{color:#458;font-weight:bold}
.CodeRay .complex{color:#a08}
.CodeRay .constant,.CodeRay .predefined-constant{color:#008080}
.CodeRay .color{color:#099}
.CodeRay .class-variable{color:#369}
.CodeRay .decorator{color:#b0b}
.CodeRay .definition{color:#099}
.CodeRay .delimiter{color:#000}
.CodeRay .doc{color:#970}
.CodeRay .doctype{color:#34b}
.CodeRay .doc-string{color:#d42}
.CodeRay .escape{color:#666}
.CodeRay .entity{color:#800}
.CodeRay .error{color:#808}
.CodeRay .exception{color:inherit}
.CodeRay .filename{color:#099}
.CodeRay .function{color:#900;font-weight:bold}
.CodeRay .global-variable{color:#008080}
.CodeRay .hex{color:#058}
.CodeRay .integer,.CodeRay .float{color:#099}
.CodeRay .include{color:#555}
.CodeRay .inline{color:#00}
.CodeRay .inline .inline{background:#ccc}
.CodeRay .inline .inline .inline{background:#bbb}
.CodeRay .inline .inline-delimiter{color:#d14}
.CodeRay .inline-delimiter{color:#d14}
.CodeRay .important{color:#555;font-weight:bold}
.CodeRay .interpreted{color:#b2b}
.CodeRay .instance-variable{color:#008080}
.CodeRay .label{color:#970}
.CodeRay .local-variable{color:#963}
.CodeRay .octal{color:#40e}
.CodeRay .predefined{color:#369}
.CodeRay .preprocessor{color:#579}
.CodeRay .pseudo-class{color:#555}
.CodeRay .directive{font-weight:bold}
.CodeRay .type{font-weight:bold}
.CodeRay .predefined-type{color:inherit}
.CodeRay .reserved,.CodeRay .keyword {color:#000;font-weight:bold}
.CodeRay .key{color:#808}
.CodeRay .key .delimiter{color:#606}
.CodeRay .key .char{color:#80f}
.CodeRay .value{color:#088}
.CodeRay .regexp .delimiter{color:#808}
.CodeRay .regexp .content{color:#808}
.CodeRay .regexp .modifier{color:#808}
.CodeRay .regexp .char{color:#d14}
.CodeRay .regexp .function{color:#404;font-weight:bold}
.CodeRay .string{color:#d20}
.CodeRay .string .string .string{background:#ffd0d0}
.CodeRay .string .content{color:#d14}
.CodeRay .string .char{color:#d14}
.CodeRay .string .delimiter{color:#d14}
.CodeRay .shell{color:#d14}
.CodeRay .shell .delimiter{color:#d14}
.CodeRay .symbol{color:#990073}
.CodeRay .symbol .content{color:#a60}
.CodeRay .symbol .delimiter{color:#630}
.CodeRay .tag{color:#008080}
.CodeRay .tag-special{color:#d70}
.CodeRay .variable{color:#036}
.CodeRay .insert{background:#afa}
.CodeRay .delete{background:#faa}
.CodeRay .change{color:#aaf;background:#007}
.CodeRay .head{color:#f8f;background:#505}
.CodeRay .insert .insert{color:#080}
.CodeRay .delete .delete{color:#800}
.CodeRay .change .change{color:#66f}
.CodeRay .head .head{color:#f4f}</style><link href="reveal.js/lib/css/zenburn.css" rel="stylesheet" /><script type="text/javascript">document.write( '<link rel="stylesheet" href="reveal.js/css/print/' + ( window.location.search.match( /print-pdf/gi ) ? 'pdf' : 'paper' ) + '.css" type="text/css" media="print">' );</script></head><body><div class="reveal"><div class="slides"><section><h2>Ratpack</h2><p><small>Under the hood.</small></p></section><section id="luke-daley"><h2>Luke Daley</h2><div class="paragraph"><p>Creator.</p></div></section>
<section id="what"><h2>What?</h2><div class="ulist"><ul><li class="fragment"><p>Execution (non blocking)</p></li><li class="fragment"><p>Promise transformations</p></li><li class="fragment"><p>Handler composition</p></li></ul></div></section>
<section id="blocking"><h2>Blocking?</h2><div class="paragraph"><p>Any kind of waiting.</p></div>
<div class="ulist"><ul><li class="fragment"><p>IO (e.g. <code>InputStream</code>)</p></li><li class="fragment"><p><code>Lock.lock()</code></p></li><li class="fragment"><p>JDBC</p></li><li class="fragment"><p>Common HTTP clients</p></li><li class="fragment"><p><em>most APIs</em></p></li></ul></div></section>
<section id="why-is-blocking-bad"><h2>Why is blocking “bad”?</h2><div class="paragraph"><p>More threads than there are cores creates <em>contention</em>.</p></div></section>
<section id="blocking-and-threads"><h2>Blocking and threads</h2><div class="paragraph"><p>Each concurrent piece of work requires a separate thread.</p></div>
<div class="paragraph fragment"><p>100 concurrent requests =~ 100 threads</p></div></section>
<section id="performance"><h2>Performance?</h2><div class="paragraph"><p>Not the whole story.</p></div>
<div class="paragraph fragment"><p>Really… low & <em>predictably scalable</em> resource usage.</p></div></section>
<section id="more"><h2>More…</h2><div class="paragraph"><p><a href="http://www.kegel.com/c10k.html">The C10K problem.</a></p></div>
<div class="paragraph"><p><a href="https://www.youtube.com/watch?v=aEuNBk1b5OE&t=499">Why Netflix are moving towards non blocking.</a></p></div></section>
<section id="non-blocking"><h2>Non blocking?</h2><div class="paragraph"><p>Relinquish the thread when you have to wait.</p></div>
<div class="paragraph"><p>Get a thread back when you can do more work.</p></div>
<div class="paragraph fragment"><p><strong>Use it or lose it.</strong></p></div></section>
<section id="blocking-2"><h2>Blocking</h2><div class="paragraph"><p>Render the contents of a file.</p></div>
<div class="listingblock"><div class="content"><pre class="CodeRay"><code class="groovy language-groovy">handlers {
get {
render file(<span class="string"><span class="delimiter">"</span><span class="content">foo.txt</span><span class="delimiter">"</span></span>).text
}
}</code></pre></div></div>
<div class="paragraph"><p>Bad!</p></div></section>
<section id="non-blocking-2"><h2>Non blocking</h2><div class="paragraph"><p>Render the contents of a file.</p></div>
<div class="listingblock"><div class="content"><pre class="CodeRay"><code class="groovy language-groovy"><span class="keyword">import</span> <span class="include">ratpack.exec.Blocking</span>
handlers {
get {
Blocking.get {
file(<span class="string"><span class="delimiter">"</span><span class="content">foo.txt</span><span class="delimiter">"</span></span>).text
} then {
render <span class="local-variable">it</span>
}
}</code></pre></div></div>
<div class="paragraph"><p>Good!</p></div></section>
<section id="non-blocking-2-2"><h2>Non blocking (2)</h2><div class="paragraph"><p>Render the contents of a file.</p></div>
<div class="listingblock"><div class="content"><pre class="CodeRay"><code class="groovy language-groovy"><span class="keyword">import</span> <span class="include">ratpack.exec.Blocking</span>
handlers {
get {
render Blocking.get { file(<span class="string"><span class="delimiter">"</span><span class="content">foo.txt</span><span class="delimiter">"</span></span>).text }
}
}</code></pre></div></div>
<div class="paragraph"><p>Even better!</p></div></section>
<section id="non-blocking-3"><h2>Non blocking (3)</h2><div class="paragraph"><p>Render the contents of a file.</p></div>
<div class="listingblock"><div class="content"><pre class="CodeRay"><code class="groovy language-groovy"><span class="keyword">import</span> <span class="include">ratpack.exec.Promise</span>
handlers {
get {
render Promise.of { down ->
readFileAsync(file(<span class="string"><span class="delimiter">"</span><span class="content">foo.txt</span><span class="delimiter">"</span></span>), <span class="keyword">new</span> <span class="predefined-type">Callback</span>() {
<span class="type">void</span> onDone(<span class="predefined-type">String</span> contents) {
down.success(contents)
}
})
}
}
}</code></pre></div></div>
<div class="paragraph"><p>Better!</p></div></section>
<section id="rendering-files"><h2>Rendering files</h2><div class="paragraph"><p>Use this API.</p></div>
<div class="listingblock"><div class="content"><pre class="CodeRay"><code class="groovy language-groovy">handlers {
get {
render file(<span class="string"><span class="delimiter">"</span><span class="content">foo.txt</span><span class="delimiter">"</span></span>) <span class="comment">// adhoc</span>
}
files { <span class="local-variable">it</span>.dir(<span class="string"><span class="delimiter">"</span><span class="content">public</span><span class="delimiter">"</span></span>) } <span class="comment">// serve a directory</span>
}</code></pre></div></div></section>
<section id="implications"><h2>Implications?</h2><div class="paragraph"><p>Non blocking changes a lot.</p></div>
<div class="ulist"><ul><li class="fragment"><p><code>try</code>/<code>catch</code> doesn’t work.</p></li><li class="fragment"><p><code>ThreadLocal</code> doesn’t work.</p></li><li class="fragment"><p>Most libs aren’t non blocking.</p></li><li class="fragment"><p>Debugging sucks (stack traces are segmented)</p></li></ul></div></section>
<section id="execution"><h2>Execution</h2><div class="paragraph"><p><a href="http://ratpack.io/manual/current/api/index.html?ratpack/exec/Execution.html"><code>ratpack.exec.Execution</code></a></p></div>
<div class="paragraph"><p>Kinda similar to <code>Thread</code> with the blocking model.</p></div>
<div class="paragraph"><p>A logical piece of work (e.g. handling a request).</p></div></section>
<section id="try-catch"><h2>try/catch</h2><div class="paragraph"><p>Executions have an error handler.</p></div>
<div class="listingblock"><div class="content"><pre class="CodeRay"><code class="groovy language-groovy"><span class="keyword">import</span> <span class="include">ratpack.exec.Execution</span>
<span class="keyword">import</span> <span class="include">ratpack.exec.Blocking</span>
Execution.fork().onError {
println <span class="local-variable">it</span>.message
}.start {
Blocking.get { <span class="keyword">throw</span> <span class="keyword">new</span> <span class="exception">Exception</span>(<span class="string"><span class="delimiter">"</span><span class="content">!</span><span class="delimiter">"</span></span>) }
}</code></pre></div></div></section>
<section id="try-catch-2"><h2>try/catch (2)</h2><div class="paragraph"><p>Request executions use <code>ServerErrorHandler</code> as the error handler.</p></div>
<div class="listingblock"><div class="content"><pre class="CodeRay"><code class="groovy language-groovy"><span class="keyword">package</span> ratpack.handling;
<span class="directive">public</span> <span class="type">interface</span> ServerErrorHandler {
<span class="type">void</span> error(<span class="predefined-type">Context</span> context, <span class="predefined-type">Throwable</span> throwable) <span class="directive">throws</span> <span class="exception">Exception</span>;
}</code></pre></div></div></section>
<section id="thread-locals"><h2>Thread locals</h2><div class="paragraph"><p><code>Execution</code> implements <code>MutableRegistry</code></p></div>
<div class="listingblock"><div class="content"><pre class="CodeRay"><code class="groovy language-groovy"><span class="keyword">import</span> <span class="include">ratpack.exec.Execution</span>
<span class="keyword">import</span> <span class="include">ratpack.exec.Blocking</span>
Execution.fork().start {
Execution.current().add(<span class="string"><span class="delimiter">"</span><span class="content">some value</span><span class="delimiter">"</span></span>)
Blocking.get { Execution.current().get(<span class="predefined-type">String</span>) } then { render <span class="local-variable">it</span> }
}</code></pre></div></div></section>
<section id="execinterceptor"><h2>ExecInterceptor</h2><div class="paragraph"><p>Interceptors let you wrap execution segments.</p></div>
<div class="listingblock"><div class="content"><pre class="CodeRay"><code class="groovy language-groovy"><span class="keyword">package</span> ratpack.exec
<span class="directive">public</span> <span class="type">interface</span> ExecInterceptor {
<span class="type">enum</span> ExecType {
BLOCKING,
COMPUTE
}
<span class="type">void</span> intercept(Execution execution, ExecType execType, Block continuation) <span class="directive">throws</span> <span class="exception">Exception</span>;
}</code></pre></div></div></section>
<section id="execinterceptor-2"><h2>ExecInterceptor</h2><div class="paragraph"><p>Let’s you integrate with libs using thread locals.</p></div>
<div class="listingblock"><div class="content"><pre class="CodeRay"><code class="groovy language-groovy"><span class="keyword">import</span> <span class="include">org.slf4j.MDC</span>;
<span class="directive">public</span> <span class="type">void</span> intercept(Execution execution, ExecType type, Block continuation) <span class="directive">throws</span> <span class="exception">Exception</span> {
<span class="predefined-type">Map</span> map = execution.get(<span class="predefined-type">Map</span>.class);
MDC.setContextMap(map);
<span class="keyword">try</span> {
continuation.execute();
} <span class="keyword">finally</span> {
map.clear();
map.putAll(MDC.getCopyOfContextMap());
MDC.clear();
}
}</code></pre></div></div></section>
<section id="promise"><h2>Promise</h2><div class="paragraph"><p>A representation of a <em>potential</em> value.</p></div>
<div class="paragraph fragment"><p>Say things about the value without actually having the value.</p></div></section>
<section id="promise-2"><h2>Promise</h2><div class="listingblock"><div class="content"><pre class="CodeRay"><code class="groovy language-groovy"><span class="keyword">package</span> ratpack.exec;
<span class="directive">public</span> <span class="type">interface</span> Promise<T> {
<span class="directive">static</span> <T> Promise<T> of(Upstream<T> upstream) { }
}
<span class="directive">public</span> <span class="type">interface</span> Upstream<T> {
<span class="type">void</span> connect(Downstream<T> downstream) <span class="directive">throws</span> <span class="exception">Exception</span>;
}
<span class="directive">public</span> <span class="type">interface</span> Downstream<T> {
<span class="type">void</span> success(T value);
<span class="type">void</span> error(<span class="predefined-type">Throwable</span> throwable);
<span class="type">void</span> complete();
}</code></pre></div></div></section>
<section id="promise-usage"><h2>Promise usage</h2><div class="listingblock"><div class="content"><pre class="CodeRay"><code class="groovy language-groovy">Promise.of { down ->
<span class="predefined-type">Thread</span>.start { down.success(<span class="string"><span class="delimiter">"</span><span class="content">foo</span><span class="delimiter">"</span></span>) }
} then {
println <span class="local-variable">it</span> <span class="comment">// prints 'foo'</span>
}</code></pre></div></div></section>
<section id="promise-transformations"><h2>Promise transformations</h2><div class="listingblock"><div class="content"><pre class="CodeRay"><code class="groovy language-groovy">Promise.of { <span class="local-variable">it</span>.down(<span class="string"><span class="delimiter">"</span><span class="content">foo</span><span class="delimiter">"</span></span>) }
.map { <span class="local-variable">it</span>.toUpperCase() }
.flatMap { foo ->
Blocking.get {
<span class="keyword">new</span> <span class="predefined-type">File</span>(<span class="string"><span class="delimiter">"</span><span class="content">foo.txt</span><span class="delimiter">"</span></span>).text
}.map {
<span class="local-variable">it</span> + foo
}
}
.cache()
.then {
println <span class="local-variable">it</span>
}</code></pre></div></div></section>
<section id="promise-transformations-2"><h2>Promise transformations</h2><div class="listingblock"><div class="content"><pre class="CodeRay"><code class="groovy language-groovy"><span class="keyword">package</span> ratpack.exec;
<span class="directive">public</span> <span class="type">interface</span> Promise<T> {
<O> Promise<O> transform(Function<Upstream<T>, Upstream<O>> function);
}</code></pre></div></div></section>
<section id="map"><h2>map()</h2><div class="listingblock"><div class="content"><pre class="CodeRay"><code class="groovy language-groovy"><O> Promise<O> map(Function<T, O> function) {
<span class="keyword">return</span> transform(up ->
down -> up.connect(<span class="keyword">new</span> Downstream<T>() {
<span class="type">void</span> success(T value) {
<span class="keyword">try</span> {
down.success(function.apply(value));
} <span class="keyword">catch</span> (<span class="predefined-type">Throwable</span> e) {
down.error(e);
}
}
<span class="type">void</span> error(<span class="predefined-type">Throwable</span> t) { down.error(t) }
<span class="type">void</span> complete() { down.complete() }
})
);
}</code></pre></div></div></section>
<section id="other-transform-impls"><h2>Other transform impls…</h2><div class="paragraph"><p>Read the <a href="https://github.com/ratpack/ratpack/blob/master/ratpack-core/src/main/java/ratpack/exec/Promise.java">source of <code>ratpack.exec.Promise</code></a>.</p></div></section>
<section id="handler-composition"><h2>Handler Composition</h2><div class="paragraph fragment"><p>All request handling logic is expressed as a single handler.</p></div></section>
<section id="handler"><h2>Handler</h2><div class="listingblock"><div class="content"><pre class="CodeRay"><code class="groovy language-groovy"><span class="keyword">package</span> ratpack.handling;
<span class="directive">public</span> <span class="type">interface</span> <span class="predefined-type">Handler</span> {
<span class="type">void</span> handle(<span class="predefined-type">Context</span> ctx) <span class="directive">throws</span> <span class="exception">Exception</span>;
}</code></pre></div></div></section>
<section id="closures-as-handlers"><h2>Closures as handlers</h2><div class="listingblock"><div class="content"><pre class="CodeRay"><code class="groovy language-groovy">handlers {
get {
render <span class="string"><span class="delimiter">"</span><span class="content">Hello world!</span><span class="delimiter">"</span></span>
}
}</code></pre></div></div>
<div class="listingblock"><div class="content"><pre class="CodeRay"><code class="groovy language-groovy">handlers {
get <span class="keyword">new</span> <span class="predefined-type">Handler</span>() {
<span class="type">void</span> handle(<span class="predefined-type">Context</span> ctx) {
ctx.render <span class="string"><span class="delimiter">"</span><span class="content">Hello world!</span><span class="delimiter">"</span></span>
}
}
}</code></pre></div></div></section>
<section id="handler-delegation"><h2>Handler delegation</h2><div class="paragraph"><p>Handlers either respond, or <em>delegate</em>.</p></div>
<div class="ulist"><ul><li class="fragment"><p><code>Context.next()</code></p></li><li class="fragment"><p><code>Context.insert(Handler…​)</code></p></li><li class="fragment"><p><code>«handler».handle(context)</code></p></li></ul></div></section>
<section id="chain-basics"><h2>Chain basics</h2><div class="listingblock"><div class="content"><pre class="CodeRay"><code class="groovy language-groovy">handlers {
<span class="comment">// delegate is ratpack.handling.Chain</span>
all {
render <span class="string"><span class="delimiter">"</span><span class="content">Hello world!</span><span class="delimiter">"</span></span>
}
}</code></pre></div></div>
<div class="listingblock"><div class="content"><pre class="CodeRay"><code class="groovy language-groovy"><span class="keyword">package</span> ratpack.handling
<span class="type">interface</span> Chain {
Chain all(<span class="predefined-type">Handler</span> handler)
}</code></pre></div></div>
<div class="paragraph"><p>Collects handlers to a list the inserts.</p></div></section>
<section id="chainhandler"><h2>ChainHandler</h2><div class="listingblock"><div class="content"><pre class="CodeRay"><code class="groovy language-groovy"><span class="keyword">package</span> ratpack.handling.internal
<span class="directive">public</span> <span class="type">class</span> <span class="class">ChainHandler</span> <span class="directive">implements</span> <span class="predefined-type">Handler</span> {
<span class="directive">private</span> <span class="directive">final</span> <span class="predefined-type">Handler</span><span class="type">[]</span> handlers;
<span class="directive">public</span> ChainHandler(<span class="predefined-type">Handler</span>... handlers) {
<span class="local-variable">this</span>.handlers = handlers
}
<span class="directive">public</span> <span class="type">void</span> handle(<span class="predefined-type">Context</span> context) {
context.insert(handlers);
}
}</code></pre></div></div></section>
<section id="chain-get-string-handler"><h2>Chain.get(String, Handler)</h2><div class="listingblock"><div class="content"><pre class="CodeRay"><code class="groovy language-groovy"><span class="keyword">import</span> <span class="include">ratpack.handler.Handlers</span>;
<span class="keyword">default</span> Chain get(<span class="predefined-type">String</span> path, <span class="predefined-type">Handler</span> handler) {
<span class="keyword">return</span> all(
Handlers.path(
path,
Handlers.chain(Handlers.get(), handler)
)
);
}</code></pre></div></div></section>
<section id="handlers"><h2>Handlers</h2><div class="paragraph"><p>Composable pieces, and the basis of Chain.</p></div>
<div class="listingblock"><div class="content"><pre class="CodeRay"><code class="groovy language-groovy"><span class="keyword">package</span> ratpack.handling.Handlers;
<span class="directive">public</span> <span class="type">class</span> <span class="class">Handlers</span> {
<span class="directive">public</span> <span class="directive">static</span> <span class="predefined-type">Handler</span> path(<span class="predefined-type">String</span> path, <span class="predefined-type">Handler</span> handler) {
<span class="keyword">return</span> path(PathBinder.parse(path, <span class="predefined-constant">true</span>), handler);
}
<span class="directive">public</span> <span class="directive">static</span> <span class="predefined-type">Handler</span> path(PathBinder pathBinder, <span class="predefined-type">Handler</span> handler) {
<span class="keyword">return</span> <span class="keyword">new</span> PathHandler(pathBinder, handler);
}
<span class="directive">public</span> <span class="directive">static</span> <span class="predefined-type">Handler</span> chain(<span class="predefined-type">Handler</span>... handlers) {
<span class="keyword">return</span> <span class="keyword">new</span> ChainHandler(handlers);
}
<span class="directive">public</span> <span class="directive">static</span> <span class="predefined-type">Handler</span> get() {
<span class="keyword">return</span> <span class="keyword">new</span> MethodHandler(<span class="string"><span class="delimiter">"</span><span class="content">GET</span><span class="delimiter">"</span></span>);
}
}</code></pre></div></div></section>
<section id="pathhandler"><h2>PathHandler</h2><div class="listingblock"><div class="content"><pre class="CodeRay"><code class="groovy language-groovy"><span class="keyword">package</span> ratpack.handling.internal;
<span class="directive">public</span> <span class="type">class</span> <span class="class">PathHandler</span> <span class="directive">implements</span> <span class="predefined-type">Handler</span> {
<span class="directive">private</span> <span class="directive">final</span> PathBinder binder; <span class="directive">private</span> <span class="directive">final</span> <span class="predefined-type">Handler</span> handler;
<span class="directive">public</span> PathHandler(PathBinder binder, <span class="predefined-type">Handler</span> handler) {
<span class="local-variable">this</span>.binder = binder; <span class="local-variable">this</span>.handler = handler;
}
<span class="directive">public</span> <span class="type">void</span> handle(<span class="predefined-type">Context</span> ctx) {
Optional<PathBinding> binding = binder.bind(ctx.get(PathBinding.class));
<span class="keyword">if</span> (binding.isPresent()) {
<span class="predefined-type">Registry</span> registry = <span class="predefined-type">Registry</span>.single(PathBinding.class, binding.get());
ctx.insert(registry, handler);
} <span class="keyword">else</span> {
ctx.next();
}
}
}</code></pre></div></div></section>
<section id="methodhandler"><h2>MethodHandler</h2><div class="listingblock"><div class="content"><pre class="CodeRay"><code class="groovy language-groovy"><span class="keyword">package</span> ratpack.handling.internal;
<span class="directive">public</span> <span class="type">class</span> <span class="class">MethodHandler</span> <span class="directive">implements</span> <span class="predefined-type">Handler</span> {
<span class="directive">private</span> <span class="directive">final</span> <span class="predefined-type">String</span> method;
<span class="directive">public</span> MethodHandler(<span class="predefined-type">String</span> method) {
<span class="local-variable">this</span>.method = method;
}
<span class="directive">public</span> <span class="type">void</span> handle(<span class="predefined-type">Context</span> ctx) {
<span class="keyword">if</span> (ctx.getRequest().getMethod().name(method)) {
ctx.next();
} <span class="keyword">else</span> {
ctx.clientError(<span class="integer">405</span>);
}
}
}</code></pre></div></div></section>
<section id="chain-get-string-handler-2"><h2>Chain.get(String, Handler)</h2><div class="listingblock"><div class="content"><pre class="CodeRay"><code class="groovy language-groovy"><span class="keyword">import</span> <span class="include">ratpack.handler.Handlers</span>;
<span class="keyword">default</span> Chain get(<span class="predefined-type">String</span> path, <span class="predefined-type">Handler</span> handler) {
<span class="keyword">return</span> all(
Handlers.path(
path,
Handlers.chain(Handlers.get(), handler)
)
);
}</code></pre></div></div></section>
<section id="thanks"><h2>Thanks!</h2><div class="ulist"><ul><li><p><a href="http://ratpack.io" class="bare">http://ratpack.io</a></p></li><li><p><a href="https://github.com/ratpack" class="bare">https://github.com/ratpack</a></p></li><li><p>Join the Slack channel and talk to us!</p></li><li><p>Buy Dan Woods' book!</p></li></ul></div></section></div></div><script src="reveal.js/lib/js/head.min.js"></script><script src="reveal.js/js/reveal.js"></script><script type="text/javascript">// See https://github.com/hakimel/reveal.js#configuration for a full list of configuration options
Reveal.initialize({
// Display controls in the bottom right corner
controls: true,
// Display a presentation progress bar
progress: true,
// Display the page number of the current slide
slideNumber: true,
// Push each slide change to the browser history
history: true,
// Enable keyboard shortcuts for navigation
keyboard: true,
// Enable the slide overview mode
overview: true,
// Vertical centering of slides
center: true,
// Enables touch navigation on devices with touch input
touch: true,
// Loop the presentation
loop: false,
// Change the presentation direction to be RTL
rtl: false,
// Turns fragments on and off globally
fragments: true,
// Flags if the presentation is running in an embedded mode,
// i.e. contained within a limited portion of the screen
embedded: false,
// Number of milliseconds between automatically proceeding to the
// next slide, disabled when set to 0, this value can be overwritten
// by using a data-autoslide attribute on your slides
autoSlide: 0,
// Stop auto-sliding after user input
autoSlideStoppable: true,
// Enable slide navigation via mouse wheel
mouseWheel: false,
// Hides the address bar on mobile devices
hideAddressBar: true,
// Opens links in an iframe preview overlay
previewLinks: false,
// Theme (e.g., beige, black, league, night, serif, simple, sky, solarized, white)
// NOTE setting the theme in the config no longer works in reveal.js 3.x
//theme: Reveal.getQueryHash().theme || 'black',
// Transition style (e.g., none, fade, slide, convex, concave, zoom)
transition: Reveal.getQueryHash().transition || 'linear',
// Transition speed (e.g., default, fast, slow)
transitionSpeed: 'default',
// Transition style for full page slide backgrounds (e.g., none, fade, slide, convex, concave, zoom)
backgroundTransition: 'fade',
// Number of slides away from the current that are visible
viewDistance: 3,
// Parallax background image (e.g., "'https://s3.amazonaws.com/hakim-static/reveal-js/reveal-parallax-1.jpg'")
parallaxBackgroundImage: '',
// Parallax background size in CSS syntax (e.g., "2100px 900px")
parallaxBackgroundSize: '',
// The "normal" size of the presentation, aspect ratio will be preserved
// when the presentation is scaled to fit different resolutions. Can be
// specified using percentage units.
width: 960,
height: 700,
// Factor of the display size that should remain empty around the content
margin: 0.1,
// Bounds for smallest/largest possible scale to apply to content
minScale: 0.2,
maxScale: 1.5,
// Optional libraries used to extend on reveal.js
dependencies: [
{ src: 'reveal.js/lib/js/classList.js', condition: function() { return !document.body.classList; } },
{ src: 'reveal.js/plugin/markdown/marked.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'reveal.js/plugin/markdown/markdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'reveal.js/plugin/zoom-js/zoom.js', async: true, condition: function() { return !!document.body.classList; } },
{ src: 'reveal.js/plugin/notes/notes.js', async: true, condition: function() { return !!document.body.classList; } }
]
});</script></body></html>