aboutsummaryrefslogtreecommitdiffstats
path: root/railties/doc/guides/html/rails_on_rack.html
blob: a78d1697b013ae84e19f430b1132dba0565a7314 (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
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
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	<title>Rails on Rack</title>
	<!--[if lt IE 8]>
	<script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type="text/javascript"></script>
	<![endif]-->
	<link href="stylesheets/base.css" media="screen" rel="Stylesheet" type="text/css" />
	<link href="stylesheets/forms.css" media="screen" rel="Stylesheet" type="text/css" />
	<link href="stylesheets/more.css" media="screen" rel="Stylesheet" type="text/css" />
	<style type="text/css">
		div#container {
	max-width: 900px;
	padding-bottom: 3em;
}

div#content {
	margin-left: 200px;
}

div#container.notoc {
	max-width: 600px;
}

.notoc div#content {
	margin-left: 0;
}

pre {
	line-height: 1.4em;
}

#content p tt {
	background: #eeeeee;
	border: solid 1px #cccccc;
	padding: 3px;
}

dt {
	font-weight: bold;
}

#content dt tt {
	font-size: 10pt;
}

dd {
	margin-left: 3em;
}

#content dt tt, #content pre tt {
	background: none;
	padding: 0;
	border: 0;
}

#content .olist ol {
	margin-left: 2em;
}

#header {
	position: relative;
	max-width: 840px;
	margin-left: auto;
	margin-right: auto;
}

#header.notoc {
	max-width: 580px;
}

#logo {
	position: absolute;
	left: 10px;
	top: 10px;
	width: 110px;
	height: 140px;
}

div#header h1#site_title {
	background: url('images/ruby_on_rails_by_mike_rundle2.gif') top left no-repeat;
	position: absolute;
	width: 392px;
	height: 55px;
	left: 145px;
	top: 20px;
	margin: 0;
	padding: 0;
}

#site_title span {
	display: none;
}

#site_title_tagline {
	display: none;
}

ul#navMain {
	position: absolute;
	margin: 0;
	padding: 0;
	top: 97px;
	left: 145px;
}

.left-floaty, .right-floaty {
	padding: 15px;
}

.admonitionblock,
.tableblock {
	margin-left: 1em;
	margin-right: 1em;
	margin-top: 0.25em;
	margin-bottom: 1em;
}

.admonitionblock .icon {
	padding-right: 8px;
}

.admonitionblock .content {
	border: solid 1px #ffda78;
	background: #fffebd;
	padding: 10px;
	padding-top: 8px;
	padding-bottom: 8px;
}

.admonitionblock .title {
	font-size: 140%;
	margin-bottom: 0.5em;
}

.tableblock table {
	border: solid 1px #aaaaff;
	background: #f0f0ff;
}

.tableblock th {
	background: #e0e0e0;
}

.tableblock th,
.tableblock td {
	padding: 3px;
	padding-left: 5px;
	padding-right: 5px;
}

.sidebarblock {
	margin-top: 0.25em;
	margin: 1em;
	border: solid 1px #ccccbb;
	padding: 8px;
	background: #ffffe0;
}

.sidebarblock .sidebar-title {
	font-size: 140%;
	font-weight: 600;
	margin-bottom: 0.3em;
}

.sidebarblock .sidebar-content > .para:last-child > p {
	margin-bottom: 0;
}

.sidebarblock .sidebar-title a {
	text-decoration: none;
}

.sidebarblock .sidebar-title a:hover {
	text-decoration: underline;
}

	</style>
</head>
<body>
	<div id="header" >
		<div id="logo">
			<a href="index.html" title="Ruby on Rails"><img src="images/rails_logo_remix.gif" alt="Rails" height="140" width="110" /></a>
		</div>
		
		<h1 id="site_title"><span>Ruby on Rails</span></h1>
		<h2 id="site_title_tagline">Sustainable productivity for web-application development</h2>

		<ul id="navMain">
			<li class="first-child"><a href="http://www.rubyonrails.org/" title="Ruby on Rails" class="ruby_on_rails">Ruby on Rails</a></li>
			<li><a class="manuals" href="index.html" title="Manuals Index">Guides Index</a></li>
		</ul>
	</div>

	<div id="container">
		
		<div id="sidebar">
			<h2>Chapters</h2>
			<ol>
					<li>
					<a href="#_introduction_to_rack">Introduction to Rack</a>
					</li>
					<li>
					<a href="#_rails_on_rack">Rails on Rack</a>
						<ul>
						
							<li><a href="#_actioncontroller_dispatcher_new">ActionController::Dispatcher.new</a></li>
						
							<li><a href="#_script_server">script/server</a></li>
						
							<li><a href="#_rackup">rackup</a></li>
						
						</ul>
					</li>
					<li>
					<a href="#_action_controller_middleware_stack">Action Controller Middleware Stack</a>
						<ul>
						
							<li><a href="#_adding_middlewares">Adding Middlewares</a></li>
						
							<li><a href="#_internal_middleware_stack">Internal Middleware Stack</a></li>
						
							<li><a href="#_customizing_internal_middleware_stack">Customizing Internal Middleware Stack</a></li>
						
							<li><a href="#_inspecting_middleware_stack">Inspecting Middleware Stack</a></li>
						
						</ul>
					</li>
					<li>
					<a href="#_rails_metal_applications">Rails Metal Applications</a>
						<ul>
						
							<li><a href="#_generating_a_metal_application">Generating a Metal Application</a></li>
						
							<li><a href="#_execution_order">Execution Order</a></li>
						
						</ul>
					</li>
					<li>
					<a href="#_middlewares_and_rails">Middlewares and Rails</a>
					</li>
					<li>
					<a href="#_changelog">Changelog</a>
					</li>
			</ol>
		</div>
		
		<div id="content">
				<h1>Rails on Rack</h1>
			<div id="preamble">
<div class="sectionbody">
<div class="paragraph"><p>This guide covers Rails integration with Rack and interfacing with other Rack components. By referring to this guide, you will be able to:</p></div>
<div class="ulist"><ul>
<li>
<p>
Create Rails Metal applications
</p>
</li>
<li>
<p>
Use Rack Middlewares in your Rails applications
</p>
</li>
<li>
<p>
Understand Action Pack&#8217;s internal Middleware stack
</p>
</li>
<li>
<p>
Define custom internal Middleware stack
</p>
</li>
<li>
<p>
Understand the best practices for developing a middleware aimed at Rails applications
</p>
</li>
</ul></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
<img src="./images/icons/note.png" alt="Note" />
</td>
<td class="content">This guide assumes a working knowledge of Rack protocol and Rack concepts such as middlewares, url maps and Rack::Builder.</td>
</tr></table>
</div>
</div>
</div>
<h2 id="_introduction_to_rack">1. Introduction to Rack</h2>
<div class="sectionbody">
<div class="sidebarblock">
<div class="sidebar-content">
<div class="paragraph"><p>Rack provides a minimal, modular and adaptable interface for developing web applications in Ruby. By wrapping HTTP requests and responses in the simplest way possible, it unifies and distills the API for web servers, web frameworks, and software in between (the so-called middleware) into a single method call.</p></div>
<div class="ulist"><ul>
<li>
<p>
<a href="http://rack.rubyforge.org/doc">Rack API Documentation</a>
</p>
</li>
</ul></div>
</div></div>
<div class="paragraph"><p>Explaining Rack is not really in the scope of this guide. In case you are not familiar with Rack&#8217;s basics, you should check out the following links:</p></div>
<div class="ulist"><ul>
<li>
<p>
<a href="http://rack.github.com">Official Rack Website</a>
</p>
</li>
<li>
<p>
<a href="http://chneukirchen.org/blog/archive/2007/02/introducing-rack.html">Introducing Rack</a>
</p>
</li>
<li>
<p>
<a href="http://m.onkey.org/2008/11/17/ruby-on-rack-1">Ruby on Rack #1 - Hello Rack!</a>
</p>
</li>
<li>
<p>
<a href="http://m.onkey.org/2008/11/18/ruby-on-rack-2-rack-builder">Ruby on Rack #2 - The Builder</a>
</p>
</li>
</ul></div>
</div>
<h2 id="_rails_on_rack">2. Rails on Rack</h2>
<div class="sectionbody">
<h3 id="_actioncontroller_dispatcher_new">2.1. ActionController::Dispatcher.new</h3>
<div class="paragraph"><p><tt>ActionController::Dispatcher.new</tt> is the primary Rack application object of a Rails application. It responds to <tt>call</tt> method with a single <tt>env</tt> argument and returns a Rack response. Any Rack compliant web server should be using <tt>ActionController::Dispatcher.new</tt> object to serve a Rails application.</p></div>
<h3 id="_script_server">2.2. script/server</h3>
<div class="paragraph"><p><tt>script/server</tt> does the basic job of creating a <tt>Rack::Builder</tt> object and starting the webserver. This is Rails equivalent of Rack&#8217;s <tt>rackup</tt> script.</p></div>
<div class="paragraph"><p>Here&#8217;s how <tt>script/server</tt> creates an instance of <tt>Rack::Builder</tt></p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>app <span style="color: #990000">=</span> Rack<span style="color: #990000">::</span>Builder<span style="color: #990000">.</span>new <span style="color: #FF0000">{</span>
  use Rails<span style="color: #990000">::</span>Rack<span style="color: #990000">::</span>LogTailer <span style="font-weight: bold"><span style="color: #0000FF">unless</span></span> options<span style="color: #990000">[:</span>detach<span style="color: #990000">]</span>
  use Rails<span style="color: #990000">::</span>Rack<span style="color: #990000">::</span>Static
  use Rails<span style="color: #990000">::</span>Rack<span style="color: #990000">::</span>Debugger <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> options<span style="color: #990000">[:</span>debugger<span style="color: #990000">]</span>
  run ActionController<span style="color: #990000">::</span>Dispatcher<span style="color: #990000">.</span>new
<span style="color: #FF0000">}</span><span style="color: #990000">.</span>to_app</tt></pre></div></div>
<div class="paragraph"><p>Middlewares used in the code above are most useful in development envrionment. The following table explains their usage:</p></div>
<div class="tableblock">
<table rules="all"
width="100%"
frame="border"
cellspacing="0" cellpadding="4">
<col width="50%" />
<col width="50%" />
<thead valign="top">
<tr>
<th align="left">Middleware </th>
<th align="left">Purpose</th>
</tr>
</thead>
<tbody valign="top">
<tr>
<td align="left"><p class="table">Rails::Rack::LogTailer</p></td>
<td align="left"><p class="table">Appends log file output to console</p></td>
</tr>
<tr>
<td align="left"><p class="table">Rails::Rack::Static</p></td>
<td align="left"><p class="table">Serves static files inside <tt>RAILS_ROOT/public</tt> directory</p></td>
</tr>
<tr>
<td align="left"><p class="table">Rails::Rack::Debugger</p></td>
<td align="left"><p class="table">Starts Debugger</p></td>
</tr>
</tbody>
</table>
</div>
<h3 id="_rackup">2.3. rackup</h3>
<div class="paragraph"><p>To use <tt>rackup</tt> instead of Rails' <tt>script/server</tt>, you can put the following inside <tt>config.ru</tt> of your Rails application&#8217;s root directory:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-style: italic"><span style="color: #9A1900"># RAILS_ROOT/config.ru</span></span>
<span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">"config/environment"</span>

use Rails<span style="color: #990000">::</span>Rack<span style="color: #990000">::</span>LogTailer
use Rails<span style="color: #990000">::</span>Rack<span style="color: #990000">::</span>Static
run ActionController<span style="color: #990000">::</span>Dispatcher<span style="color: #990000">.</span>new</tt></pre></div></div>
<div class="paragraph"><p>And start the server:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="color: #990000">[</span>lifo@null application<span style="color: #990000">]</span>$ rackup</tt></pre></div></div>
<div class="paragraph"><p>To find out more about different <tt>rackup</tt> options:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="color: #990000">[</span>lifo@null application<span style="color: #990000">]</span>$ rackup --help</tt></pre></div></div>
</div>
<h2 id="_action_controller_middleware_stack">3. Action Controller Middleware Stack</h2>
<div class="sectionbody">
<div class="paragraph"><p>Many of Action Controller&#8217;s internal components are implemented as Rack middlewares. <tt>ActionController::Dispatcher</tt> uses <tt>ActionController::MiddlewareStack</tt> to combine various internal and external middlewares to form a complete Rails Rack application.</p></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
<img src="./images/icons/note.png" alt="Note" />
</td>
<td class="content">
<div class="title">What is ActionController::MiddlewareStack ?</div><tt>ActionController::MiddlewareStack</tt> is Rails equivalent of <tt>Rack::Builder</tt>, but built for better flexibility and more features to meet Rails' requirements.</td>
</tr></table>
</div>
<h3 id="_adding_middlewares">3.1. Adding Middlewares</h3>
<div class="paragraph"><p>Rails provides a very simple configuration interface for adding generic Rack middlewares to a Rails applications.</p></div>
<div class="paragraph"><p>Here&#8217;s how you can add middlewares via <tt>environment.rb</tt></p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-style: italic"><span style="color: #9A1900"># environment.rb</span></span>

config<span style="color: #990000">.</span>middleware<span style="color: #990000">.</span>use Rack<span style="color: #990000">::</span>BounceFavicon</tt></pre></div></div>
<h3 id="_internal_middleware_stack">3.2. Internal Middleware Stack</h3>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>use <span style="color: #FF0000">"ActionController::Lock"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span><span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">=&gt;</span> lambda <span style="color: #FF0000">{</span>
  <span style="color: #990000">!</span>ActionController<span style="color: #990000">::</span>Base<span style="color: #990000">.</span>allow_concurrency
<span style="color: #FF0000">}</span>

use <span style="color: #FF0000">"ActionController::Failsafe"</span>

use <span style="color: #FF0000">"ActiveRecord::QueryCache"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span><span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">=&gt;</span> lambda <span style="color: #FF0000">{</span> <span style="font-weight: bold"><span style="color: #0000FF">defined</span></span><span style="color: #990000">?(</span>ActiveRecord<span style="color: #990000">)</span> <span style="color: #FF0000">}</span>

<span style="color: #990000">[</span><span style="color: #FF0000">"ActionController::Session::CookieStore"</span><span style="color: #990000">,</span>
 <span style="color: #FF0000">"ActionController::Session::MemCacheStore"</span><span style="color: #990000">,</span>
 <span style="color: #FF0000">"ActiveRecord::SessionStore"</span><span style="color: #990000">].</span>each <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>store<span style="color: #990000">|</span>
   use<span style="color: #990000">(</span>store<span style="color: #990000">,</span> ActionController<span style="color: #990000">::</span>Base<span style="color: #990000">.</span>session_options<span style="color: #990000">,</span>
      <span style="color: #990000">:</span><span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">=&gt;</span> lambda <span style="color: #FF0000">{</span>
        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> session_store <span style="color: #990000">=</span> ActionController<span style="color: #990000">::</span>Base<span style="color: #990000">.</span>session_store
          session_store<span style="color: #990000">.</span>name <span style="color: #990000">==</span> store
        <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
      <span style="color: #FF0000">}</span>
    <span style="color: #990000">)</span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>

use ActionController<span style="color: #990000">::</span>VerbPiggybacking</tt></pre></div></div>
<div class="tableblock">
<table rules="all"
width="100%"
frame="border"
cellspacing="0" cellpadding="4">
<col width="50%" />
<col width="50%" />
<thead valign="top">
<tr>
<th align="left">Middleware </th>
<th align="left">Purpose</th>
</tr>
</thead>
<tbody valign="top">
<tr>
<td align="left"><p class="table">ActionController::Lock</p></td>
<td align="left"><p class="table">Sets <tt>env["rack.multithread"]</tt> flag to <tt>true</tt> and wraps the application within a Mutex.</p></td>
</tr>
<tr>
<td align="left"><p class="table">ActionController::Failsafe</p></td>
<td align="left"><p class="table">Returns HTTP Status <tt>500</tt> to the client if an exception gets raised while dispatching.</p></td>
</tr>
<tr>
<td align="left"><p class="table">ActiveRecord::QueryCache</p></td>
<td align="left"><p class="table">Enable the Active Record query cache.</p></td>
</tr>
<tr>
<td align="left"><p class="table">ActionController::Session::CookieStore</p></td>
<td align="left"><p class="table">Uses the cookie based session store.</p></td>
</tr>
<tr>
<td align="left"><p class="table">ActionController::Session::MemCacheStore</p></td>
<td align="left"><p class="table">Uses the memcached based session store.</p></td>
</tr>
<tr>
<td align="left"><p class="table">ActiveRecord::SessionStore</p></td>
<td align="left"><p class="table">Uses the database based session store.</p></td>
</tr>
<tr>
<td align="left"><p class="table">ActionController::VerbPiggybacking</p></td>
<td align="left"><p class="table">Sets HTTP method based on <tt>_method</tt> parameter or <tt>env["HTTP_X_HTTP_METHOD_OVERRIDE"]</tt>.</p></td>
</tr>
</tbody>
</table>
</div>
<h3 id="_customizing_internal_middleware_stack">3.3. Customizing Internal Middleware Stack</h3>
<div class="paragraph"><p>VERIFY THIS WORKS. Just a code dump at the moment.</p></div>
<div class="paragraph"><p>Put the following in an initializer.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>ActionController<span style="color: #990000">::</span>Dispatcher<span style="color: #990000">.</span>middleware <span style="color: #990000">=</span> ActionController<span style="color: #990000">::</span>MiddlewareStack<span style="color: #990000">.</span>new <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>m<span style="color: #990000">|</span>
  m<span style="color: #990000">.</span>use ActionController<span style="color: #990000">::</span>Lock
  m<span style="color: #990000">.</span>use ActionController<span style="color: #990000">::</span>Failsafe
  m<span style="color: #990000">.</span>use ActiveRecord<span style="color: #990000">::</span>QueryCache
  m<span style="color: #990000">.</span>use ActionController<span style="color: #990000">::</span>Session<span style="color: #990000">::</span>CookieStore
  m<span style="color: #990000">.</span>use ActionController<span style="color: #990000">::</span>VerbPiggybacking
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
<div class="paragraph"><p>Josh says :</p></div>
<div class="sidebarblock">
<div class="sidebar-content">
<div class="paragraph"><p>3.3: I wouldn&#8217;t recommend this: custom internal stack
i&#8217;d recommend using config.middleware.use api
we still need a better api for swapping out existing middleware, etc
config.middleware.swap AC::Sessions, My::Sessoins
or something like that</p></div>
</div></div>
<h3 id="_inspecting_middleware_stack">3.4. Inspecting Middleware Stack</h3>
<div class="paragraph"><p>Rails has a handy rake task for inspecting the middleware stack in use:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>$ rake middleware</tt></pre></div></div>
<div class="paragraph"><p>For a freshly generated Rails application, this will produce:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>use ActionController<span style="color: #990000">::</span>Lock
use ActionController<span style="color: #990000">::</span>Failsafe
use ActiveRecord<span style="color: #990000">::</span>QueryCache
use ActionController<span style="color: #990000">::</span>Session<span style="color: #990000">::</span>CookieStore<span style="color: #990000">,</span> <span style="color: #FF0000">{</span><span style="color: #990000">:</span>secret<span style="color: #990000">=&gt;</span><span style="color: #FF0000">"aa5150a22c1a5f24112260c33ae2131a88d7539117bfdcd5696fb2be385b60c6da9f7d4ed0a67e3b8cc85cc4e653ba0111dd1f3f8999520f049e2262068c16a6"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>session_key<span style="color: #990000">=&gt;</span><span style="color: #FF0000">"_edge_session"</span><span style="color: #FF0000">}</span>
use Rails<span style="color: #990000">::</span>Rack<span style="color: #990000">::</span>Metal
use ActionController<span style="color: #990000">::</span>VerbPiggybacking
run ActionController<span style="color: #990000">::</span>Dispatcher<span style="color: #990000">.</span>new</tt></pre></div></div>
<div class="paragraph"><p>This rake task is very useful if the application requires highly customized Rack middleware setup.</p></div>
</div>
<h2 id="_rails_metal_applications">4. Rails Metal Applications</h2>
<div class="sectionbody">
<div class="paragraph"><p>Rails Metal applications are minimal Rack applications specially designed for integrating with a typical Rails application. As Rails Metal Applications skip all of the Action Controller stack, serving a request has no overhead from the Rails framework itself. This is especially useful for infrequent cases where the performance of the full stack Rails framework is an issue.</p></div>
<h3 id="_generating_a_metal_application">4.1. Generating a Metal Application</h3>
<div class="paragraph"><p>Rails provides a generator called <tt>performance_test</tt> for creating new performance tests:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>script/generate metal poller</tt></pre></div></div>
<div class="paragraph"><p>This generates <tt>poller.rb</tt> in the <tt>app/metal</tt> directory:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-style: italic"><span style="color: #9A1900"># Allow the metal piece to run in isolation</span></span>
<span style="font-weight: bold"><span style="color: #000080">require</span></span><span style="color: #990000">(</span>File<span style="color: #990000">.</span>dirname<span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">__FILE__</span></span><span style="color: #990000">)</span> <span style="color: #990000">+</span> <span style="color: #FF0000">"/../../config/environment"</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">unless</span></span> <span style="font-weight: bold"><span style="color: #0000FF">defined</span></span><span style="color: #990000">?(</span>Rails<span style="color: #990000">)</span>

<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Poller
  <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>call<span style="color: #990000">(</span>env<span style="color: #990000">)</span>
    <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> env<span style="color: #990000">[</span><span style="color: #FF0000">"PATH_INFO"</span><span style="color: #990000">]</span> <span style="color: #990000">=~</span> <span style="color: #FF6600">/^\/poller/</span>
      <span style="color: #990000">[</span><span style="color: #993399">200</span><span style="color: #990000">,</span> <span style="color: #FF0000">{</span><span style="color: #FF0000">"Content-Type"</span> <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"text/html"</span><span style="color: #FF0000">}</span><span style="color: #990000">,</span> <span style="color: #990000">[</span><span style="color: #FF0000">"Hello, World!"</span><span style="color: #990000">]]</span>
    <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>
      <span style="color: #990000">[</span><span style="color: #993399">404</span><span style="color: #990000">,</span> <span style="color: #FF0000">{</span><span style="color: #FF0000">"Content-Type"</span> <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"text/html"</span><span style="color: #FF0000">}</span><span style="color: #990000">,</span> <span style="color: #990000">[</span><span style="color: #FF0000">"Not Found"</span><span style="color: #990000">]]</span>
    <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
  <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
<div class="paragraph"><p>Metal applications are an optimization. You should make sure to <a href="http://weblog.rubyonrails.org/2008/12/20/performance-of-rails-metal">understand the related performance implications</a> before using it.</p></div>
<h3 id="_execution_order">4.2. Execution Order</h3>
<div class="paragraph"><p>All Metal Applications are executed by <tt>Rails::Rack::Metal</tt> middleware, which is a part of the <tt>ActionController::MiddlewareStack</tt> chain.</p></div>
<div class="paragraph"><p>Here&#8217;s the primary method responsible for running the Metal applications:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> call<span style="color: #990000">(</span>env<span style="color: #990000">)</span>
  <span style="color: #009900">@metals</span><span style="color: #990000">.</span>keys<span style="color: #990000">.</span>each <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>app<span style="color: #990000">|</span>
    result <span style="color: #990000">=</span> app<span style="color: #990000">.</span>call<span style="color: #990000">(</span>env<span style="color: #990000">)</span>
    <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> result <span style="font-weight: bold"><span style="color: #0000FF">unless</span></span> result<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">].</span>to_i <span style="color: #990000">==</span> <span style="color: #993399">404</span>
  <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
  <span style="color: #009900">@app</span><span style="color: #990000">.</span>call<span style="color: #990000">(</span>env<span style="color: #990000">)</span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
<div class="paragraph"><p>In the code above, <tt>@metals</tt> is an ordered ( alphabetical ) hash of metal applications. Due to the alphabetical ordering, <tt>aaa.rb</tt> will come before <tt>bbb.rb</tt> in the metal chain.</p></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
<img src="./images/icons/important.png" alt="Important" />
</td>
<td class="content">Metal applications cannot return the HTTP Status <tt>404</tt> to a client, as it is used for continuing the Metal chain execution. Please use normal Rails controllers or a custom middleware if returning <tt>404</tt> is a requirement.</td>
</tr></table>
</div>
</div>
<h2 id="_middlewares_and_rails">5. Middlewares and Rails</h2>
<div class="sectionbody">
</div>
<h2 id="_changelog">6. Changelog</h2>
<div class="sectionbody">
<div class="paragraph"><p><a href="http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/4">Lighthouse ticket</a></p></div>
<div class="ulist"><ul>
<li>
<p>
January 11, 2009: First version by <a href="../authors.html#lifo">Pratik</a>
</p>
</li>
</ul></div>
</div>

		</div>
	</div>
</body>
</html>