summaryrefslogtreecommitdiffstats
path: root/talks/farwest-demo/index.html
blob: 8c16b859c55c6baddd42ce63e227cdbc26617ddf (plain) (blame)
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
<!doctype html>
<html>
	<head>
		<meta charset="utf-8">
		<title>Farwest Demo</title>

		<meta name="description" content"Farwest Demo BEAM FOSDEM 2020 talk">
		<meta name="author" content="Loïc Hoguin">

		<link rel="stylesheet" href="css/reveal.css">
		<link rel="stylesheet" href="css/theme/black.css">
	</head>
	<body>
		<div class="reveal">
			<div class="slides">

<section>
	<h1>Farwest Demo</h1>
	<p>An overview and a demo of the Farwest project</p>
	<p><small>Loïc Hoguin, <a href="https://ninenines.eu">Nine Nines</a></small></p>
</section>

<section>
	<h1>What is Farwest?</h1>
</section>

<section>
	<h2>Erlang/OTP REST framework</h2>
	<p>HATEOAS</p>
	<p>Standards based (whenever possible)</p>
	<p>Built on top of <code>cowboy_rest</code></p>
</section>

<section>
	<h2>Building blocks</h2>
	<ul>
		<li>Resource modules</li>
		<li>URI and URI Templates (RFC 6570)</li>
		<li>Media types</li>
		<li>Operations (forms)</li>
		<li>Metadata</li>
	</ul>
</section>

<section>
	<h2>Client libraries</h2>
	<p>One library per language/platform</p>
	<p>Build on top of existing HTTP clients</p>
	<p>Don't write a separate client per API</p>
</section>

<section>
	<h1>Work done so far</h1>
</section>

<section>
	<h2>Discovery</h2>
	<ul>
		<li>Link HTTP header (RFC 8288)</li>
		<li>Links in response bodies</li>
		<li>URI Templates router, reverse routing</li>
		<li>Variants (draft)</li>
		<li>Skeleton Erlang/OTP client library</li>
	</ul>
</section>

<section>
	<h2>Generate response bodies</h2>
	<ul>
		<li>Automatic HTML generation</li>
		<li>Skeleton forms</li>
		<li>Automatic BED (binary Erlang data) generation</li>
	</ul>
</section>

<section>
	<h2>Mod:describe()</h2>
	<ul>
		<li>URI or URI Template</li>
		<li>Operations</li>
		<li>Operation/media type pairs (input/output)</li>
		<li>Links</li>
	</ul>
</section>

<section>
	<h2>Configurable operations</h2>
	<ul>
		<li>HTTP method used</li>
		<li>Semantics (not yet enforced)</li>
	</ul>
</section>

<section>
	<h1>Demo</h1>
	<section>
		<ol>
			<li><code>git clone https://github.com/ninenines/farwest_demo</code></li>
			<li><code>cd farwest_demo</code></li>
			<li><code>make run</code></li>
			<li>Open <a href="http://localhost:8080">http://localhost:8080</a> in your browser</li>
			<li>Use <code>curl -i http://localhost:8080</code></li>
		</ol>
	</section>

	<section>
		<pre><code data-trim data-noescape>
describe() → #{
    uri ⇒ "/",
    media_types ⇒ #{
        html ⇒ ["text/html"],
        bed ⇒ ["application/x-bed"]
    },
    operations ⇒ #{
        get ⇒ #{output ⇒ [html, bed]}
    }
}.
		</code></pre>
	</section>

	<section>
		<pre><code data-trim data-noescape>
links(Req) →
    {ok, [
        {child, fwd_processes_r},
        {child, fwd_tables_r}
    ], Req}.
		</code></pre>
	</section>

	<section>
		<pre><code data-trim data-noescape>
$ curl -I http://localhost:8080       
HTTP/1.1 200 OK
content-length: 2398
content-type: text/html
date: Mon, 27 Jan 2020 11:56:23 GMT
link: &lt;/processes&gt;; rel="child";
    variants-06="accept=(\"text/html\" \"application/x-bed\")",
    &lt;/tables&gt;; rel="child"; variants-06="accept=(\"text/html\")"
server: Cowboy
variant-key-06: ("text/html")
variants-06: accept=("text/html" "application/x-bed")
vary: accept
		</code></pre>
	</section>

	<section>
		<pre><code data-trim data-noescape>
get(Req) →
    Info = observer_backend:sys_info(),
    Data = #{
        «"System Version"» ⇒ g(otp_release, Info),
        ...
    },
    {ok, Data, Req}.

to_representation(Req, html, Data) →
    {ok, farwest_html:from_term(Req, Data), Req};
to_representation(Req, bed, Data) →
    {ok, farwest_bed:from_term(Req, Data), Req}.
		</code></pre>
	</section>

	<section>
		<img src="pics/fwd_system_r.png"/>
	</section>

	<section>
		<pre><code data-trim data-noescape>
$ curl -I http://localhost:8080/tables
HTTP/1.1 200 OK
content-length: 7052
content-type: text/html
date: Mon, 27 Jan 2020 11:56:10 GMT
farwest-link-templates: &lt;/tables/{name}&gt;; rel="child";
    variants-06="accept=(\"text/html\")"
link: &lt;/&gt;; rel="parent"; variants-06="accept=
    (\"text/html\" \"application/x-bed\")"
server: Cowboy
		</code></pre>
	</section>

	<section>
		<img src="pics/fwd_tables_r.png"/>
	</section>

	<section>
		<pre><code data-trim data-noescape>
{'$fw_link', child,
    farwest:link_to(fwd_table_r, #{«"name"» ⇒ TableName}),
    TableName}
		</code></pre>
	</section>

	<section>
		<img src="pics/fwd_table_r.png"/>
	</section>

	<section>
		<pre><code data-trim data-noescape>
describe() → #{
    uri ⇒ "/tables/{name}/{key}",
    media_types ⇒ #{
        html ⇒ ["text/html"],
        term ⇒ ["text/plain"] %% text/x-erlang-term?
    },
    operations ⇒ #{
        get ⇒ #{output ⇒ [html]},
        put ⇒ #{input ⇒ [term]},
        delete ⇒ #{}
    }
}.
		</code></pre>
	</section>

	<section>
		<img src="pics/fwd_table_row_r.png"/>
	</section>

	<section>
		<pre><code data-trim data-noescape>
put(Req0=#{bindings := #{name := Name0}}) →
    Name = binary_to_atom(Name0, utf8),
    {ok, Body, Req} = cowboy_req:read_body(Req0),
    {ok, Tuple} = parse_string(
        unicode:characters_to_list(Body) ++ "."),
    ets:delete_object(Name, Tuple),
    ets:insert(Name, Tuple),
    {ok, Req}.
		</code></pre>
	</section>

	<section>
		<pre><code data-trim data-noescape>
delete(Req=#{bindings := #{name := Name0, key := Key0}}) →
    Name = binary_to_atom(Name0, utf8),
    {ok, Key} = parse_string(
        unicode:characters_to_list(Key0) ++ "."),
    ets:delete(Name, Key),
    {ok, Req}.
		</code></pre>
	</section>
</section>

<section>
	<h1>Upcoming work</h1>
</section>

<section>
	<h2>Improved discovery</h2>
	<p>Automatic sitemap?</p>
	<p>External resources?</p>
	<p>Link relation resources?</p>
	<p>Better discovery of operations</p>
	<p>Include operations/forms in non-HTML formats</p>
	<p>Cache metadata (etag, cache-control...)</p>
</section>

<section>
	<h2>Generate response bodies</h2>
	<p>Template-based HTML generation</p>
	<p>Replace/insert your own templates</p>
	<p>Automatic JSON/JSON-LD/... generation</p>
</section>

<section>
	<h2>Semantics</h2>
	<p>Schemas (schema.org or other)</p>
	<p>Better forms using schemas</p>
	<p>Read data and expand URI Templates</p>
</section>

<section>
	<h2>Improved client libraries</h2>
	<p>More functions</p>
	<p>Client-side cache for Gun</p>
	<p>Non-Erlang client libraries</p>
</section>

<section>
	<h1>Thanks</h1>
	<p><a href="https://github.com/ninenines/farwest">github.com/ninenines/farwest</a></p>
	<p><a href="https://github.com/ninenines/farwest_demo">github.com/ninenines/farwest_demo</a></p>
</section>

			</div>
		</div>
		<script src="js/reveal.js"></script>
		<script>
Reveal.initialize({
//	controls: false,
	progress: false,
	history: true
});
		</script>
	</body>
</html>