-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.html
1140 lines (943 loc) · 50.1 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
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
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html prefix="dc: http://purl.org/dc/elements/1.1/" lang="en">
<head>
<title>Liberator</title>
<meta charset="utf-8">
<meta property="dc:creator" content="Malcolm Sparks"/>
<meta property="dc:contributor" content="Philipp Meier"/>
<meta property="dc:language" content="en"/>
<meta property="dc:title" content="Liberator"/>
<meta property="dc:description" content="A free, open-source web library for the Clojure programming language with an emphasis on REST"/>
<!-- http://www.google.com/webfonts#UsePlace:use/Collection:Molengo|Anonymous+Pro:400,700|Oregano -->
<link href='http://fonts.googleapis.com/css?family=Molengo|Anonymous+Pro:400,700|Oregano' rel='stylesheet' type='text/css'>
<style>
html {
margin: 0;
padding: 0;
}
body {
margin: 0;
padding: 0;
color: black;
font-family: Molengo,Helvetica,Arial;
font-size: 11pt;
background: #cfcfcf;
background-image: url("zenbg-1.png"), url("zenbg-2.png");
background-repeat: repeat-x, repeat;
}
#title { display: none; }
div.fixed {
position: fixed;
}
div#logo {
float: left;
width: 70px;
margin-top: 10px;
margin-left: 10px;
border: 1px solid black;
}
<!--
We have a fixed header of 40px, we means that the anchor links need to be offset by the same.
http://www.pixelflips.com/blog/anchor-links-with-a-fixed-header/
-->
span.anchor {
display: block;
height: 40px;
margin-top: -40px;
display: hidden;
}
nav {
clear: left;
}
nav ul {
font-family: Molengo,Helvetica,Arial;
margin: 0 4pt;
padding: 0 0;
font-size: 12pt;
}
nav li {
list-style-type: none;
margin: 1em 0 1em 0;
padding: 2pt;
text-align: center;
}
nav li a {
color: #363;
text-decoration: none;
}
nav li a:hover {
text-decoration: underline;
}
h1, h2 {
font-family: Oregano, cursive;
font-weight: normal;
text-shadow: 1px 1px 6px #ddd;
}
h2 {
font-size: 17pt
}
p#description {
font-family: Molengo,Helvetica,Arial; font-size: 20pt; margin-top: 3pt;
font-size: 160%;
margin-top: 6pt;
margin-left: 0px;
text-shadow: 1px 1px 6px #ddd;
}
#main {
padding-bottom: 40px;
margin-top: 50px;
margin-left: 100px;
}
#content {
float: left;
box-shadow: -4px 4px 7px #aa8;
}
section {
width: 620px;
margin-left: 12pt;
margin-right: 12pt;
}
#asides {
margin: 4em 0 0 0;
float: left;
width: 320px;
}
aside {
background: #6c6;
margin: 10pt 0 30pt 10px;
font-size: 80%;
padding: 6pt 10pt 10pt 10pt;
box-shadow: 6px 6px 17px #aa8;
text-align: justify;
}
aside h1, aside h2 {
text-shadow: 1px 1px 6px #4a4;
}
aside h2 { font-size: 14pt; }
svg { border: 0px dotted black }
div#logo svg { border: none; }
div#status { margin-left: 40pt; float: left }
dl.callouts dt { float: left; }
dl.callouts dd { font-size: 80%; padding-bottom: 4pt; margin-left: 16pt }
pre {
font-family: 'Anonymous Pro', monospace;
background: black;
margin-left: 10pt;
padding: 6px;
overflow: auto;
border: 2px solid grey;
color: lightgrey;
color: #2c2
}
.clojure.symbol { color: #2c2; font-weight: bold }
.clojure.keyword { color: #8cc; font-weight: normal }
.clojure.string { color: orange; font-weight: normal; font-size: 80% }
.code.highlight { background: #300 }
table {
border-width: 3px;
border-color: #666666;
border-collapse: collapse;
}
td {
border-style: solid;
border-color: #666666;
background-color: #ffffff;
}
th {
border-style: solid;
border-color: #666666;
background-color: #dedede;
}
kbd {
font-weight: bold;
}
div#banner {
min-height: 40px;
background: rgba(0,0,0,0.8);
opacity: 5;
margin: 0;
padding: 0;
top: 0;
width: 100%;
}
div#banner h1 {
color: #ccc;
margin: 0 0 0 0.5em;
padding: 0;
}
#alert {
background: #ff4;
opacity: 0.8;
margin: 50px 20px 20px 120px;
min-height: 40px;
box-shadow: 6px 6px 17px #aa8;
float: left;
}
#alert p {
padding: 2pt;
margin: 2pt;
color: black;
font-size: 14pt;
opacity: 1;
}
.hidden {
display: none;
}
dl.faq dt {
margin: 12pt;
font-size: 14pt;
}
footer {
clear: left;
float: left;
width: 90%;
text-align: right;
border-top: 1px solid #aaa;
padding-top: 2pt 2pt 2pt 0pt;
margin: 40px 10px 10px 0px;
}
figcaption {
margin: 4pt;
}
</style>
</head>
<body>
<script>
show_alert = function() {
document.querySelector("#alert").classList.remove("hidden");
}
hide_alert = function() {
document.querySelector("#alert").classList.add("hidden");
}
</script>
<div id=banner class=fixed>
<h1>Liberator</h1>
</div>
<div id="alert" class=hidden>
<p>Just to let you know, this is an <span style="color: red; font-style: italic">unofficial</span> version of the Liberator site. The official version is
at <a href="http://clojure-liberator.github.com/">http://clojure-liberator.github.com/</a>. </p>
<p style="font-size: smaller"><a href="javascript:hide_alert()">Hide</a></p>
</div>
<script>
if (location.hostname != "clojure-liberator.github.com") {
show_alert();
}
</script>
<a href="https://github.com/clojure-liberator/liberator">
<img style="position: absolute; top: 0; right: 0; border: 0;" src="forkme_right_green_007200.png" alt="Fork me on GitHub">
</a>
<div class=fixed>
<nav>
<ul>
<li><a href="#introduction">Intro</a>
<li><a href="#flowchart">Flowchart</a>
<li><a href="#functions">Functions</a>
<li><a href="#getting-started">Get started</a>
<li><a href="#community">Community</a>
<li><a href="#faq">FAQ</a>
<li><a href="https://github.com/clojure-liberator/liberator">Github</a>
</ul>
</nav>
</div>
<div id=main>
<div id=content>
<!-- We put empty spans before each section to provide anchor targets:
See http://www.pixelflips.com/blog/anchor-links-with-a-fixed-header/ for more details. -->
<span class="anchor" id="introduction"></span>
<section>
<h1 id=title>Liberator</h1>
<p id=description>With Liberator it is <em>easy</em> to create <abbr title="Representational State Transfer">REST</abbr>ful web services in Clojure.</p>
</section>
<section>
<h2>What it is</h2>
<p>
Liberator is a Clojure library that helps you expose your data
as REST resources while automatically complying with all the
relevant requirements of HTTP specification
(<a href="http://www.ietf.org/rfc/rfc2616.txt">RFC-2616</a>). Your
resources will automatically gain useful HTTP features, such as
caching and content negotiation. Liberator is loosely modeled on
<a href="http://wiki.basho.com/Webmachine.html">webmachine</a>.
</section>
<span class="anchor" id="status"></span>
<section>
<h2>Status</h2>
<p>Latest version: <a href="https://clojars.org/liberator">0.5.0</a>
</section>
<section>
<h2>Where it fits</h2>
<p>
Liberator is not a full-featured web framework like
<a href="http://www.webnoir.org">Noir</a>. Rather, Liberator forms part of a composition of other components. So be prepared to ...
<ul>
<li>Add a routing library, like <a href="https://github.com/weavejester/compojure">Compojure</a> or <a href="https://github.com/cgrand/moustache">Moustache</a>
<li>Add a presentation library, like <a href="https://github.com/weavejester/hiccup">Hiccup</a>
<li>Add some miscellaneous Ring middleware
</ul>
<p>If you are familiar with Ring, Liberator <dfn>resources</dfn>
are Ring handlers. A Ring handler is a Clojure function that
takes a map as an argument (representing the web request) and
returns another map representing the web response.
<p>You connect a Liberator resource to a route. For example,
if you use Compojure as your routing library, you would connect things like this :-
<pre><code>(<span class="clojure symbol">ns</span> <span class="clojure symbol">com.acme</span>
(<span class="clojure keyword">:use</span>
[<span class="clojure symbol">compojure.core</span> <span class="clojure keyword">:only</span> [<span class="clojure symbol">ANY</span>]]
[<span class="clojure symbol">liberator.core</span> <span class="clojure keyword">:only</span> [<span class="clojure symbol">resource</span>]]))
(<span class="clojure symbol">ANY</span> <span class="clojure string">"/products/:sku"</span> [sku]
<span class="code highlight">(<span class="clojure symbol">resource</span> ①)</span></code></pre>
<dl class="callouts">
<dt>①
<dd>The Liberator <code>resource</code> function takes an
even number of arguments, each pair is a keyword
(indicating the argument name) and the argument
value. There are over 50 different arguments you can give
to this function, all of which will be explained
later.
</dl>
<p>Now if we sent a web request to the server with the path <code>/products/54354</code> it would be routed to our Liberator resource.
<p>This is how it all fits together :-
<figure>
<svg xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://www.w3.org/2000/svg" version="1.1" width="600" height="180">
<defs>
<style type="text/css">
<![CDATA[
text {
font-size: 9pt;
font-family: Molengo,Arial;
}
]]>
</style>
<marker id="arrowhead"
viewBox="0 0 10 10" refX="5" refY="5"
markerUnits="strokeWidth"
markerWidth="4" markerHeight="3" fill="darkred"
stroke="black"
orient="auto">
<path d="M 0 0 L 10 5 L 0 10 z" />
</marker>
<linearGradient id="clojure">
<stop offset="5%" stop-color="#0f0" />
<stop offset="95%" stop-color="#0f0" />
</linearGradient>
<linearGradient id="liberator">
<stop offset="5%" stop-color="#FA0" />
<stop offset="95%" stop-color="#FA0" />
</linearGradient>
</defs>
<g transform="translate(30,20)">
<rect x="0" y="0" width="120" height="140" fill="grey" opacity="0.5" stroke="black" stroke-width="2"/>
<text x="60" y="60" text-anchor="middle" dominant-baseline="central">Browser or other</text>
<text x="60" y="80" text-anchor="middle" dominant-baseline="central">HTTP user-agent</text>
</g>
<g transform="translate(275,20)">
<rect x="0" y="0" width="100" height="140" fill="#0f0" opacity="0.5" stroke="black" stroke-width="2"/>
<text x="50" y="50" text-anchor="middle" dominant-baseline="central">Java-based</text>
<text x="50" y="70" text-anchor="middle" dominant-baseline="central">web server</text>
<text x="50" y="90" text-anchor="middle" dominant-baseline="central">(eg. Jetty)</text>
</g>
<a xlink:href="http://google.com?q=clojure+ring">
<g transform="translate(375,160) rotate(-90)">
<rect x="0" y="0" width="140" height="30" fill="url(#clojure)" opacity="0.5" stroke="black" stroke-width="2"/>
<text x="70" y="15" text-decoration="underline" text-anchor="middle" dominant-baseline="central" fill="blue">Ring interface</text>
</g>
</a>
<g transform="translate(405,160) rotate(-90)">
<rect x="0" y="0" width="140" height="40" fill="url(#clojure)" opacity="0.5" stroke="black" stroke-width="2"/>
<text x="70" y="12" text-anchor="middle" dominant-baseline="central">Ring-middleware</text>
<text x="70" y="28" text-anchor="middle" dominant-baseline="central">and routing</text>
</g>
<g transform="translate(450,160) rotate(-90)">
<!-- <rect x="0" y="0" width="140" height="55" fill="green" opacity="0.2" stroke="black" stroke-width="2"/> -->
<path d="M0,50 h20 v10 h20 v-10 h20 v10 h20 v-10 h20 v10 h20 v-10 h20 v-55 h-140 z" stroke-width="2" stroke="black" fill="url(#liberator)" opacity="0.5"/>
<text x="70" y="20"><tspan text-anchor="middle" dominant-baseline="central" font-size="12pt" font-weight="bold">Liberator</tspan></text>
</g>
<g transform="translate(515,160) rotate(-90)">
<!-- <rect x="0" y="0" width="140" height="40" fill="green" opacity="0.2" stroke="black" stroke-width="2"/>-->
<path d="M0,65 v-80 h20 v10 h20 v-10 h20 v10 h20 v-10 h20 v10 h20 v-10 h20 v80z" stroke-width="2" stroke="black" fill="url(#clojure)" opacity="0.5"/>
<text x="70" y="10"><tspan text-anchor="middle" dominant-baseline="central" font-size="12pt" font-weight="bold" fill="green">Your</tspan></text>
<text x="70" y="30"><tspan text-anchor="middle" dominant-baseline="central" font-size="12pt" font-weight="bold" fill="green">Clojure code</tspan></text>
</g>
<path id="request-response" d="M150,30 h400 a 60,20 270 0,1 0,120 h-390" stroke="darkred" opacity="0.5" stroke-width="5px" fill="none" marker-end="url(#arrowhead)"/>
<circle cx="0" cy="0" r="4" fill="white">
<desc>This is the 'pulse' that follows the request-response path to indicate the order that events happen. Not that useful but makes things a bit more obvious.</desc>
<animateMotion dur="2s" repeatCount="2" rotate="auto">
<mpath xlink:href="#request-response"/>
</animateMotion>
</circle>
</svg>
</figure>
<p>(If you need more information about adding the web server component, see the Ring documentation <a href="#">here</a>).
</section>
<span class="anchor" id="works"></span>
<section>
<h2>How it works</h2>
<p>Liberator works by using a <b>decision tree</b> to determine how to
properly handle the request.
<p>Every decision is made by a <dfn>decision function</dfn>. The
diagram below illustrates how the outcomes of decision functions
determine the response status and the <dfn>handler function</dfn>
that will produce the response.
<figure>
<svg id="flow-diagram" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://www.w3.org/2000/svg" version="1.1" width="600" height="330">
<defs>
<style type="text/css">
<![CDATA[
text {
font-size: 9pt;
font-family: Molengo,Arial;
}
]]>
</style>
<g id="handler">
<rect y="-15" width="180" height="30" stroke="black" stroke-width="1px" fill="#acccff"/>
<path d="M 10,-15 l 0,30 M 170,-15 l 0,30" stroke="black"/>
</g>
<g id="decision">
</g>
<g id="entry">
<rect x="0" y="0" width="240" height="20" stroke="#c0c0c0" stroke-width="1" fill="none"/>
<path d="M70,0 v20" stroke="#c0c0c0" stroke-width="1"/>
</g>
<marker id="darkorange-marker"
viewBox="0 0 10 10" refX="5" refY="5"
markerUnits="strokeWidth"
markerWidth="4" markerHeight="3" fill="darkorange"
orient="auto">
<path d="M 0 0 L 10 5 L 0 10 z" />
</marker>
<marker id="darkblue-marker"
viewBox="0 0 10 10" refX="5" refY="5"
markerUnits="strokeWidth"
markerWidth="4" markerHeight="3" fill="purple"
orient="auto">
<path d="M 0 0 L 10 5 L 0 10 z" />
</marker>
<marker id="darkgreen-marker"
viewBox="0 0 10 10" refX="5" refY="5"
markerUnits="strokeWidth"
markerWidth="4" markerHeight="3" fill="darkgreen"
orient="auto">
<path d="M 0 0 L 10 5 L 0 10 z" />
</marker>
<marker id="grey-marker"
viewBox="0 0 10 10" refX="5" refY="5"
markerUnits="strokeWidth"
markerWidth="4" markerHeight="3" fill="grey"
orient="auto">
<path d="M 0 0 L 10 5 L 0 10 z" />
</marker>
</defs>
<g transform="translate(135,55)">
<path d="M 0,-50 v 25" stroke-width="3px" stroke="purple" marker-end="url(#darkblue-marker)"/>
<text x="-15" y="-40">
<tspan text-anchor="end" dominant-baseline="central"
font-style="normal">START</tspan>
</text>
<g transform="translate(0,0)">
<text x="-25" y="0">
<tspan text-anchor="end" dominant-baseline="central"
font-style="normal">:service-available?</tspan>
</text>
<text x="-25" y="15" visibility="hidden">
<tspan text-anchor="end" dominant-baseline="central"
font-style="normal">TRUE</tspan>
<set id="anim5" attributeName="visibility" to="visible" begin="0.2s" dur="indefinite"/>
</text>
<g transform="rotate(45)">
<path d="M 0,-20 l 20,20 -20,20 -20,-20 Z" stroke-width="2px" stroke="black" fill="none"/>
<path d="M 20,0 h 15" stroke-width="3px" stroke="grey" marker-end="url(#grey-marker)"/>
<animateTransform id="anim1" attributeName="transform"
attributeType="XML"
type="rotate" from="45" to="90"
begin="0s" dur="0.4s" fill="freeze" />
</g>
<g transform="translate(40,0)">
<text x="5" y="0"><tspan text-anchor="start" dominant-baseline="middle" font-style="normal">:handle-service-not-available</tspan></text>
<g transform="translate(175,-20)">
<use x="0" y="0" xlink:href="#entry"/>
<text x="5" y="10" dominant-baseline="central" font-style="normal">:status</text>
<text x="75" y="10" dominant-baseline="central">503</text>
<use x="0" y="20" xlink:href="#entry"/>
<text x="5" y="30" dominant-baseline="central" font-style="normal">:message</text>
<text x="75" y="30" dominant-baseline="central">"Service unavailable."</text>
</g>
</g>
</g>
<g transform="translate(0,60)">
<text x="-25" y="0"><tspan text-anchor="end" font-style="normal" dominant-baseline="central">:authorized?</tspan></text>
<text x="-25" y="15" visibility="hidden">
<tspan text-anchor="end" dominant-baseline="central"
font-style="normal">TRUE</tspan>
<set id="anim6" attributeName="visibility" to="visible" begin="0.4s" dur="indefinite"/>
</text>
<g transform="rotate(45)">
<path d="M 0,-20 l 20,20 -20,20 -20,-20 Z" stroke-width="2px" stroke="black" fill="none"/>
<path d="M 20,0 h 15" stroke-width="3px" stroke="grey" marker-end="url(#grey-marker)"/>
<animateTransform id="anim2"
attributeName="transform"
attributeType="XML"
type="rotate" from="45" to="90"
begin="0.15s" dur="0.4s" fill="freeze" />
</g>
<g transform="translate(40,0)">
<text x="5" y="0"><tspan font-style="normal" dominant-baseline="middle" text-anchor="start">:handle-unauthorized</tspan></text>
<g transform="translate(135,-20)">
<use x="0" y="0" xlink:href="#entry"/>
<text x="5" y="10" dominant-baseline="central" font-style="normal">:status</text>
<text x="75" y="10" dominant-baseline="central">401</text>
<use x="0" y="20" xlink:href="#entry"/>
<text x="5" y="30" dominant-baseline="central" font-style="normal">:message</text>
<text x="75" y="30" dominant-baseline="central">"Not authorized."</text>
</g>
</g>
</g>
<g transform="translate(0,120)">
<text x="-25" y="0" text-anchor="end" font-style="normal" dominant-baseline="central">:valid-entity-length?</text>
<text x="-25" y="15" visibility="hidden">
<tspan text-anchor="end" dominant-baseline="central"
font-style="normal">TRUE</tspan>
<set id="anim7" attributeName="visibility" to="visible" begin="0.6s" dur="indefinite"/>
</text>
<g transform="rotate(45)">
<path d="M 0,-20 l 20,20 -20,20 -20,-20 Z" stroke-width="2px" stroke="black" fill="none"/>
<path d="M 20,0 h 15" stroke-width="3px" stroke="grey" marker-end="url(#grey-marker)"/>
<animateTransform id="anim3"
attributeName="transform"
attributeType="XML"
type="rotate" from="45" to="90"
begin="0.3s" dur="0.4s" fill="freeze" />
</g>
<g transform="translate(40,0)">
<text x="5" y="0"><tspan font-style="normal" dominant-baseline="middle" text-anchor="start">:handle-request-entity-too-large</tspan></text>
<g transform="translate(185,-20)">
<use x="0" y="0" xlink:href="#entry"/>
<text x="5" y="10" dominant-baseline="central" font-style="normal">:status</text>
<text x="75" y="10" dominant-baseline="central">413</text>
<use x="0" y="20" xlink:href="#entry"/>
<text x="5" y="30" dominant-baseline="central" font-style="normal">:message</text>
<text x="75" y="30" dominant-baseline="central">"Request Entity Too Large."</text>
</g>
</g>
</g>
<g transform="translate(0,180)">
<path d="M 40,0 l30,30 h365 v-60 h-365 z" stroke-width="2px" stroke="purple" fill="none" opacity="0">
<animate attributeType="CSS" attributeName="opacity"
from="0" to="0.8" begin="0.8s" dur="0.2s" fill="freeze" />
<desc>Highlighting the successful result</desc>
</path>
<text x="-25" y="0"><tspan text-anchor="end" font-style="normal" dominant-baseline="central">:resource-exists?</tspan><tspan>①</tspan></text>
<text x="-25" y="15" visibility="hidden">
<tspan text-anchor="end" dominant-baseline="central"
font-style="normal">FALSE</tspan>
<set id="anim8" attributeName="visibility" to="visible" begin="0.8s" dur="indefinite"/>
</text>
<g transform="rotate(45)">
<path d="M 0,-20 l 20,20 -20,20 -20,-20 Z" stroke-width="2px" stroke="black" fill="none"/>
<path d="M 20,0 h 15" stroke-width="3px" stroke="grey" marker-end="url(#grey-marker)"/>
<animateTransform id="anim4"
attributeName="transform"
attributeType="XML"
type="rotate" from="45" to="0"
begin="0.45s" dur="0.4s" fill="freeze" />
</g>
<g transform="translate(40,0)">
<text x="15" y="0"><tspan font-style="normal" dominant-baseline="middle" text-anchor="start">:handle-not-found</tspan><tspan>②</tspan></text>
<g transform="translate(145,-20)">
<use x="0" y="0" xlink:href="#entry"/>
<text x="5" y="10" dominant-baseline="central" font-style="normal">:status</text>
<text x="75" y="10" dominant-baseline="central">404</text>
<use x="0" y="20" xlink:href="#entry"/>
<text x="5" y="30" dominant-baseline="central" font-style="normal">:message</text>
<text x="75" y="30" dominant-baseline="central">"Not Found."</text>
</g>
</g>
</g>
<g transform="translate(-30,240)">
<text x="5" y="0"><tspan font-style="normal" dominant-baseline="middle" text-anchor="start">:handle-ok</tspan></text>
<g transform="translate(75,-20)">
<use x="0" y="0" xlink:href="#entry"/>
<text x="5" y="10" dominant-baseline="central" font-style="normal">:status</text>
<text x="75" y="10" dominant-baseline="central">200</text>
<use x="0" y="20" xlink:href="#entry"/>
<text x="5" y="30" dominant-baseline="central" font-style="normal">:message</text>
<text x="75" y="30" dominant-baseline="central">"OK."</text>
</g>
</g>
</g>
</svg>
</figure>
<script>
pressdo = function() {
document.getElementById("anim1").beginElement();
document.getElementById("anim2").beginElement();
document.getElementById("anim3").beginElement();
document.getElementById("anim4").beginElement();
}
</script>
<p style="text-align: right" >
<button onclick="pressdo()">Play animation</button>
</p>
<dl class=callouts>
<dt>①
<dd>This is a <em>decision function</em>.
<dt>②
<dd>This is a <em>handler function</em>.
</dl>
<p>As a developer you can specify <b>replacements</b> to both these types of function.
<p>For example, suppose we want to override the default 404
message. We can provide a replacement to
the <em>:handle-not-found</em> handler function when we define the
resource, like this:
<pre><code>(<span class="clojure symbol">compojure/ANY</span> <span class="clojure string">"/products/:sku"</span> [sku]
(<span class="clojure symbol">liberator/resource</span>
<span class="clojure keyword">:handle-not-found</span> (<span class="clojure symbol">fn</span>① [ctx②]
(<span class="clojure symbol">str</span> <span class="clojure string">"No such product: "</span> sku)③)))</code></pre>
<dl class=callouts>
<dt>①
<dd>We are providing a replacement function, but we could just as easily specify a constant or string value in this case. Constant values can be used in place of functions where appropriate.
<dt>②
<dd>This is the Liberator <em>context argument</em>. We'll come to this shortly.
<dt>③
<dd>We returned only a string here. Liberator is smart enough to turn this into a proper Ring response. This is a useful feature of Liberator, that it will coerce whatever you return into a correct Ring response, using whatever information it has already established (via content negotiation, etc.)
</dl>
<p>So the value which will be returned from the resource will be something like this :-
<pre><code>{:status 404
:body "No such product: 12345"}</code></pre>
<p>This will be processed by Ring to generate the final HTTP response.
<p>But shouldn't the body be valid HTML? This question assumes
that the user-agent (the software that sent us the request) is capable
of displaying HTML but we don't yet know that.
<p>Before we change the response body to valid HTML, let's
indicate to the user-agent that we are prepared to do so. We do
this via the <i>:available-media-types</i> argument.
<pre><code>(<span class="clojure symbol">compojure/ANY</span> <span class="clojure string">"/products/:sku"</span> [sku]
(<span class="clojure symbol">liberator/resource</span>
<span class="clojure keyword">:handle-not-found</span> (<span class="clojure symbol">fn</span> [ctx] (<span class="clojure symbol">str</span> <span class="clojure string">"No such product: "</span> sku))
<span class="code highlight"><span class="clojure keyword">:available-media-types</span> [<span class="clojure string">"text/html"</span> <span class="clojure string">"text/plain;q=0.8"</span>]</span>①))</code></pre>
<dl class=callouts>
<dt>①
<dd>This vector contains the mime-types of the
<em>representations</em> that we are able to support, HTML
(<code>text/html</code>) and plain-text
(<code>text/plain</code>). Each mime-type can be given an
optional <em>q-value</em> which indicates which
representation the resource would <i>prefer</i> to
generate. Note that the user-agent's preferred
representation (indicated by the Accept header) always
takes priority over this preference. The highest <em>q-value</em> wins and a
missing <em>q-value</em> implies a value of 1.
</dl>
</section>
<span class="anchor" id="flowchart"></span>
<section>
<h2>Understanding the flowchart</h2>
<p>Decisions are made according to a flowchart which determines
the order in which decisions and places where the path splits into
two.
<p>The flowchart always ends at a handler (at the bottom of the
chart) which determines the HTTP status code in the response.
<p>On deciding how to implement the behaviour you have designed
for your resource you will often have this diagram in front of you
to decide which functions to replace.
<p>
<figure>
<figcaption>The Liberator flowchart (if you don't see anything below, please <a href="http://philipp.meier.name/t/liberator-flow-color.svg" target="_blank">open</a> it)</figcaption>
<a href="http://philipp.meier.name/t/liberator-flow-color.svg" target="_blank">
<object width="80%" height="80%" data="http://philipp.meier.name/t/liberator-flow-color.svg"></object>
</a>
</figure>
</p>
<p><small>Our diagrams are in SVG format. If your browser cannot
display SVG, there is also
a <a href="http://philipp.meier.name/t/liberator-flow-color.png">PNG</a>
version of this flowchart available.</small></p>
<p><small>Attribution: This diagram aligns with
the <a href="http://wiki.basho.com/Webmachine-Diagram.html">one
created by Alan Dean for webmachine</a>. We have made a few small changes to the naming of decisions (so that they can be represented by Clojure keys) and to include the side-effecting <tt>post!</tt>, <tt>put!</tt> and <tt>delete!</tt> actions.</small>
</section>
<span class="anchor" id="functions"></span>
<section>
<h2>Replacing functions</h2>
<h3>The context argument</h3>
<p>The context argument is provided as a single argument to all
replacement functions. It is a map of 5 entries.
<table id=context-arg dir=ltr border=1>
<style>
#context-arg tr td:first-child { text-align: left; padding-top: 5pt; font-weight: normal }
#context-arg td ul { list-style-type: none }
#context-arg td { padding-left: 10pt }
</style>
<colgroup width=30% />
<colgroup width=1* style="background:#ddd;" />
<tbody>
<tr>
<td>:status
<td>The response status code
</tr>
<tr>
<td>:message
<td>The default status message that usually accompanies the status code
</tr>
<tr>
<td>:request
<td>The original Ring request
</tr>
<tr>
<td>:resource
<td>The resource map that defines the Liberator resource
</tr>
<tr>
<td>:representation
<td>This is a map containing the results of content negotiation. It contains 4 sub-keys :-
<ul>
<li>:media-type
<li>:language
<li>:charset
<li>:encoding
</ul>
</td>
</tr>
</tbody>
</table>
<h3>The return value</h3>
<p>For decision functions you usually return a boolean. However,
sometimes you want to store some state in the context to pass to
the next decisions and ultimately to the handler.
<p>
If you return a map, this will get merged into the context given
to future functions. Returning a map implies a true result, so
if you want to return a false result but also specify a map you
can do this by returning a vector pair containing the map,
ie. [false {}]
<p>For handler functions you have more flexibility. If you return
a map, it will be treated as a Ring response map. Anything else
will be treated as the :body value of a Ring reponse map, and the
rest of the details will be added by Liberator. It is possible in
this way to override the status code if necessary. Only do this if
you know what you're doing, but if you're sure Liberator won't
stand in your way.
<h3>Function reference</h3>
<script>
toggle_advanced = function(b) {
if (b.checked) disp = "none"; else disp = "table-row";
Array.prototype.forEach.call(document.querySelectorAll("#overrides .advanced"),
function(e) { e.setAttribute("style", "display: " + disp); });
}
</script>
<input checked name="adv" type="checkbox" onclick="toggle_advanced(this)">Hide advanced functions</input>
<table id=overrides dir=ltr border=1 >
<style>
#overrides tr td:first-child { text-align: left; padding-top: 5pt; font-weight: bold; background: black }
#overrides tr th { font-family: Oregano, cursive }
#overrides td ul { list-style-type: none }
#overrides td { padding-left: 10pt }
#overrides td.decision { color: orange }
#overrides td.handler { color: green }
#overrides td.action { color: red }
#overrides td.declaration { color: grey }
#overrides tr.advanced { display: none }
</style>
<colgroup width=30% />
<colgroup width=20% />
<colgroup width=50% />
<thead>
<tr><th>Key<th>Type<th>Purpose</tr>
</thead>
<tbody>
<tr><td class="decision">:allowed?<td>decision<td>Whether the user is allowed access to the resource. Returning false will cause a 403 Forbidden.</tr>
<tr><td class="decision">:authorized?<td>decision<td></tr>
<tr><td class="declaration">:available-charsets<td>declaration<td></tr>
<tr><td class="declaration">:available-encodings<td>declaration<td></tr>
<tr><td class="declaration">:available-languages<td>declaration<td></tr>
<tr><td class="declaration">:available-media-types<td>declaration<td></tr>
<tr class="advanced"><td class="decision">:can-post-to-missing?<td>decision<td></tr>
<tr class="advanced"><td class="decision">:charset-available?<td>decision<td></tr>
<tr class="advanced"><td class="decision">:conflict?<td>decision<td></tr>
<tr><td class="action">:delete!<td>action<td></tr>
<tr class="advanced"><td class="decision">:encoding-available?<td>decision<td></tr>
<tr class="advanced"><td class="declaration">:etag<td>declaration<td></tr>
<tr class="advanced"><td class="decision">:existed?<td>decision<td></tr>
<tr class="advanced"><td class="decision">:exists?<td>decision<td></tr>
<tr class="advanced"><td class="handler">:handle-created<td>handler<td></tr>
<tr class="advanced"><td class="handler">:handle-gone<td>handler<td></tr>
<tr class="advanced"><td class="handler">:handle-malformed<td>handler<td></tr>
<tr class="advanced"><td class="handler">:handle-method-not-allowed<td>handler<td></tr>
<tr class="advanced"><td class="handler">:handle-multiple-representations<td>handler<td></tr>
<tr class="advanced"><td class="handler">:handle-no-content<td>handler<td></tr>
<tr><td class="handler">:handle-not-acceptable<td>handler<td></tr>
<tr class="advanced"><td class="handler">:handle-not-found<td>handler<td></tr>
<tr class="advanced"><td class="handler">:handle-not-implemented<td>handler<td></tr>
<tr class="advanced"><td class="handler">:handle-not-modified<td>handler<td></tr>
<tr><td class="handler">:handle-ok<td>handler<td></tr>
<tr class="advanced"><td class="handler">:handle-precondition-failed<td>handler<td></tr>
<tr class="advanced"><td class="handler">:handle-request-entity-too-large<td>handler<td></tr>
<tr class="advanced"><td class="handler">:handle-service-not-available<td>handler<td></tr>
<tr><td class="handler">:handle-unsupported-media-type<td>handler<td></tr>
<tr class="advanced"><td class="handler">:handle-uri-too-long<td>handler<td></tr>
<tr class="advanced"><td class="decision">:known-content-type?<td>decision<td></tr>
<tr class="advanced"><td class="decision" >:known-method?<td>decision<td></tr>
<tr class="advanced"><td class="decision">:language-available?<td>decision<td></tr>
<tr class="advanced"><td class="decision">:malformed?<td>decision<td></tr>
<tr class="advanced"><td class="decision">:method-allowed?<td>decision<td></tr>
<tr class="advanced"><td class="decision">:multiple-representations?<td>decision<td></tr>
<tr class="advanced"><td class="decision">:new?<td>decision<td></tr>
<tr><td class="action">:post!<td>action<td></tr>
<tr><td class="decision">:post-redirect?<td>decision<td></tr>
<tr class="advanced"><td class="decision">:post-to-existing?<td>decision<td></tr>
<tr><td class="action">:put!<td>action<td></tr>
<tr class="advanced"><td class="decision">:put-to-different-url?<td>decision<td></tr>
<tr class="advanced"><td class="decision">:respond-with-entity?<td>decision<td></tr>
<tr><td class="declaration">:see-other<td>declaration<td></tr>
<tr><td class="decision">:service-available?<td>decision<br>(Defaults to true)<td>Return false if, for some reason, you are no longer able to fulfill any requests, perhaps due to capacity issues. (You've just appeared on Hacker News!)</tr>
<tr><td class="handler">:unauthorized<td>handler<td></tr>
<tr class="advanced"><td class="handler">:unknown-method<td>handler<td></tr>
<tr class="advanced"><td class="decision">:uri-too-long?<td>decision<td></tr>
<tr class="advanced"><td class="decision">:valid-content-header?<td>decision<td></tr>
<tr class="advanced"><td class="decision">:valid-entity-length?<td>decision<td></tr>
</table>
</section>
<span class="anchor" id="getting-started"></span>
<section>
<h2>Getting started</h2>
<p>For this bit, please use <a href="http://leiningen.org">leiningen 2</a>.</p>
<p>Create a new project
<pre>> <kbd>lein new myproj</kbd></pre>
<p>Edit the <code>project.clj</code> file so that the <code>liberator</code> dependency is added.
<pre><code>(<span class="clojure symbol">defproject</span> myproj <span class="clojure string">"0.0.1"</span>
<span class="clojure keyword">:dependencies</span> [...
[liberator <span class="clojure string">"0.8.0"</span>]])</code></pre>
<p>Start the REPL
<pre>> <kbd>lein repl</kbd></pre>
<p>Import liberator
<pre>REPL> <kbd>(use 'liberator.core)</kbd></pre>
<h3>Running the examples</h3>
<p>A set of examples is included. If you want to see the examples in a browser, run
<pre>> <kbd>lein examples</kbd></pre>
<p>This will start a web server on port 8000 (but you can specify a alternative port with an argument, eg. <kbd>lein examples 8001</kbd>). Alternatively you can run the web server with <kbd>lein ring server</kbd>).
<h3>Swank users</h3>
<p>If you code in Emacs (with Slime) and want to use swank, ensure
you have the lein swank plugin installed. For example, ensure you have included the lein-swank plugin in your <tt>$HOME/.lein/profiles.clj</tt> file :-
<pre>
{<span class="clojure keyword">:user</span> {<span class="clojure keyword">:plugins</span> [[<span class="clojure symbol">lein-swank</span> <span class="clojure string">"1.4.4"</span>]]}}
</pre>
<p>Now you can use leiningen to start the swank server :-
<pre>> <kbd>lein swank</kbd></pre>
<p>In Emacs, execute <tt>M-x slime-connect</tt> and wait for the REPL to start. To start the examples server, type the following commands into the Emacs REPL :-
<pre>
user> <kbd>(require 'examples.server)</kbd>
user> <kbd>(examples.server/-main)</kbd></pre>
<p>Now open <tt>examples/clj/examples.clj</tt> and add a new route to the top of the <tt>assemble-routes</tt> function. Re-eval the buffer and test in the browser. Happy hacking!</p>
<pre>
(<span class="clojure symbol">defn</span> assemble-routes []
(<span class="clojure symbol">routes</span>
<span class="code highlight">(<span class="clojure symbol">ANY</span> <span class="clojure string">"/my-example"</span> [] (<span class="clojure symbol">resource</span> <span class="clojure keyword">:handle-ok</span> <span class="clojure string">"My example..."</span>))</span>
(<span class="clojure symbol">ANY</span> <span class="clojure string">"/hello-world"</span> [] <span class="clojure symbol">hello-world</span>)
...
))</pre>