-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.html
More file actions
641 lines (517 loc) · 21.7 KB
/
index.html
File metadata and controls
641 lines (517 loc) · 21.7 KB
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Writing (Micro)Services with Flask</title>
<link rel="stylesheet" href="css/reveal.css"/>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"/>
<link rel="stylesheet" href="css/theme/solarized.css" id="theme"/>
<!-- Code syntax highlighting -->
<link rel="stylesheet" href="lib/css/zenburn.css"/>
<link rel="stylesheet" href="local/presentation.css"/>
<!-- Printing and PDF exports -->
<script>
var link = document.createElement( 'link' );
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = window.location.search.match( /print-pdf/gi ) ? 'css/print/pdf.css' : 'css/print/paper.css';
document.getElementsByTagName( 'head' )[0].appendChild( link );
</script>
<!--[if lt IE 9]>
<script src="lib/js/html5shiv.js"></script>
<![endif]-->
</head>
<body>
<div class="reveal">
<div class="slides">
<section id="title">
<h2>Writing (Micro)Services with Flask</h2>
<p><small>Chris St. Pierre
- <a target="_blank" href="http://twitter.com/chris_st_pierre">@chris_st_pierre</a></small></p>
<h4>Follow along:<br />
<a target="_blank" href="https://stpierre.github.io/flask-microservices/">stpierre.github.io/flask-microservices/</a></h4>
<h4>Clone:<br />
<code style="white-space: nowrap;">https://github.com/stpierre/flask-microservices.git</code></h4>
<h4>Ensure that <code>make</code> and Python 2.7 are installed</h4>
</section>
<section id="slack">
<h3>Slack: <code>#flask-microservices</code> at
<a target="_blank"
href="https://usenix-lisa.slack.com">usenix-lisa.slack.com</a></h3>
<h3>Need an invite? <a target="_blank"
href="http://lisainvite.herokuapp.com">lisainvite.herokuapp.com</a></h3>
</section>
<section id="intro">
<div class="tooltip-buttons">
<button class="more-info" data-toggle="tooltip"
title="Click on this symbol for more detailed information about a topic!"></button>
</div>
<h3>We have three hours to write a REST API that
exposes <code>uptime</code> and <code>iostat</code> to the
internet.</h3>
<aside class="notes">
Mostly harmless.
</aside>
</section>
<section id="flask">
<div class="tooltip-buttons">
<button class="more-info" data-toggle="tooltip"
title="<a target='_blank' href='http://flask.pocoo.org/'>Official Flask website and docs</a>"></button>
</div>
<h2>Flask</h2>
<aside class="notes">
Lightweight *Python* web framework.
Need Python.
</aside>
</section>
<section id="flask-restful">
<div class="tooltip-buttons">
<button class="more-info" data-toggle="tooltip"
title="<a target='_blank' href='http://flask-restful-cn.readthedocs.io/en/0.3.5/'>Official Flask-RESTful docs</a>"></button>
</div>
<h2>Flask-RESTFul</h2>
<aside class="notes">
Flask extension to quickly build REST APIs.
</aside>
</section>
<section id="virtualenv">
<div class="tooltip-buttons">
<button class="more-info" data-toggle="tooltip"
title="Unfamiliar with virtualenv? Here's a <a target='_blank' href='http://docs.python-guide.org/en/latest/dev/virtualenvs/'>great intro and overview</a>"></button>
</div>
<pre class="wide"><code class="bash" data-trim>
make venv
</code></pre>
</section>
<section id="uptime">
<h3>The basics: <code>uptime</code></h3>
</section>
<section id="edit-app">
<pre class="wide"><code class="bash" data-trim>
emacs solution/app.py
</code></pre>
</section>
<section id="vi">
<pre class="wide"><code class="bash" data-trim>
% alias vi=emacs
% type vi
vi is aliased to `emacs'
</code></pre>
<aside class="notes">
<ul>
<li>api.resource - route</li>
<li>restful.Resource</li>
<li>get()</li>
</ul>
</aside>
</section>
<section id="tests">
<div class="tooltip-buttons">
<button class="more-info" data-toggle="tooltip"
title="The unit tests just use the builtin <a target='_blank' href='https://docs.python.org/2/library/unittest.html'><code>unittest</code></a> library, plus <a target='_blank' href='https://pypi.python.org/pypi/mock'><code>mock</code></a> and <a target='_blank' href='https://pypi.python.org/pypi/ddt'><code>ddt</code></a>"></button>
</div>
<pre class="wide"><code class="bash" data-trim>
make test
</code></pre>
<aside class="notes">
one failing test
</aside>
</section>
<section id="subprocess-check-output">
<div class="tooltip-buttons">
<button class="more-info" data-toggle="tooltip"
title="<a target='_blank' href='https://docs.python.org/2/library/subprocess.html'>Official <code>subprocess</code> docs</a>"></button>
</div>
<pre class="wide"><code class="python" data-trim>
subprocess.check_output(["uptime"])
</code></pre>
</section>
<section id="uptime-test">
<pre class="wide"><code class="bash" data-trim>
make service
</code></pre>
<pre class="wide"><code class="bash" data-trim>
curl http://localhost:5000/v1/uptime/
</code></pre>
<aside class="notes">
<p>probably still have one failing test, unless you cheated</p>
<p>service must be restarted every time you make a change</p>
</aside>
</section>
<section id="return-dicts">
<!-- todo: why? -->
<div class="tooltip-buttons">
<button class="more-info" data-toggle="tooltip"
title="Returning a dict (an Object, in JavaScript)
prevents <a target='_blank' href='http://blog.jeremiahgrossman.com/2006/01/advanced-web-attack-techniques-using.html'>a very clever type of attack</a> against your application."></button>
</div>
<h2>Always return a dict!</h2>
<aside class="notes">
security & stuff
</aside>
</section>
<section id="uptime-dict">
<pre class="wide"><code class="python" data-trim>
return {"uptime": subprocess.check_output(["uptime"]).strip()}
</code></pre>
<aside class="notes">
<p>one less failing test!</p>
<p>tests designed to only fail as you implement stuff</p>
</aside>
</section>
<section id="uptime-test-dict">
<pre class="wide"><code class="bash" data-trim>
% curl http://localhost:5000/v1/uptime/
{"uptime": "20:16:57 up 14 days, 7:33, 25 users, load average: 0.58, 0.25, 0.14"}
</code></pre>
</section>
<section id="iostat">
<h3>Query strings and error handling: <code>iostat</code></h3>
</section>
<section id="iostat-invocation">
<p>Mac OS:
<pre><code class="nohighlight" data-trim>
iostat -d [-c <count>] [-w <wait>]
</code></pre>
</p>
<p>Linux:
<pre><code class="nohighlight" data-trim>
iostat -d <wait> <count>
</code></pre>
</p>
<aside class="notes">
lots of options to iostat, these are the common options
</aside>
</section>
<section id="query-string">
<!-- TODO: highlight query string -->
<pre class="wide"><code class="nohighlight" data-trim>
http://localhost:5000/v1/iostat/?count=4&wait=2
</code></pre>
</section>
<section id="flask-request-args">
<pre class="wide"><code class="python" data-trim>
wait = int(flask.request.args.get("wait", 1))
</code></pre>
<aside class="notes">
<p>you can figure out count</p>
<p>write iostat()!</p>
</aside>
</section>
<section id="iostat-first-get">
<pre class="wide"><code class="bash" data-trim>
curl http://localhost:5000/v1/iostat/
curl http://localhost:5000/v1/iostat/\?count=2
curl http://localhost:5000/v1/iostat/\?count=5\&wait=1
</code></pre>
<aside class="notes">
<p>escape question mark and ampersand</p>
<p>lots of unit test failures</p>
</aside>
</section>
<section id="idiot-proofing">
<blockquote>"If you make something idiot-proof, someone will
just make a better idiot."</blockquote>
</section>
<section id="dumb-iostat-examples">
<pre class="wide"><code class="bash" data-trim>
curl http://localhost:5000/v1/iostat/\?count=-10
curl http://localhost:5000/v1/iostat/\?count=1.5
curl http://localhost:5000/v1/iostat/\?count=1\&wait=10
</code></pre>
</section>
<section id="erroring-out">
<pre class="wide"><code class="bash" data-trim>
restful.abort(400, msg="Try harder, moron")
</code></pre>
</section>
<section id="dumb-iostat-scenarios">
<div class="tooltip-buttons">
<button class="more-info" data-toggle="tooltip"
title="It might be useful to peruse <a target='_blank' href='http://www.restapitutorial.com/httpstatuscodes.html'>a list of all of the HTTP status codes</a>"></button>
</div>
<ol>
<li><code>count</code> or <code>wait</code> are not integers</li>
<li><code>count</code> or <code>wait</code> are less than 1</li>
<li><code>wait</code> is specified, but <code>count</code> is 1</li>
</ol>
<aside class="notes">
<p>return 400</p>
<p>int() makes the first one easy</p>
</aside>
</section>
<section id="iostat-timeout">
<pre class="wide"><code class="bash" data-trim>
curl http://localhost:5000/v1/iostat/\?count=1000\&wait=1000
</code></pre>
</section>
<section id="design-decision">
<h3>A presentation design decision.</h3>
<aside class="notes">
<p>async > database stuff -- it's just sqlalchemy</p>
<p>async > authn -- apache/nginx can suffice</p>
</aside>
</section>
<section id="iostat-v2">
<pre class="wide"><code class="bash" data-trim>
curl http://localhost:5000/v2/iostat/
</code></pre>
<aside class="notes">
<p>when v1 sucks, write v2. preserve v1 for compat</p>
<p>v2 is full implementation. don't make ppl use v1 for
some stuff and v2 for other stuff</p>
</aside>
</section>
<section id="uptime-v2">
<pre class="wide"><code class="python" data-trim>
@api.resource("/v1/uptime/", "/v2/uptime")
class UptimeV1(restful.Resource):
...
</code></pre>
</section>
<section id="tasks">
<h3>Introducing tasks</h3>
<aside class="notes">
<p>iostat returns a task</p>
<p>task can be polled to get resulting data</p>
</aside>
</section>
<section id="v2-api-overview">
<ul>
<li><code>/v2/uptime/</code>: no change!</li>
<li><code>/v2/iostat/</code>: returns task object</li>
<li><code>/v2/task/</code>: monitor and delete tasks</li>
</ul>
<aside class="notes">
could also make uptime return a task, for consistency
</aside>
</section>
<section id="celery">
<div class="tooltip-buttons">
<button class="more-info" data-toggle="tooltip"
title="<a target='_blank' href='http://www.celeryproject.org/'>Official Celery website and docs</a>"></button>
</div>
<h3>Celery: Distributed Task Queue</h3>
<aside class="notes">
<p>python software that makes this super simple</p>
<p>our implementation uses sqlite, but can use amqp,
redis, etc.</p>
</aside>
</section>
<section id="edit-task">
<pre class="wide"><code class="bash" data-trim>
emacs solution/tasks.py
</code></pre>
<aside class="notes">
the task is just a function, can be called sync or async
</aside>
</section>
<section id="calling-task">
<pre class="wide"><code class="python" data-trim>
result = tasks.iostat.delay(count, wait)
</code></pre>
<aside class="notes">
<p>delay tells it to be async</p>
<p>return value is AsyncResult obj</p>
<p>can return a string</p>
</aside>
</section>
<section id="task-return-value">
<div class="tooltip-buttons">
<button class="more-info" data-toggle="tooltip"
title="Official docs: <a target='_blank' href='http://flask.pocoo.org/docs/0.11/api/#flask.make_response'><code>flask.make_response()</code></a> and <a target='_blank' href='http://flask.pocoo.org/docs/0.11/api/#flask.json.jsonify'><code>flask.jsonify()</code></a>"></button>
</div>
<pre class="wide"><code class="python" data-trim>
return flask.make_response(
flask.jsonify({"task_id": result.id,
"links": [
{"rel": "task",
"href": api.url_for(TaskV2,
task_id=result.id)}]}),
201)
</code></pre>
<aside class="notes">
<p>result is AsyncResult obj</p>
<p>201 means created</p>
<p>we return the URL to the task for polling</p>
</aside>
</section>
<section id="create-iostat-task">
<pre class="wide"><code class="bash" data-trim>
make celery
</code></pre>
<pre class="wide"><code class="bash" data-trim>
% curl http://localhost:5000/v2/iostat/
{
"links": [
{
"href": "/v2/task/1800799b-bfa1-4e14-9552-02d2dd82f01a",
"rel": "task"
}
],
"task_id": "1800799b-bfa1-4e14-9552-02d2dd82f01a"
}
</code></pre>
<aside class="notes">
can create task, can't monitor it or get results
</aside>
</section>
<section id="task-api">
<div class="tooltip-buttons">
<button class="more-info" data-toggle="tooltip"
title="See <a target='_blank' href='http://www.restapitutorial.com/lessons/httpmethods.html'>this handy list of the main REST HTTP verbs</a>"></button>
</div>
<ul>
<li><code>GET /v2/task/<task_id>/</code>: poll a
task</li>
<li><code>DELETE /v2/task/<task_id>/</code>: cancel
a task</li>
</ul>
</section>
<section id="url-params">
<div class="tooltip-buttons">
<button class="more-info" data-toggle="tooltip"
title="The <code><string:...></code> is not a type, but a werkzeug converter. See <a target='_blank' href='http://werkzeug.pocoo.org/docs/0.11/routing/#builtin-converters'>the full list of builtin converters</a>"></button>
</div>
<pre class="wide"><code class="python" data-trim>
@api.resource("/v2/task/<string:task_id>")
class TaskV2(restful.Resource):
def get(self, task_id):
...
</code></pre>
</section>
<section id="instantiating-task-obj">
<div class="tooltip-buttons">
<button class="more-info" data-toggle="tooltip"
title="<a target='_blank' href='http://docs.celeryproject.org/en/latest/reference/celery.result.html#celery.result.AsyncResult'>Official <code>celery.result.AsyncResult</code> docs</a>"></button>
</div>
<pre class="wide"><code class="python" data-trim>
task = tasks.app.AsyncResult(task_id)
</code></pre>
</section>
<section id="task-obj-considerations">
<ul>
<li>If <code>task.state == "PENDING"</code>, the task
doesn't exist.</li>
<li><code>task.ready()</code> tells you if the task is
done running and <code>task.result</code> is
meaningful.</li>
<li>If <code>task.result</code> is an exception, the task
failed.</li>
</ul>
<aside class="notes">
<p>Return 404 on pending<p>
<p>Stringify exceptions</p>
</aside>
</section>
<section id="task-get-return-value">
<ul>
<li><code>task_id</code></li>
<li><code>state</code></li>
<li><code>result</code>, only if the task is ready</li>
<li><code>links</code></li>
</ul>
<aside class="notes">
<p>links should include self and, if appropriate, cancel</p>
</aside>
</section>
<section id="task-get-delete-link">
<pre class="wide"><code class="python" data-trim>
retval["links"].append({"rel": "cancel",
"method": "DELETE",
"href": api.url_for(TaskV2, task_id=task.id)})
</code></pre>
<aside class="notes">
only include this if the task can be canceled -- that is,
if it's not ready
</aside>
</section>
<section id="defining-delete">
<pre class="wide"><code class="python" data-trim>
@api.resource("/v2/task/<string:task_id>")
class TaskV2(restful.Resource):
def get(self, task_id):
...
def delete(self, task_id):
...
</code></pre>
<aside class="notes">
add a function for any HTTP verb you want to support
</aside>
</section>
<section id="deleting-task">
<pre class="wide"><code class="python" data-trim>
task.revoke()
</code></pre>
</section>
<section id="delete-return-value">
<div class="tooltip-buttons">
<button class="more-info" data-toggle="tooltip"
title="The <a target='_blank' href="http://www.restapitutorial.com/httpstatuscodes.html">full list of HTTP return codes</a>, including the ever-useful 418."></button>
</div>
<pre class="wide"><code class="python" data-trim>
return flask.make_response("", 204)
</code></pre>
<aside class="notes">
204 == accepted
</aside>
</section>
<section id="v3-db">
<h3>Extra credit: API v3</h3>
<ul>
<li>Create (POST), read (GET), update (PUT/PATCH), and
delete (DELETE) users in a database</li>
<li>Database can be created with <code>make
v3-database</code></li>
<li>User records have four fields: username, password,
admin, fullname</li>
</ul>
</section>
<section id="v3-authnz">
<h3>Extra credit: API v3</h3>
<ul>
<li>Use users in the database for authentication</li>
<li>Unauthenticated users can only
access <code>/v3/</code></li>
<li>Non-admin users cannot create, update, or delete users,
but can do everything else</li>
<li>Admin users can do everything</li>
<li>v1 and v2 APIs do not need authentication</li>
</ul>
</section>
<section id="v3-deets">
<h3>Extra credit: API v3</h3>
<ul>
<li>Unit tests are already provided</li>
<li>One solution is in <code>SOLUTION_DO_NOT_PEEK/</code></li>
</ul>
</section>
</div>
</div>
<script src="lib/js/head.min.js"></script>
<script src="js/reveal.js"></script>
<script src="https://code.jquery.com/jquery-3.1.0.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="local/presentation.js"></script>
<script>
// Full list of configuration options available at:
// https://github.com/hakimel/reveal.js#configuration
Reveal.initialize({
controls: true,
progress: true,
history: true,
center: true,
transition: 'slide', // none/fade/slide/convex/concave/zoom
// Optional reveal.js plugins
dependencies: [
{ src: '../lib/js/classList.js', condition: function() { return !document.body.classList; } },
{ src: '../plugin/markdown/marked.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: '../plugin/markdown/markdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: '../plugin/highlight/highlight.js', async: true, condition: function() { return !!document.querySelector( 'pre code' ); }, callback: function() { hljs.initHighlightingOnLoad(); } },
{ src: '../plugin/zoom-js/zoom.js', async: true },
{ src: '../plugin/notes/notes.js', async: true }
]
});
</script>
</body>
</html>