-
Notifications
You must be signed in to change notification settings - Fork 0
/
initialization.html
866 lines (757 loc) · 44 KB
/
initialization.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
<!DOCTYPE html>
<html lang="zh-TW">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Rails 啟動過程 — Ruby on Rails 指南</title>
<meta name="description" content="Ruby on Rails 指南:系統學習 Rails(Rails 4.2 版本)" >
<meta name="keywords" content="Ruby on Rails Guides 指南 中文 學習 免費 網路 Web 開發" >
<meta name="author" content="http://git.io/G_R1sA">
<meta property="fb:admins" content="1340181291">
<meta property="og:title" content="Rails 啟動過程 — Ruby on Rails 指南" >
<meta property="og:site_name" content="Ruby on Rails 指南">
<meta property="og:image" content="http://rails.ruby.tw/images/rails_guides_cover.jpg">
<meta property="og:url" content="http://rails.ruby.tw/">
<meta property="og:type" content="article">
<meta property="og:description" content="Ruby on Rails 指南:系統學習 Rails(Rails 4.2 版本)">
<link rel="stylesheet" href="stylesheets/application.css">
<link href="http://fonts.googleapis.com/css?family=Noto+Sans:400,700|Noto+Serif:700|Source+Code+Pro" rel="stylesheet">
<link href="images/favicon.ico" rel="shortcut icon" type="image/x-icon">
</head>
<body class="guide">
<div id="fb-root"></div>
<script>(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/zh-TW/sdk.js#xfbml=1&appId=837401439623727&version=v2.0";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>
<script type="text/javascript">
window.twttr=(function(d,s,id){var t,js,fjs=d.getElementsByTagName(s)[0];if(d.getElementById(id)){return}js=d.createElement(s);js.id=id;js.src="https://platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);return window.twttr||(t={_e:[],ready:function(f){t._e.push(f)}})}(document,"script","twitter-wjs"));
</script>
<div id="topNav">
<div class="wrapper">
<strong class="more-info-label">更多內容 <a href="http://rubyonrails.org/">rubyonrails.org:</a></strong>
<span class="red-button more-info-button">
更多內容
</span>
<ul class="more-info-links s-hidden">
<li class="more-info"><a href="http://rubyonrails.org/">綜覽</a></li>
<li class="more-info"><a href="http://rubyonrails.org/download">下載</a></li>
<li class="more-info"><a href="http://rubyonrails.org/deploy">部署</a></li>
<li class="more-info"><a href="https://github.com/rails/rails">原始碼</a></li>
<li class="more-info"><a href="http://rubyonrails.org/screencasts">影片</a></li>
<li class="more-info"><a href="http://rubyonrails.org/documentation">文件</a></li>
<li class="more-info"><a href="http://rubyonrails.org/community">社群</a></li>
<li class="more-info"><a href="http://weblog.rubyonrails.org/">Blog</a></li>
</ul>
</div>
</div>
<div id="header">
<div class="wrapper clearfix">
<h1><a href="index.html" title="回首頁">Guides.rubyonrails.org</a></h1>
<ul class="nav">
<li><a class="nav-item" href="index.html">首頁</a></li>
<li class="guides-index guides-index-large">
<a href="index.html" id="guidesMenu" class="guides-index-item nav-item">指南目錄</a>
<div id="guides" class="clearfix" style="display: none;">
<hr>
<dl class="L">
<dt>起步走</dt>
<dd><a href="getting_started.html">Rails 起步走</a></dd>
<dt>Models</dt>
<dd><a href="active_record_basics.html">Active Record 基礎</a></dd>
<dd><a href="active_record_migrations.html">Active Record 遷移</a></dd>
<dd><a href="active_record_validations.html">Active Record 驗證</a></dd>
<dd><a href="active_record_callbacks.html">Active Record 回呼</a></dd>
<dd><a href="association_basics.html">Active Record 關聯</a></dd>
<dd><a href="active_record_querying.html">Active Record 查詢</a></dd>
<dt>Views</dt>
<dd><a href="layouts_and_rendering.html">Rails 算繪與版型</a></dd>
<dd><a href="form_helpers.html">Action View 表單輔助方法</a></dd>
<dt>Controllers</dt>
<dd><a href="action_controller_overview.html">Action Controller 綜覽</a></dd>
<dd><a href="routing.html">Rails 路由:深入淺出</a></dd>
</dl>
<dl class="R">
<dt>深入了解</dt>
<dd><a href="active_support_core_extensions.html">Active Support 核心擴展</a></dd>
<dd><a href="i18n.html">Rails 國際化 API</a></dd>
<dd><a href="action_mailer_basics.html">Action Mailer 基礎</a></dd>
<dd><a href="active_job_basics.html">Active Job 基礎</a></dd>
<dd><a href="security.html">Rails 安全指南</a></dd>
<dd><a href="debugging_rails_applications.html">除錯 Rails 應用程式</a></dd>
<dd><a href="configuring.html">Rails 應用程式設定</a></dd>
<dd><a href="command_line.html">Rake 任務與 Rails 命令列工具</a></dd>
<dd><a href="asset_pipeline.html">Asset Pipeline</a></dd>
<dd><a href="working_with_javascript_in_rails.html">在 Rails 使用 JavaScript</a></dd>
<dd><a href="constant_autoloading_and_reloading.html">Constant Autoloading and Reloading</a></dd>
<dt>擴充 Rails</dt>
<dd><a href="rails_on_rack.html">Rails on Rack</a></dd>
<dd><a href="generators.html">客製與新建 Rails 產生器</a></dd>
<dd><a href="rails_application_templates.html">Rails 應用程式模版</a></dd>
<dt>貢獻 Ruby on Rails</dt>
<dd><a href="contributing_to_ruby_on_rails.html">貢獻 Ruby on Rails</a></dd>
<dd><a href="api_documentation_guidelines.html">API 文件準則</a></dd>
<dd><a href="ruby_on_rails_guides_guidelines.html">Ruby on Rails 指南準則</a></dd>
<dt>維護方針</dt>
<dd><a href="maintenance_policy.html">維護方針</a></dd>
<dt>發佈記</dt>
<dd><a href="upgrading_ruby_on_rails.html">升級 Ruby on Rails</a></dd>
<dd><a href="4_2_release_notes.html">Ruby on Rails 4.2 發佈記</a></dd>
<dd><a href="4_1_release_notes.html">Ruby on Rails 4.1 發佈記</a></dd>
<dd><a href="4_0_release_notes.html">Ruby on Rails 4.0 發佈記</a></dd>
<dd><a href="3_2_release_notes.html">Ruby on Rails 3.2 發佈記</a></dd>
<dd><a href="3_1_release_notes.html">Ruby on Rails 3.1 發佈記</a></dd>
<dd><a href="3_0_release_notes.html">Ruby on Rails 3.0 發佈記</a></dd>
<dd><a href="2_3_release_notes.html">Ruby on Rails 2.3 發佈記</a></dd>
<dd><a href="2_2_release_notes.html">Ruby on Rails 2.2 發佈記</a></dd>
<dt>Rails 指南翻譯術語</dt>
<dd><a href="translation_terms.html">翻譯術語</a></dd>
</dl>
</div>
</li>
<li><a class="nav-item" href="//github.com/docrails-tw/guides">貢獻翻譯</a></li>
<li><a class="nav-item" href="contributing_to_ruby_on_rails.html">貢獻</a></li>
<li><a class="nav-item" href="credits.html">致謝</a></li>
<li class="guides-index guides-index-small">
<select class="guides-index-item nav-item">
<option value="index.html">指南目錄</option>
<optgroup label="起步走">
<option value="getting_started.html">Rails 起步走</option>
</optgroup>
<optgroup label="Models">
<option value="active_record_basics.html">Active Record 基礎</option>
<option value="active_record_migrations.html">Active Record 遷移</option>
<option value="active_record_validations.html">Active Record 驗證</option>
<option value="active_record_callbacks.html">Active Record 回呼</option>
<option value="association_basics.html">Active Record 關聯</option>
<option value="active_record_querying.html">Active Record 查詢</option>
</optgroup>
<optgroup label="Views">
<option value="layouts_and_rendering.html">Rails 算繪與版型</option>
<option value="form_helpers.html">Action View 表單輔助方法</option>
</optgroup>
<optgroup label="Controllers">
<option value="action_controller_overview.html">Action Controller 綜覽</option>
<option value="routing.html">Rails 路由:深入淺出</option>
</optgroup>
<optgroup label="深入了解">
<option value="active_support_core_extensions.html">Active Support 核心擴展</option>
<option value="i18n.html">Rails 國際化 API</option>
<option value="action_mailer_basics.html">Action Mailer 基礎</option>
<option value="active_job_basics.html">Active Job 基礎</option>
<option value="security.html">Rails 安全指南</option>
<option value="debugging_rails_applications.html">除錯 Rails 應用程式</option>
<option value="configuring.html">Rails 應用程式設定</option>
<option value="command_line.html">Rake 任務與 Rails 命令列工具</option>
<option value="asset_pipeline.html">Asset Pipeline</option>
<option value="working_with_javascript_in_rails.html">在 Rails 使用 JavaScript</option>
<option value="constant_autoloading_and_reloading.html">Constant Autoloading and Reloading</option>
</optgroup>
<optgroup label="擴充 Rails">
<option value="rails_on_rack.html">Rails on Rack</option>
<option value="generators.html">客製與新建 Rails 產生器</option>
<option value="rails_application_templates.html">Rails 應用程式模版</option>
</optgroup>
<optgroup label="貢獻 Ruby on Rails">
<option value="contributing_to_ruby_on_rails.html">貢獻 Ruby on Rails</option>
<option value="api_documentation_guidelines.html">API 文件準則</option>
<option value="ruby_on_rails_guides_guidelines.html">Ruby on Rails 指南準則</option>
</optgroup>
<optgroup label="維護方針">
<option value="maintenance_policy.html">維護方針</option>
</optgroup>
<optgroup label="發佈記">
<option value="upgrading_ruby_on_rails.html">升級 Ruby on Rails</option>
<option value="4_2_release_notes.html">Ruby on Rails 4.2 發佈記</option>
<option value="4_1_release_notes.html">Ruby on Rails 4.1 發佈記</option>
<option value="4_0_release_notes.html">Ruby on Rails 4.0 發佈記</option>
<option value="3_2_release_notes.html">Ruby on Rails 3.2 發佈記</option>
<option value="3_1_release_notes.html">Ruby on Rails 3.1 發佈記</option>
<option value="3_0_release_notes.html">Ruby on Rails 3.0 發佈記</option>
<option value="2_3_release_notes.html">Ruby on Rails 2.3 發佈記</option>
<option value="2_2_release_notes.html">Ruby on Rails 2.2 發佈記</option>
</optgroup>
<optgroup label="Rails 指南翻譯術語">
<option value="translation_terms.html">翻譯術語</option>
</optgroup>
</select>
</li>
</ul>
</div>
</div>
</div>
<hr class="hide">
<div id="feature">
<div class="wrapper">
<h2>Rails 啟動過程</h2><p>本篇介紹 Rails 4 啟動過程的內部原理。非常深入的一篇指南,推薦進階的 Rails 開發者閱讀。</p><p>讀完本篇,您將了解:</p>
<ul>
<li>如何使用 <code>rails server</code>。</li>
<li>Rails 啟動過程的時間軸。</li>
<li>啟動時 <code>require</code> 了那些檔案。</li>
<li><code>Rails::Server</code> 介面是如何定義的,以及如何使用。</li>
</ul>
<div id="subCol">
<h3 class="chapter"><img src="images/chapters_icon.gif" alt="" />Chapters</h3>
<ol class="chapters">
<li>
<a href="#%E5%95%9F%E5%8B%95%EF%BC%81">啟動!</a>
<ul>
<li><a href="#railties-bin-rails"><code>railties/bin/rails</code></a></li>
<li><a href="#railties-lib-rails-app-rails-loader-rb"><code>railties/lib/rails/app_rails_loader.rb</code></a></li>
<li><a href="#bin-rails"><code>bin/rails</code></a></li>
<li><a href="#config-boot-rb"><code>config/boot.rb</code></a></li>
<li><a href="#rails-commands-rb"><code>rails/commands.rb</code></a></li>
<li><a href="#rails-commands-command-tasks-rb"><code>rails/commands/command_tasks.rb</code></a></li>
<li><a href="#actionpack-lib-action-dispatch-rb"><code>actionpack/lib/action_dispatch.rb</code></a></li>
<li><a href="#rails-commands-server-rb"><code>rails/commands/server.rb</code></a></li>
<li><a href="#%E5%95%9F%E5%8B%95%EF%BC%81-rack-lib-rack-server-rb">Rack: <code>lib/rack/server.rb</code></a></li>
<li><a href="#%E5%95%9F%E5%8B%95%EF%BC%81-config-application-rb"><code>config/application.rb</code></a></li>
<li><a href="#rails-server-start"><code>Rails::Server#start</code></a></li>
<li><a href="#config-environment-rb"><code>config/environment.rb</code></a></li>
<li><a href="#%E5%95%9F%E5%8B%95%EF%BC%81-config-application-rb"><code>config/application.rb</code></a></li>
</ul>
</li>
<li>
<a href="#%E8%BC%89%E5%85%A5-rails">載入 Rails</a>
<ul>
<li><a href="#railties-lib-rails-all-rb"><code>railties/lib/rails/all.rb</code></a></li>
<li><a href="#%E5%9B%9E%E5%88%B0-config-environment-rb">回到 <code>config/environment.rb</code></a></li>
<li><a href="#railties-lib-rails-application-rb"><code>railties/lib/rails/application.rb</code></a></li>
<li><a href="#%E8%BC%89%E5%85%A5-rails-rack-lib-rack-server-rb">Rack: lib/rack/server.rb</a></li>
</ul>
</li>
</ol>
</div>
</div>
</div>
<div id="container">
<div class="wrapper">
<div id="mainCol">
<p>本篇針對 Rails 4,走一遍啟動 Rails 所需的每個方法呼叫。過程中詳細解釋每個步驟的用途,特別針對從 <code>rails server</code>, 到應用程式啟動起來之間的過程做解說。</p><div class="note"><p>除非特別聲明,本篇提及的路徑都是相對於 <a href="https://github.com/rails/rails">Rails 原始碼</a>的目錄,或相對於 Rails 應用程式的路徑。</p></div><div class="info"><p>若想跟著瀏覽 Rails 的<a href="https://github.com/rails/rails">原始碼</a>,推薦<a href="https://github.com/blog/793-introducing-the-file-finder">使用 GitHub 提供的檔案搜索</a>功能,來快速找到檔案,在 GitHub Repository 頁面按 <code>t</code> 即可使用。</p></div><h3 id="啟動!">1 啟動!</h3><p>從初始化(Initialize)與啟動(boot)應用程式開始。Rails 應用程式通常在執行 <code>rails server</code> 或 <code>rails console</code> 時會啟動。</p><h4 id="railties-bin-rails">1.1 <code>railties/bin/rails</code>
</h4><p><a href="https://github.com/rails/rails/blob/master/railties/bin/rails">View Source</a></p><p><code>rails server</code> 命令裡的 <code>rails</code>,是放在載入路徑(load path)下的 Ruby 執行檔。這個執行檔的內容如下:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
version = ">= 0"
load Gem.bin_path('railties', 'rails', version)
</pre>
</div>
<p>若在 Rails Console 裡試這個命令,會看到這個命令載入了 <a href="https://github.com/rails/rails/blob/master/railties/bin/rails"><code>railties/bin/rails</code></a>。</p><p><code>railties/bin/rails</code> 裡有這一行:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
require "rails/cli"
</pre>
</div>
<p><a href="https://github.com/rails/rails/blob/master/railties/lib/rails/cli.rb"><code>railties/lib/rails/cli</code></a> 接著呼叫 <code>Rails::AppRailsLoader.exec_app_rails</code>。</p><h4 id="railties-lib-rails-app-rails-loader-rb">1.2 <code>railties/lib/rails/app_rails_loader.rb</code>
</h4><p><a href="https://github.com/rails/rails/blob/master/railties/lib/rails/app_rails_loader.rb">View Source</a></p><p><code>exec_app_rails</code> 的主要目的是執行應用程式的 <code>bin/rails</code>,若當前目錄沒有 <code>bin/rails</code>,會往上搜索,看找不找的到 <code>bin/rails</code>。這也是為什麼可以在 rails 應用程式裡的任何目錄下使用 <code>rails</code> 命令。</p><p><code>rails server</code> 實際上等於下面這個命令:</p><div class="code_container">
<pre class="brush: plain; gutter: false; toolbar: false">
$ exec ruby bin/rails server
</pre>
</div>
<h4 id="bin-rails">1.3 <code>bin/rails</code>
</h4><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
#!/usr/bin/env ruby
APP_PATH = File.expand_path('../../config/application', __FILE__)
require_relative '../config/boot'
require 'rails/commands'
</pre>
</div>
<p><code>APP_PATH</code> 常數之後會被 <code>rails/commands</code> 使用。這裡引用的 <code>config/boot</code> 檔案是指 <code>config/boot.rb</code>,負責載入與設定 Bundler。</p><h4 id="config-boot-rb">1.4 <code>config/boot.rb</code>
</h4><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
# Set up gems listed in the Gemfile.
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE'])
</pre>
</div>
<p>標準的 Rails 應用程式裡,會有一個檔案,裡面記錄所有依賴的 RubyGems:<code>Gemfile</code>。<code>config/boot.rb</code> 將 <code>ENV['BUNDLE_GEMFILE']</code> 設為 <code>Gemfile</code> 的位置。若 <code>Gemfile</code> 存在,則需要 <code>require 'bundler/setup'</code>。這一行是 Bundler 用來設定 <code>Gemfile</code> 內所有相依 RubyGems 的載入路徑。</p><p>標準的 Rails 應用程式依賴以下 RubyGems:</p>
<ul>
<li>actionmailer</li>
<li>actionpack</li>
<li>actionview</li>
<li>activemodel</li>
<li>activerecord</li>
<li>activesupport</li>
<li>arel</li>
<li>builder</li>
<li>bundler</li>
<li>erubis</li>
<li>i18n</li>
<li>mail</li>
<li>mime-types</li>
<li>rack</li>
<li>rack-cache</li>
<li>rack-mount</li>
<li>rack-test</li>
<li>rails</li>
<li>railties</li>
<li>rake</li>
<li>sqlite3</li>
<li>thor</li>
<li>tzinfo</li>
</ul>
<h4 id="rails-commands-rb">1.5 <code>rails/commands.rb</code>
</h4><p><a href="https://github.com/rails/rails/blob/master/railties/lib/rails/commands.rb">View Source</a></p><p><code>config/boot.rb</code> 執行完畢後,下個 <code>require</code> 的檔案是 <code>rails/commands</code>,用來展開命令的別名(alias)。在 <code>rails server</code> 這個情況裡,<code>ARGV</code> 的內容是 <code>server</code>,無需展開:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
ARGV << '--help' if ARGV.empty?
aliases = {
"g" => "generate",
"d" => "destroy",
"c" => "console",
"s" => "server",
"db" => "dbconsole",
"r" => "runner"
}
command = ARGV.shift
command = aliases[command] || command
require 'rails/commands/commands_tasks'
Rails::CommandsTasks.new(ARGV).run_command!(command)
</pre>
</div>
<div class="info"><p>如上所見,<code>ARGV</code> 為空時,Rails 會印出幫助訊息。</p></div><p>若用了別名,如 <code>rails s</code>,便會用 <code>aliases</code> 展開成對應的命令:</p><h4 id="rails-commands-command-tasks-rb">1.6 <code>rails/commands/command_tasks.rb</code>
</h4><p><a href="https://github.com/rails/rails/blob/master/railties/lib/rails/commands/commands_tasks.rb">View Source</a></p><p>當輸入錯的 Rails 命令時,<code>run_command!</code> 負責拋出錯誤訊息。若命令是有效的,則會呼叫與命令同名的方法。</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
COMMAND_WHITELIST = %(plugin generate destroy console server dbconsole application runner new version help)
def run_command!(command)
command = parse_command(command)
if COMMAND_WHITELIST.include?(command)
send(command)
else
write_error_message(command)
end
end
</pre>
</div>
<p>假設傳入的命令是 <code>server</code>,Rails 會執行以下的程式碼:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
def server
set_application_directory!
require_command!("server")
Rails::Server.new.tap do |server|
# We need to require application after the server sets environment,
# otherwise the --environment option given to the server won't propagate.
require APP_PATH
Dir.chdir(Rails.application.root)
server.start
end
end
private
def set_application_directory!
Dir.chdir(File.expand_path('../../', APP_PATH)) unless File.exist?(File.expand_path("config.ru"))
end
def require_command!(command)
require "rails/commands/#{command}"
end
</pre>
</div>
<p>沒找到 <code>config.ru</code> 時,會切換到 Rails 的根目錄(從 <code>APP_PATH</code> 往上兩層,<code>APP_PATH</code> 指向 <code>config/application.rb</code> )。接著 <code>require</code> <code>rails/commands/server</code>([rails/commands/server.rb](rails/commands/server),會把 <code>Rails::Server</code> 類別設定好。</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
require 'fileutils'
require 'optparse'
require 'action_dispatch'
require 'rails'
module Rails
class Server < ::Rack::Server
</pre>
</div>
<p><code>fileutils</code> 和 <code>optparse</code> 是 Ruby 的標準函式庫,用來處理檔案與解析命令行參數。</p><h4 id="actionpack-lib-action-dispatch-rb">1.7 <code>actionpack/lib/action_dispatch.rb</code>
</h4><p><a href="https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch.rb">View Source</a></p><p>Action Dispatch 是 Rails 框架負責處理路由的元件。為 Rails 加入像是路由、Session 以及常見的 Middlewares。</p><h4 id="rails-commands-server-rb">1.8 <code>rails/commands/server.rb</code>
</h4><p><a href="https://github.com/rails/rails/blob/master/railties/lib/rails/commands/server.rb">View Source</a></p><p><code>Rails::Server</code> 在這個檔案裡定義,繼承自 <code>Rack::Server</code>。呼叫 <code>Rails::Server.new</code> 時,會呼叫 <a href="https://github.com/rails/rails/blob/master/railties/lib/rails/commands/server.rb"><code>rails/commands/server.rb</code></a> 裡的 <code>initialize</code> 方法:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
def initialize(*)
super
set_environment
end
</pre>
</div>
<p>首先呼叫 <code>super</code>,<code>super</code> 會呼叫 <code>Rack::Server</code> 的 <code>initialize</code>。</p><h4 id="啟動!-rack-lib-rack-server-rb">1.9 Rack: <code>lib/rack/server.rb</code>
</h4><p><a href="https://github.com/rack/rack/blob/master/lib/rack/server.rb">View Source</a></p><p><code>Rack::Server</code> 負責給所有基於 Rack 的應用程式,提供通用的伺服器接口(interface),Rails 也是基於 Rack 的應用程式。</p><p><code>Rack::Server</code> 的 <code>initialize</code> 方法只是設定幾個變數而已:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
def initialize(options = nil)
@options = options
@app = options[:app] if options && options[:app]
end
</pre>
</div>
<p>這個情況裡,<code>options</code> 會是 <code>nil</code>,所以 <code>initialize</code> 什麼也沒做。</p><p><code>super</code> 結束之後,回到 <code>rails/commands/server.rb</code>。接著在 <code>Rails::Server</code> 的上下文裡呼叫 <code>set_environment</code>,猛一看好像沒做什麼:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
def set_environment
ENV["RAILS_ENV"] ||= options[:environment]
end
</pre>
</div>
<p>實際上 <code>options</code> 方法做了很多事情。這個方法在 <code>Rack::Server</code> 的定義是:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
def options
@options ||= parse_options(ARGV)
end
</pre>
</div>
<p><code>parse_options</code> 方法的內容:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
def parse_options(args)
options = default_options
# Don't evaluate CGI ISINDEX parameters.
# http://www.meb.uni-bonn.de/docs/cgi/cl.html
args.clear if ENV.include?("REQUEST_METHOD")
options.merge! opt_parser.parse!(args)
options[:config] = ::File.expand_path(options[:config])
ENV["RACK_ENV"] = options[:environment]
options
end
</pre>
</div>
<p><code>default_options</code> 的內容:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
def default_options
environment = ENV['RACK_ENV'] || 'development'
default_host = environment == 'development' ? 'localhost' : '0.0.0.0'
{
:environment => environment,
:pid => nil,
:Port => 9292,
:Host => default_host,
:AccessLog => [],
:config => "config.ru"
}
end
</pre>
</div>
<p>接著看到,因為 <code>ENV</code> 裡沒有 <code>REQUEST_METHOD</code>,可以忽略 <code>args.clear</code>。下一行 <code>options.merge! opt_parser.parse!(args)</code>,把從命令行來的參數與 <code>opt_parser</code> 的選項合併,<code>opt_parser</code> 在 <code>Rack::Server</code> 裡定義:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
def opt_parser
Options.new
end
</pre>
</div>
<p>雖然 <a href="https://github.com/rack/rack/blob/master/lib/rack/server.rb#L6-L87"><code>parse!</code></a> 是在 <code>Rack::Server</code> 裡定義,但在 <code>Rails::Server</code> <a href="https://github.com/rails/rails/blob/master/railties/lib/rails/commands/server.rb#L9-L47">被覆寫了</a>,因為要收不同的參數。<code>Rails::Server</code> 定義的 <code>parse!</code> 方法開頭是:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
def parse!(args)
args, options = args.dup, {}
opt_parser = OptionParser.new do |opts|
opts.banner = "Usage: rails server [mongrel, thin, etc] [options]"
opts.on("-p", "--port=port", Integer,
"Runs Rails on the specified port.", "Default: 3000") { |v| options[:Port] = v }
...
</pre>
</div>
<p>這個方法會設定好 <code>options</code> 所有的鍵,Rails 根據這些鍵,決定伺服器該怎麼執行。在 <code>initialize</code> 結束之後,回到 <a href="https://github.com/rails/rails/blob/master/railties/lib/rails/commands/commands_tasks.rb"><code>rails/commands/command_tasks.rb</code></a>,見 <code># Back to here</code>:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
def server
set_application_directory!
require_command!("server")
# Back to here
Rails::Server.new.tap do |server|
# We need to require application after the server sets environment,
# otherwise the --environment option given to the server won't propagate.
require APP_PATH
Dir.chdir(Rails.application.root)
server.start
end
end
</pre>
</div>
<h4 id="啟動!-config-application-rb">1.10 <code>config/application.rb</code>
</h4><p>當 <code>require APP_PATH</code> 執行時,會載入 <code>config/application.rb</code>。回想一下,<code>APP_PATH</code> 在 Rails 應用程式下的 <code>bin/rails</code> 裡定義:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
APP_PATH = File.expand_path('../../config/application', __FILE__)
</pre>
</div>
<p><code>config/application.rb</code> 裡放的是任何要對應用程式修改的設定。</p><h4 id="rails-server-start">1.11 <code>Rails::Server#start</code>
</h4><p><a href="https://github.com/rails/rails/blob/master/railties/lib/rails/commands/server.rb#L70-L81">View Source</a></p><p><code>config/application.rb</code> 載入完畢後,呼叫了 <code>server.start</code>。<code>#start</code> 方法的定義是:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
def start
print_boot_information
trap(:INT) { exit }
create_tmp_directories
log_to_stdout if options[:log_stdout]
super
...
end
private
def print_boot_information
...
puts "=> Run `rails server -h` for more startup options"
...
puts "=> Ctrl-C to shutdown server" unless options[:daemonize]
end
def create_tmp_directories
%w(cache pids sessions sockets).each do |dir_to_make|
FileUtils.mkdir_p(File.join(Rails.root, 'tmp', dir_to_make))
end
end
def log_to_stdout
wrapped_app # touch the app so the logger is set up
console = ActiveSupport::Logger.new($stdout)
console.formatter = Rails.logger.formatter
console.level = Rails.logger.level
Rails.logger.extend(ActiveSupport::Logger.broadcast(console))
end
</pre>
</div>
<p>Rails 啟動過程“初次輸出訊息”的地方。這個方法會捕捉 <code>INT</code> 信號,所以當你按下 <code>CTRL-C</code> 時,才能從進程(process)裡離開。從這段程式碼可以看到,會建立出 <code>tmp/cache</code>、<code>tmp/pids</code>、<code>tmp/sessions</code> 以及 <code>tmp/sockets</code> 這四個目錄。接著呼叫 <code>wrapped_app</code>,這個方法負責在指定 <code>ActiveSupport::Logger</code> 之前,建立出 Rack 應用程式。</p><p>上面 <code>start</code> 方法裡的 <code>super</code> 方法會呼叫 <a href="https://github.com/rack/rack/blob/master/lib/rack/server.rb#L228-L265"><code>Rack::Server.start</code></a>,此方法定義如下:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
def start &blk
if options[:warn]
$-w = true
end
if includes = options[:include]
$LOAD_PATH.unshift(*includes)
end
if library = options[:require]
require library
end
if options[:debug]
$DEBUG = true
require 'pp'
p options[:server]
pp wrapped_app
pp app
end
check_pid! if options[:pid]
# Touch the wrapped app, so that the config.ru is loaded before
# daemonization (i.e. before chdir, etc).
wrapped_app
daemonize_app if options[:daemonize]
write_pid if options[:pid]
trap(:INT) do
if server.respond_to?(:shutdown)
server.shutdown
else
exit
end
end
server.run wrapped_app, options, &blk
end
</pre>
</div>
<p>Rails 應用程式感興趣的是最後一行,<code>server.run</code>。這裡又遇到 <code>wrapped_app</code> 方法了,是深入介紹的時候了。</p><p><code>wrapped_app</code> 的定義:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
@wrapped_app ||= build_app app
</pre>
</div>
<p><code>app</code> 方法的定義:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
def app
@app ||= options[:builder] ? build_app_from_string : build_app_and_options_from_config
end
...
private
def build_app_and_options_from_config
if !::File.exist? options[:config]
abort "configuration #{options[:config]} not found"
end
app, options = Rack::Builder.parse_file(self.options[:config], opt_parser)
self.options.merge! options
app
end
def build_app_from_string
Rack::Builder.new_from_string(self.options[:builder])
end
</pre>
</div>
<p><code>options[:config]</code> 的預設值是 <code>config.ru</code>,而 <code>config.ru</code> 的內容:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
# This file is used by Rack-based servers to start the application.
require ::File.expand_path('../config/environment', __FILE__)
run <%= app_const %>
</pre>
</div>
<p><a href="https://github.com/rack/rack/blob/371cf6f3a8d390edfa901b6f963b78810270a387/lib/rack/builder.rb#L32-L46"><code>Rack::Builder.parse_file</code></a> 方法讀取 <code>config.ru</code> ,並進行解析:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
app = new_from_string cfgfile, config
...
def self.new_from_string(builder_script, file="(rackup)")
eval "Rack::Builder.new {\n" + builder_script + "\n}.to_app",
TOPLEVEL_BINDING, file, 0
end
</pre>
</div>
<p><code>Rack::Builder</code> 的 <a href="https://github.com/rack/rack/blob/371cf6f3a8d390edfa901b6f963b78810270a387/lib/rack/builder.rb#L53-L56"><code>initialize</code> 方法</a>接受區塊參數,會在 <code>Rack::Builder</code> 的實體裡執行這個區塊。Rails 啟動過程主要都在這裡發生。最先執行的是 <code>config.ru</code> 裡的這一行:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
require ::File.expand_path('../config/environment', __FILE__)
</pre>
</div>
<h4 id="config-environment-rb">1.12 <code>config/environment.rb</code>
</h4><p>這個檔案通常由 <code>config.ru</code>(即 <code>rails server</code>)與 Passenger <code>require</code> 進來。這也是兩種啟動伺服器方法首次相遇的地方。在這之前都只是在設定 Rack 與 Rails 而已。</p><p>這個檔案從 <code>require</code> <code>config/application.rb</code> 開始:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
# Load the Rails application.
require File.expand_path('../application', __FILE__)
</pre>
</div>
<h4 id="啟動!-config-application-rb">1.13 <code>config/application.rb</code>
</h4><p>這個檔案 <code>require</code> <code>config/boot.rb</code>:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
require File.expand_path('../boot', __FILE__)
</pre>
</div>
<p>但只在 <code>config/boot.rb</code> 沒有被 <code>require</code> 的前提下才會進行 <code>require</code>。如此一來 <code>rails server</code> 才不會重複 <code>require</code>,但 Passenger 每次都會重新 <code>require</code> <code>config/boot.rb</code>。</p><p>有趣的事情開始了!</p><h3 id="載入-rails">2 載入 Rails</h3><p><code>config/application.rb</code> 檔案的下一行是:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
require 'rails/all'
</pre>
</div>
<h4 id="railties-lib-rails-all-rb">2.1 <code>railties/lib/rails/all.rb</code>
</h4><p><a href="https://github.com/rails/rails/blob/master/railties/lib/rails/all.rb">View Source</a></p><p>這個檔案負責 <code>require</code> Rails 框架的各個元件:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
require "rails"
%w(
active_record
action_controller
action_view
action_mailer
rails/test_unit
sprockets
).each do |framework|
begin
require "#{framework}/railtie"
rescue LoadError
end
end
</pre>
</div>
<p>這是整個 Rails 框架載入的地方,讓每個元件在應用程式裡都可以使用。每個部分怎麼載入的不深入探究,但有興趣可以自己深入研究。</p><p>現在只要記得,共用的功能像是 Rails 引擎、I18n 以及 Rails 所有的設定都是在這裡定義完成。</p><h4 id="回到-config-environment-rb">2.2 回到 <code>config/environment.rb</code>
</h4><p><code>config/application.rb</code> 的其他部分定義了 <code>Rails::Application</code> 的設定,這些設定在應用程式啟動完畢時會全部載入進來。當 <code>config/application.rb</code> 載入 Rails 完畢時,以及應用程式命名空間定義完畢時,會回到應用程式初始化的地方,也就是 <code>config/environment.rb</code>。舉個例子,若應用程式叫做 <code>Blog</code>,則會找到 <code>Rails.application.initialize!</code>,這個方法在 <code>rails/application.rb</code> 裡定義。</p><h4 id="railties-lib-rails-application-rb">2.3 <code>railties/lib/rails/application.rb</code>
</h4><p><a href="https://github.com/rails/rails/blob/master/railties/lib/rails/application.rb">View Source</a></p><p><code>initialize!</code> 方法:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
def initialize!(group=:default) #:nodoc:
raise "Application has been already initialized." if @initialized
run_initializers(group, self)
@initialized = true
self
end
</pre>
</div>
<p>可以看到應用程式只會初始化一次。Initializers(<code>config/initializers</code> 目錄下的設定檔)透過 <code>run_initializers</code> 方法依序執行,<code>run_initializers</code> 方法在 <a href="https://github.com/rails/rails/blob/master/railties/lib/rails/initializable.rb"><code>railties/lib/rails/initializable.rb</code></a> 裡定義:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
def run_initializers(group=:default, *args)
return if instance_variable_defined?(:@ran)
initializers.tsort_each do |initializer|
initializer.run(*args) if initializer.belongs_to?(group)
end
@ran = true
end
</pre>
</div>
<p><code>run_initializers</code> 很巧妙。在這裡會遍歷所有類別的祖先,找出有回應 <code>initializers</code> 方法的類別。接著按名稱將這些類別排序,再執行它們。舉例來說,<code>Engine</code> 類別透過給每個 Engine 提供 <code>initializers</code> 方法,讓這些 Engine 都可以引用進來。</p><p>Rails::Application 類別(在 <a href="https://github.com/rails/rails/blob/master/railties/lib/rails/application.rb"><code>railties/lib/rails/application.rb</code></a> 裡定義)定義了 bootstrap、railtie、finisher 這三個 Initializers。第一個執行的 Initializer 是 bootstrap,bootstrap 將應用程式準備好(像是初始化 logger),而 finisher initializer 則是最後執行(像是把 Middleware 都建好)。而 railtie initializers 則是在 <code>Rails::Application</code> 裡定義,在 bootstrap 與 finisher 之間執行。</p><p>Initializers 都執行完畢後,回到 <code>Rack::Server</code>。</p><h4 id="載入-rails-rack-lib-rack-server-rb">2.4 Rack: lib/rack/server.rb</h4><p><a href="https://github.com/rack/rack/blob/master/lib/rack/server.rb">View Source</a></p><p>在 <a href="#%E5%95%9F%E5%8B%95%EF%BC%81-rack:-lib/rack/server.rb">1.9 小節</a> ,我們看過 <code>app</code> 是如何被定義的:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
def app
@app ||= options[:builder] ? build_app_from_string : build_app_and_options_from_config
end
...
private
def build_app_and_options_from_config
if !::File.exist? options[:config]
abort "configuration #{options[:config]} not found"
end
app, options = Rack::Builder.parse_file(self.options[:config], opt_parser)
self.options.merge! options
app
end
def build_app_from_string
Rack::Builder.new_from_string(self.options[:builder])
end
</pre>
</div>
<p>到了這一步,<code>app</code> 便是 Rails 應用程式本身(Middleware),接下來 Rack 會呼叫所有的 Middlewares:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
def build_app(app)
middleware[options[:environment]].reverse_each do |middleware|
middleware = middleware.call(self) if middleware.respond_to?(:call)
next unless middleware
klass = middleware.shift
app = klass.new(app, *middleware)
end
app
end
</pre>
</div>
<p>記得 <code>wrapped_app</code> 在 <code>Server#start</code> 呼叫了 <code>build_app</code> (最後一行):</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
server.run wrapped_app, options, &blk
</pre>
</div>
<p>到這裡 <code>server.run</code> 取決於所使用的伺服器實作是那一個。假設用的是 Puma,下面是 Puma 的 <code>run</code> 方法:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
...
DEFAULT_OPTIONS = {
:Host => '0.0.0.0',
:Port => 8080,
:Threads => '0:16',
:Verbose => false
}
def self.run(app, options = {})
options = DEFAULT_OPTIONS.merge(options)
if options[:Verbose]
app = Rack::CommonLogger.new(app, STDOUT)
end
if options[:environment]
ENV['RACK_ENV'] = options[:environment].to_s
end
server = ::Puma::Server.new(app)
min, max = options[:Threads].split(':', 2)
puts "Puma #{::Puma::Const::PUMA_VERSION} starting..."
puts "* Min threads: #{min}, max threads: #{max}"
puts "* Environment: #{ENV['RACK_ENV']}"
puts "* Listening on tcp://#{options[:Host]}:#{options[:Port]}"
server.add_tcp_listener options[:Host], options[:Port]
server.min_threads = min
server.max_threads = max
yield server if block_given?
begin
server.run.join
rescue Interrupt
puts "* Gracefully stopping, waiting for requests to finish"
server.stop(true)
puts "* Goodbye!"
end
end
</pre>
</div>
<p>伺服器本身的實作不深入探究,但這是 Rails 啟動過程整個旅程的最後一站。</p><p>希望這高度抽象的綜覽能幫助你更好的了解 Rails 程式是如何執行的,進而成為一個更好的 Rails 開發者。若想了解更多的話,那就閱讀 Rails 的原始碼吧!</p>
<h3>反饋</h3>
<p>
歡迎幫忙改善指南的品質。
</p>
<p>
如發現任何錯誤之處,歡迎修正。開始貢獻前,可以先閱讀<a href="http://edgeguides.rubyonrails.org/contributing_to_ruby_on_rails.html#contributing-to-the-rails-documentation">貢獻指南:文件</a>。
</p>
<p>翻譯如有錯誤,深感抱歉,歡迎 <a href="https://github.com/docrails-tw/guides/fork">Fork</a> 修正,或至此處<a href="https://github.com/docsrails-tw/guides/issues/new">回報</a>。</p>
<p>
文章可能有未完成或過時的內容。請先檢查 <a href="http://edgeguides.rubyonrails.org">Edge Guides</a> 來確定問題在 master 是否已經修掉了。再上 master 補上缺少的文件。內容參考 <a href="ruby_on_rails_guides_guidelines.html">Ruby on Rails 指南準則</a>來了解行文風格。
</p>
<p>最後,任何關於 Ruby on Rails 文件的討論,歡迎至 <a href="http://groups.google.com/group/rubyonrails-docs">rubyonrails-docs 郵件論壇</a>。
</p>
</div>
</div>
</div>
<hr class="hide">
<div id="footer">
<div class="wrapper">
<p>本著作係採用<a href="https://creativecommons.org/licenses/by-sa/4.0/deed.zh_TW">創用 CC 姓名標示-相同方式分享 4.0 國際授權條款</a>授權。</p>
<p>“Rails”、“Ruby on Rails”,以及 Rails logo 為 David Heinemeier Hansson 的商標。版權所有。</p>
</div>
</div>
<script src="javascripts/jquery.min.js"></script>
<script src="javascripts/responsive-tables.js"></script>
<script src="javascripts/guides.js"></script>
<script src="javascripts/syntaxhighlighter/shCore.js"></script>
<script src="javascripts/syntaxhighlighter/shBrushRuby.js"></script>
<script src="javascripts/syntaxhighlighter/shBrushXml.js"></script>
<script src="javascripts/syntaxhighlighter/shBrushSql.js"></script>
<script src="javascripts/syntaxhighlighter/shBrushPlain.js"></script>
<script type="text/javascript">
SyntaxHighlighter.all();
$(guidesIndex.bind);
</script>
<script>
(function(i,s,o,g,r,a,m){i["GoogleAnalyticsObject"]=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,"script","//www.google-analytics.com/analytics.js","ga");
ga("create", "UA-49903900-1", "auto");
ga("require", "displayfeatures");
ga("send", "pageview");
</script>
</body>
</html>