forked from g3d/espresso.github.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Setup.html
833 lines (621 loc) · 21.8 KB
/
Setup.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Espresso Framework</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="javascripts/jquery.js" type="text/javascript"></script>
<link href="javascripts/bootstrap/css/bootstrap.min.css" rel="stylesheet" />
<link href="javascripts/bootstrap/css/bootstrap-responsive.min.css" rel="stylesheet" />
<script src="javascripts/bootstrap/js/bootstrap.min.js" type="text/javascript"></script>
<link href="javascripts/google-code-prettify/prettify.css" type="text/css" rel="stylesheet" />
<script src="javascripts/google-code-prettify/prettify.js" type="text/javascript"></script>
<link href="stylesheets/master.css" type="text/css" rel="stylesheet" />
</head>
<body>
<div class="container">
<div class="row">
<div class="span2">
<a href="index.html">
<img src="images/logo.png" class="pull-right logo" /></a>
</div>
<div class="span6">
<h1>
<a href="index.html">
Espresso Framework</a>
</h1>
<h4>
Scalable Web Framework aimed at Speed and Simplicity
</h4>
</div>
<div class="span pull-right">
<div class="row">
<div class="span">
<h4>
<a href="presentation.html">
Official Presentation</a>
</h4>
<h4 style="white-space: nowrap;">
<a href="Events.html">
Events</a> |
<a href="CHANGELOG.html">
Change Log</a>
</h4>
</div>
</div>
</div>
</div>
<hr/>
<div class="row" style="text-align: center;">
<div class="btn-toolbar">
<div class="btn-group" style="text-align: left; margin: 5px;">
<a href="Intro.html" class="btn">
Intro</a>
<button class="btn dropdown-toggle" data-toggle="dropdown">
<span class="caret"></span>
</button>
<ul class="dropdown-menu">
<li>
<a href="Intro.html#actions">
Actions</a>
</li>
<li>
<a href="Intro.html#controllers">
Controllers</a>
</li>
<li>
<a href="Intro.html#slices">
Slices</a>
</li>
<li>
<a href="Intro.html#mvc-">
MVC?</a>
</li>
<li>
<a href="Intro.html#models-">
Models?</a>
</li>
</ul>
</div>
<div class="btn-group" style="text-align: left; margin: 5px;">
<a href="Routing.html" class="btn">
Routing</a>
<button class="btn dropdown-toggle" data-toggle="dropdown">
<span class="caret"></span>
</button>
<ul class="dropdown-menu">
<li>
<a href="Routing.html#base-url">
Base URL</a>
</li>
<li>
<a href="Routing.html#canonicals">
Canonicals</a>
</li>
<li>
<a href="Routing.html#actions">
Actions</a>
</li>
<li>
<a href="Routing.html#action-mapping">
Action Mapping</a>
</li>
<li>
<a href="Routing.html#action-aliases">
Action Aliases</a>
</li>
<li>
<a href="Routing.html#parametrization">
Parametrization</a>
</li>
<li>
<a href="Routing.html#format">
Format</a>
</li>
<li>
<a href="Routing.html#restful-actions">
RESTful Actions</a>
</li>
<li>
<a href="Routing.html#rewriter">
Rewriter</a>
</li>
</ul>
</div>
<div class="btn-group" style="text-align: left; margin: 5px;">
<a href="Setup.html" class="btn">
Setup</a>
<button class="btn dropdown-toggle" data-toggle="dropdown">
<span class="caret"></span>
</button>
<ul class="dropdown-menu">
<li>
<a href="Setup.html#global-setup">
Global Setup</a>
</li>
<li>
<a href="Setup.html#setup-by-name">
Setup by Name</a>
</li>
<li>
<a href="Setup.html#setup-by-format">
Setup by Format</a>
</li>
<li>
<a href="Setup.html#remote-setup">
Remote Setup</a>
</li>
</ul>
</div>
<div class="btn-group" style="text-align: left; margin: 5px;">
<a href="Workflow.html" class="btn">
Workflow</a>
<button class="btn dropdown-toggle" data-toggle="dropdown">
<span class="caret"></span>
</button>
<ul class="dropdown-menu">
<li>
<a href="Workflow.html#route">
Route</a>
</li>
<li>
<a href="Workflow.html#params">
Params</a>
</li>
<li>
<a href="Workflow.html#passing-control">
Passing Control</a>
</li>
<li>
<a href="Workflow.html#fetching-body">
Fetching Body</a>
</li>
<li>
<a href="Workflow.html#halt">
Halt</a>
</li>
<li>
<a href="Workflow.html#redirect">
Redirect</a>
</li>
<li>
<a href="Workflow.html#reload">
Reload</a>
</li>
<li>
<a href="Workflow.html#error-handlers">
Error Handlers</a>
</li>
<li>
<a href="Workflow.html#hooks">
Hooks</a>
</li>
<li>
<a href="Workflow.html#authorization">
Authorization</a>
</li>
<li>
<a href="Workflow.html#sessions">
Sessions</a>
</li>
<li>
<a href="Workflow.html#flash">
Flash</a>
</li>
<li>
<a href="Workflow.html#cookies">
Cookies</a>
</li>
<li>
<a href="Workflow.html#content-type">
Content Type</a>
</li>
<li>
<a href="Workflow.html#charset">
Charset</a>
</li>
<li>
<a href="Workflow.html#cache-control">
Cache Control</a>
</li>
<li>
<a href="Workflow.html#expires">
Expires</a>
</li>
<li>
<a href="Workflow.html#last-modified">
Last Modified</a>
</li>
<li>
<a href="Workflow.html#accepted-content-type">
Accepted Content Type</a>
</li>
<li>
<a href="Workflow.html#cache-manager">
Cache Manager</a>
</li>
<li>
<a href="Workflow.html#send-file">
Send File</a>
</li>
<li>
<a href="Workflow.html#send-files">
Send Files</a>
</li>
<li>
<a href="Workflow.html#attachment">
Attachment</a>
</li>
<li>
<a href="Workflow.html#headers">
Headers</a>
</li>
</ul>
</div>
<div class="btn-group" style="text-align: left; margin: 5px;">
<a href="ViewAPI.html" class="btn">
View API</a>
<button class="btn dropdown-toggle" data-toggle="dropdown">
<span class="caret"></span>
</button>
<ul class="dropdown-menu">
<li>
<a href="ViewAPI.html#engine">
Engine</a>
</li>
<li>
<a href="ViewAPI.html#extension">
Extension</a>
</li>
<li>
<a href="ViewAPI.html#templates-path">
Templates Path</a>
</li>
<li>
<a href="ViewAPI.html#layouts-path">
Layouts Path</a>
</li>
<li>
<a href="ViewAPI.html#layout">
Layout</a>
</li>
<li>
<a href="ViewAPI.html#rendering-templates">
Rendering Templates</a>
</li>
<li>
<a href="ViewAPI.html#rendering-layouts">
Rendering Layouts</a>
</li>
<li>
<a href="ViewAPI.html#ad-hoc-rendering">
Ad hoc Rendering</a>
</li>
<li>
<a href="ViewAPI.html#path-resolver">
Path Resolver</a>
</li>
<li>
<a href="ViewAPI.html#templates-compilation">
Templates Compilation</a>
</li>
</ul>
</div>
<div class="btn-group" style="text-align: left; margin: 5px;">
<a href="Streaming.html" class="btn">
Streaming</a>
<button class="btn dropdown-toggle" data-toggle="dropdown">
<span class="caret"></span>
</button>
<ul class="dropdown-menu">
<li>
<a href="Streaming.html#server-sent-events">
Server-Sent Events</a>
</li>
<li>
<a href="Streaming.html#websockets">
WebSockets</a>
</li>
<li>
<a href="Streaming.html#chunked-responses">
Chunked Responses</a>
</li>
</ul>
</div>
<div class="btn-group" style="text-align: left; margin: 5px;">
<a href="Assets.html" class="btn">
Assets</a>
<button class="btn dropdown-toggle" data-toggle="dropdown">
<span class="caret"></span>
</button>
<ul class="dropdown-menu">
<li>
<a href="Assets.html#tag-helpers">
Tag Helpers</a>
</li>
<li>
<a href="Assets.html#assets-mapper">
Assets Mapper</a>
</li>
</ul>
</div>
<div class="btn-group" style="text-align: left; margin: 5px;">
<a href="CRUD.html" class="btn">
CRUD</a>
<button class="btn dropdown-toggle" data-toggle="dropdown">
<span class="caret"></span>
</button>
<ul class="dropdown-menu">
<li>
<a href="CRUD.html#intro">
Intro</a>
</li>
<li>
<a href="CRUD.html#resource">
Resource</a>
</li>
<li>
<a href="CRUD.html#excluded-params">
Excluded Params</a>
</li>
<li>
<a href="CRUD.html#root">
Root</a>
</li>
<li>
<a href="CRUD.html#response">
Response</a>
</li>
<li>
<a href="CRUD.html#error-handler">
Error Handler</a>
</li>
<li>
<a href="CRUD.html#access-restriction">
Access Restriction</a>
</li>
</ul>
</div>
<div class="btn-group" style="text-align: left; margin: 5px;">
<a href="Deploy.html" class="btn">
Deploy</a>
<button class="btn dropdown-toggle" data-toggle="dropdown">
<span class="caret"></span>
</button>
<ul class="dropdown-menu">
<li>
<a href="Deploy.html#controllers">
Controllers</a>
</li>
<li>
<a href="Deploy.html#slices">
Slices</a>
</li>
<li>
<a href="Deploy.html#roots">
Roots</a>
</li>
<li>
<a href="Deploy.html#run">
Run</a>
</li>
<li>
<a href="Deploy.html#config-ru">
config.ru</a>
</li>
</ul>
</div>
</div>
</div>
<hr/>
<h2 id='global-setup'><a href='#global-setup'>Global Setup</a></h2>
<p>The main idea behind actions setup is to easily manipulate the action's behavior
without touching the actions at all.</p>
<p>That's allow to "remotely" control any number of actions with just few lines of code
and manipulate their behavior without actions refactoring.</p>
<p>To illustrate an example, let's suppose that all actions should return "UTF-8" charset.</p>
<pre>
class App < E
charset 'UTF-8'
# ...
end
</pre>
<p>Now you can define any number of actions without bothering to add the charset inside each one.</p>
<p>And when you need to change the charset returned by your actions
simply change a single line of code at class level.</p>
<p><strong>[ <a href="#">contents ↑</a> ]</strong></p>
<h2 id='setup-by-name'><a href='#setup-by-name'>Setup by Name</a></h2>
<p>Global setup is just good for mostly trivial apps.</p>
<p>But when it comes to develop more compelling apps, we need more control.</p>
<p>And Espresso kindly offering it.</p>
<p><strong># 1</strong> - Actions can be configured by name</p>
<p><strong>Example:</strong> - all actions, but <code>:api</code> and <code>:json</code>, should return "text/plain" content type.
<code>:api</code> and <code>:json</code> instead should return "application/json"</p>
<pre>
class App < E
content_type '.txt'
setup :api, :json do
content_type '.json'
end
# ...
end
</pre>
<p><strong># 2</strong> - Actions can also be configured by regular expressions</p>
<p><strong>Example:</strong> - all actions containing "<em>js</em>" will return "application/javascript" content type</p>
<pre>
class App < E
setup /_js_/ do
content_type '.js'
end
# ...
end
</pre>
<h2 id='setup-by-format'><a href='#setup-by-format'>Setup by Format</a></h2>
<p><a href="https://github.com/espresso/espresso/blob/master/docs/Routing.md#format">Format</a> is a part of routing mechanism
but it is also hugely used when it comes to setup actions.</p>
<p>It turns out that setting up actions by name and regular expressions are not enough.</p>
<p>We need even more control.</p>
<p>Cause "/book.html" and "/book.xml" definitely may behave differently, even if they are backed by same action.</p>
<p>How for ex. to tell <code>book</code> action to return some charset on "/book.html" URL
and another one on "/book.xml" URL without touching the action itself?</p>
<p>Here is where format comes in action.</p>
<p><strong># 1</strong> - To add some setup only for specific format, provide action name as a string, suffixed by desired format.</p>
<p><strong>Example:</strong> - make /book.xml to use <code>Nokogiri</code> engine and /book.html <code>Slim</code> engine</p>
<pre>
class App < E
format '.xml', '.html'
setup 'book.xml' do
engine :Nokogiri
end
setup 'book.html' do
engine :Slim
end
# or
setup :book do
engine :Nokogiri if xml?
engine :Slim if html?
end
def book
# ...
end
end
</pre>
<p>That's great, but useless when we have N actions to setup, cause we will have to define a setup for each action.</p>
<p>That's weird.</p>
<p>And here is where the format comes in action for the second time.</p>
<p><strong># 2</strong> - To setup all actions that respond to some format, simply provide format without action name.</p>
<p><strong>Example:</strong> - use different Cache-Control header for different formats</p>
<pre>
class App < E
format '.html', '.xml'
setup '.html' do
cache_control :public, :must_revalidate, :max_age => 600
end
setup '.xml' do
cache_control :private, :must_revalidate, :max_age => 60
end
# ...
end
</pre>
<p>It is also possible to use format helpers to determine current format.</p>
<p><strong>Example:</strong> - set engine for <code>:book</code> action depending on format:</p>
<pre>
class App < E
format '.html', '.xml' # this will generate `html?` and `xml?` helpers
setup :book do
if html?
engine :Slim
end
if xml?
engine :Nokogiri
end
# Disclaimer: this coding style are used just for docs readability
end
end
</pre>
<p><strong>[ <a href="#">contents ↑</a> ]</strong></p>
<h2 id='remote-setup'><a href='#remote-setup'>Remote Setup</a></h2>
<p>Any Espresso controller or slice can be packed as a gem and then installed on any server.<br/>
Very useful when you need distributed apps.</p>
<p>And it becomes even more useful with Remote Setup.</p>
<p>Say you need the same app to run some setup on Server A and another setup on Server B,
without touching/refactor the app at all.</p>
<p>Easy!</p>
<p>Let's say we have a Forum app that serves /Forums base URL and returns content of ISO-8859-1 charset.</p>
<p>That's ok for Server A.</p>
<p>But for Server B we need it to serve /forum base URL and return UTF-8 charset.</p>
<p><strong>Example:</strong> - deploying Forum app on Server B</p>
<pre>
require 'my-mega-forum-gem'
run Forum.mount '/forum' {
charset 'UTF-8'
}
</pre>
<p>That's it.</p>
<p>However, passing setup proc at mount is not the only way to setup controllers.</p>
<p>You can setup them all at once by using <code>app#global_setup</code> method.</p>
<pre>
module App
class Pages < E
# ...
end
class News < E
# ...
end
class Articles < E
# ...
end
end
app = EApp.new
app.global_setup do
# here setup will run inside all controllers
end
app.mount App
app.run
</pre>
<p>Or you can setup controllers selectively - controller name are passed as first argument:</p>
<pre>
app = EApp.new
app.setup do |ctrl|
if ctrl == App::Pages
# here setup will run ONLY inside Pages controller
end
# here setup will run inside all controllers
end
app.mount App
app.run
</pre>
<p>Please note that <strong>#mount should always go after #global_setup</strong>,
otherwise global setup will have no effect.</p>
<p><strong>[ <a href="#">contents ↑</a> ]</strong></p>
<hr/>
<footer class="footer well">
<div class="row">
<div class="span4">
<p>
License: MIT
</p>
<p>
Author: Silviu Rusu
</p>
<p>
Source Code:
<a href="https://github.com/espresso/espresso">
https://github.com/espresso/espresso</a>
</p>
</div>
<div class="span6 pull-right">
<p>
Issues/Bugs:
<a href="https://github.com/espresso/espresso/issues">
https://github.com/espresso/espresso/issues</a>
</p>
<p>
<strong>Contributors are very Welcome!</strong>
</p>
<p>
IRC channel: #espressorb on irc.freenode.net
</p>
</div>
</div>
</footer>
</div>
<a href="https://github.com/espresso/espresso"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_gray_6d6d6d.png" alt="Fork me on GitHub"></a>
<script type="text/javascript">
$(function() {
$('pre').addClass('prettyprint lang-ruby');
prettyPrint();
});
</script>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-27879048-5']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
</body>
</html>