sinatra 2.0.0.beta2 → 2.0.0.rc1
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of sinatra might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/CHANGELOG.md +8 -0
- data/Gemfile +4 -1
- data/LICENSE +2 -1
- data/README.de.md +640 -432
- data/README.es.md +1 -1
- data/README.hu.md +34 -0
- data/README.ja.md +86 -39
- data/README.md +407 -328
- data/README.ru.md +0 -38
- data/Rakefile +3 -2
- data/lib/sinatra/base.rb +38 -31
- data/lib/sinatra/show_exceptions.rb +55 -47
- data/lib/sinatra/version.rb +1 -1
- data/sinatra.gemspec +2 -2
- metadata +6 -7
data/README.es.md
CHANGED
@@ -1654,7 +1654,7 @@ get '/foo' do
|
|
1654
1654
|
request.ip # dirección IP del cliente
|
1655
1655
|
request.secure? # false (sería true sobre ssl)
|
1656
1656
|
request.forwarded? # true (si se está corriendo atrás de un proxy reverso)
|
1657
|
-
|
1657
|
+
request.env # hash de entorno directamente entregado por Rack
|
1658
1658
|
end
|
1659
1659
|
```
|
1660
1660
|
|
data/README.hu.md
CHANGED
@@ -649,6 +649,40 @@ Az alábbi kapcsolókat ismeri fel a rendszer:
|
|
649
649
|
-s # a rack szerver/handler beállítása (alapértelmezetten ez a thin)
|
650
650
|
-x # a mutex lock bekapcsolása (alapértelmezetten ki van kapcsolva)
|
651
651
|
|
652
|
+
## Több szálon futtatás
|
653
|
+
|
654
|
+
_Parafrázis [Konstantin StackOverflow válasza][so-answer] alapján_
|
655
|
+
|
656
|
+
A Sinatra nem szabja meg az konkurenciakezelés módját, hanem az alatta működő
|
657
|
+
Rack kezelőre (szerverre) hagyja ezt a feladatot, ami például a Thin, a Puma,
|
658
|
+
vagy a WEBrick. A Sinatra önmagában szálbiztos, tehát semmilyen probléma sem
|
659
|
+
adódik, ha a Rack kezelő többszálú konkurenciamodellt használ. Ezek szerint
|
660
|
+
szerverindításkor meg kell adni a Rack szervernek megfelelő indítási módot.
|
661
|
+
A következő példa egy többszálú Thin szerver indítását mutatja be.
|
662
|
+
|
663
|
+
```ruby
|
664
|
+
# app.rb
|
665
|
+
|
666
|
+
require 'sinatra/base'
|
667
|
+
|
668
|
+
class App < Sinatra::Base
|
669
|
+
get '/' do
|
670
|
+
"Hello, World"
|
671
|
+
end
|
672
|
+
end
|
673
|
+
|
674
|
+
App.run!
|
675
|
+
|
676
|
+
```
|
677
|
+
|
678
|
+
A szerverindítás parancsa a következő lenne:
|
679
|
+
|
680
|
+
``` shell
|
681
|
+
thin --threaded start
|
682
|
+
```
|
683
|
+
|
684
|
+
[so-answer]: http://stackoverflow.com/a/6282999/1725341
|
685
|
+
|
652
686
|
## Fejlesztői változat
|
653
687
|
|
654
688
|
Ha a Sinatra legfrissebb, fejlesztői változatát szeretnéd használni,
|
data/README.ja.md
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# Sinatra
|
2
2
|
|
3
3
|
*注)
|
4
|
-
|
4
|
+
本文書は英語から翻訳したものであり、その内容が最新でない場合もあります。最新の情報はオリジナルの英語版を参照してください。*
|
5
5
|
|
6
6
|
Sinatraは最小の労力でRubyによるWebアプリケーションを手早く作るための[DSL](https://ja.wikipedia.org/wiki/メインページドメイン固有言語)です。
|
7
7
|
|
@@ -70,9 +70,11 @@ ThinがあればSinatraはこれを利用するので、`gem install thin`する
|
|
70
70
|
* [名前付きテンプレート(Named Templates)](#名前付きテンプレートnamed-templates)
|
71
71
|
* [ファイル拡張子の関連付け](#ファイル拡張子の関連付け)
|
72
72
|
* [オリジナルテンプレートエンジンの追加](#オリジナルテンプレートエンジンの追加)
|
73
|
+
* [カスタムロジックを使用したテンプレートの探索](#カスタムロジックを使用したテンプレートの探索)
|
73
74
|
* [フィルタ(Filters)](#フィルタfilters)
|
74
75
|
* [ヘルパー(Helpers)](#ヘルパーhelpers)
|
75
76
|
* [セッションの使用](#セッションの使用)
|
77
|
+
* [セッションミドルウェアの選択](#セッションミドルウェアの選択)
|
76
78
|
* [停止(Halting)](#停止halting)
|
77
79
|
* [パッシング(Passing)](#パッシングpassing)
|
78
80
|
* [別ルーティングの誘発](#別ルーティングの誘発)
|
@@ -168,7 +170,6 @@ end
|
|
168
170
|
end
|
169
171
|
```
|
170
172
|
|
171
|
-
|
172
173
|
ルーティングのパターンは名前付きパラメータを含むことができ、
|
173
174
|
`params`ハッシュで取得できます。
|
174
175
|
|
@@ -379,7 +380,6 @@ get %r{(?!/index)} do
|
|
379
380
|
end
|
380
381
|
```
|
381
382
|
|
382
|
-
|
383
383
|
## 静的ファイル(Static Files)
|
384
384
|
|
385
385
|
静的ファイルは`./public`ディレクトリから配信されます。
|
@@ -427,7 +427,6 @@ end
|
|
427
427
|
|
428
428
|
Sinatraが理解できないオプションは、テンプレートエンジンに渡されることになります。
|
429
429
|
|
430
|
-
|
431
430
|
```ruby
|
432
431
|
get '/' do
|
433
432
|
haml :index, :format => :html5
|
@@ -457,7 +456,7 @@ end
|
|
457
456
|
|
458
457
|
<dt>default_encoding</dt>
|
459
458
|
<dd>
|
460
|
-
|
459
|
+
文字エンコーディングが確実でない場合に指定。デフォルトは、<tt>settings.default_encoding</tt>。
|
461
460
|
</dd>
|
462
461
|
|
463
462
|
<dt>views</dt>
|
@@ -499,9 +498,9 @@ end
|
|
499
498
|
set :views, settings.root + '/templates'
|
500
499
|
```
|
501
500
|
|
502
|
-
|
503
|
-
|
504
|
-
|
501
|
+
テンプレートの参照は、テンプレートがサブディレクトリ内にある場合でも常にシンボルで指定することを覚えておいてください。
|
502
|
+
(これは`:'subdir/template'`または`'subdir/template'.to_sym`のように指定することを意味します。)
|
503
|
+
レンダリングメソッドにシンボルではなく文字列を渡してしまうと、そのまま文字列として出力してしまいます。
|
505
504
|
|
506
505
|
### リテラルテンプレート(Literal Templates)
|
507
506
|
|
@@ -511,13 +510,19 @@ get '/' do
|
|
511
510
|
end
|
512
511
|
```
|
513
512
|
|
514
|
-
|
513
|
+
これはテンプレート文字列をレンダリングしています。
|
514
|
+
テンプレート文字列に関連するファイルパスや行数を`:path`や`:line`オプションで指定することで、バックトレースを明確にすることができます。
|
515
|
+
|
516
|
+
```ruby
|
517
|
+
get '/' do
|
518
|
+
haml '%div.title Hello World', :path => 'examples/file.haml', :line => 3
|
519
|
+
end
|
520
|
+
```
|
515
521
|
|
516
522
|
### 利用可能なテンプレート言語
|
517
523
|
|
518
524
|
いくつかの言語には複数の実装があります。使用する(そしてスレッドセーフにする)実装を指定するには、それを最初にrequireしてください。
|
519
525
|
|
520
|
-
|
521
526
|
```ruby
|
522
527
|
require 'rdiscount' # または require 'bluecloth'
|
523
528
|
get('/') { markdown :index }
|
@@ -540,7 +545,6 @@ get('/') { markdown :index }
|
|
540
545
|
</tr>
|
541
546
|
</table>
|
542
547
|
|
543
|
-
|
544
548
|
#### Erb テンプレート
|
545
549
|
|
546
550
|
<table>
|
@@ -601,7 +605,6 @@ get('/') { markdown :index }
|
|
601
605
|
|
602
606
|
インラインテンプレート用にブロックを取ることもできます(例を参照)。
|
603
607
|
|
604
|
-
|
605
608
|
#### Sass テンプレート
|
606
609
|
|
607
610
|
<table>
|
@@ -619,7 +622,6 @@ get('/') { markdown :index }
|
|
619
622
|
</tr>
|
620
623
|
</table>
|
621
624
|
|
622
|
-
|
623
625
|
#### Scss テンプレート
|
624
626
|
|
625
627
|
<table>
|
@@ -713,7 +715,6 @@ erb :overview, :locals => { :text => markdown(:introduction) }
|
|
713
715
|
|
714
716
|
MarkdownからはRubyを呼ぶことができないので、Markdownで書かれたレイアウトを使うことはできません。しかしながら、`:layout_engine`オプションを渡すことでテンプレートのものとは異なるレンダリングエンジンをレイアウトのために使うことができます。
|
715
717
|
|
716
|
-
|
717
718
|
#### Textile テンプレート
|
718
719
|
|
719
720
|
<table>
|
@@ -773,7 +774,6 @@ erb :overview, :locals => { :text => rdoc(:introduction) }
|
|
773
774
|
|
774
775
|
ノート: 他のテンプレート内で`rdoc`メソッドを呼び出せます。
|
775
776
|
|
776
|
-
|
777
777
|
```ruby
|
778
778
|
%h1 Hello From Haml!
|
779
779
|
%p= rdoc(:greetings)
|
@@ -819,7 +819,6 @@ AsciiDocテンプレートからRubyのメソッドを直接呼び出すこと
|
|
819
819
|
|
820
820
|
RadiusテンプレートからRubyのメソッドを直接呼び出すことができないため、ほぼ全ての場合にlocalsを指定する必要があるでしょう。
|
821
821
|
|
822
|
-
|
823
822
|
#### Markaby テンプレート
|
824
823
|
|
825
824
|
<table>
|
@@ -1015,7 +1014,6 @@ end
|
|
1015
1014
|
</tr>
|
1016
1015
|
</table>
|
1017
1016
|
|
1018
|
-
|
1019
1017
|
テンプレートのソースはRubyの文字列として評価され、その結果のJSON変数は`#to_json`を使って変換されます。
|
1020
1018
|
|
1021
1019
|
```ruby
|
@@ -1068,7 +1066,7 @@ get '/:id' do
|
|
1068
1066
|
end
|
1069
1067
|
```
|
1070
1068
|
|
1071
|
-
|
1069
|
+
これは他のテンプレート内で部分テンプレートとして表示する典型的な手法です。
|
1072
1070
|
|
1073
1071
|
### `yield`を伴うテンプレートとネストしたレイアウト
|
1074
1072
|
|
@@ -1105,7 +1103,6 @@ end
|
|
1105
1103
|
`liquid`, `slim `, `wlang`。
|
1106
1104
|
また汎用の`render`メソッドもブロックを取れます。
|
1107
1105
|
|
1108
|
-
|
1109
1106
|
### インラインテンプレート(Inline Templates)
|
1110
1107
|
|
1111
1108
|
テンプレートはソースファイルの最後で定義することもできます。
|
@@ -1147,7 +1144,7 @@ get '/' do
|
|
1147
1144
|
end
|
1148
1145
|
```
|
1149
1146
|
|
1150
|
-
「layout
|
1147
|
+
「layout」という名前のテンプレートが存在する場合は、そのテンプレートファイルは他のテンプレートがレンダリングされる度に使用されます。`:layout => false`で個別に、または`set :haml, :layout => false`でデフォルトとして、レイアウトを無効にすることができます。
|
1151
1148
|
|
1152
1149
|
```ruby
|
1153
1150
|
get '/' do
|
@@ -1181,6 +1178,22 @@ end
|
|
1181
1178
|
|
1182
1179
|
これは、`./views/index.myat`をレンダリングします。Tiltについての詳細は、https://github.com/rtomayko/tilt を参照してください。
|
1183
1180
|
|
1181
|
+
### カスタムロジックを使用したテンプレートの探索
|
1182
|
+
|
1183
|
+
オリジナルテンプレートの検索メカニズムを実装するためには、`#find_template`メソッドを実装します。
|
1184
|
+
|
1185
|
+
```ruby
|
1186
|
+
configure do
|
1187
|
+
set :views [ './views/a', './views/b' ]
|
1188
|
+
end
|
1189
|
+
|
1190
|
+
def find_template(views, name, engine, &block)
|
1191
|
+
Array(views).each do |v|
|
1192
|
+
super(v, name, engine, &block)
|
1193
|
+
end
|
1194
|
+
end
|
1195
|
+
```
|
1196
|
+
|
1184
1197
|
## フィルタ(Filters)
|
1185
1198
|
|
1186
1199
|
beforeフィルタは、リクエストのルーティングと同じコンテキストで各リクエストの前に評価され、それによってリクエストとレスポンスを変更可能にします。フィルタ内でセットされたインスタンス変数はルーティングとテンプレートからアクセスすることができます。
|
@@ -1263,10 +1276,9 @@ helpers FooUtils, BarUtils
|
|
1263
1276
|
|
1264
1277
|
その効果は、アプリケーションクラスにモジュールをインクルードするのと同じです。
|
1265
1278
|
|
1266
|
-
|
1267
1279
|
### セッションの使用
|
1268
1280
|
|
1269
|
-
|
1281
|
+
セッションはリクエスト間での状態維持のために使用されます。セッションを有効化すると、ユーザセッションごとに一つのセッションハッシュが与えられます。
|
1270
1282
|
|
1271
1283
|
```ruby
|
1272
1284
|
enable :sessions
|
@@ -1312,6 +1324,38 @@ foo.comのサブドメイン上のアプリ間でセッションを共有化し
|
|
1312
1324
|
set :sessions, :domain => '.foo.com'
|
1313
1325
|
```
|
1314
1326
|
|
1327
|
+
#### セッションミドルウェアの選択
|
1328
|
+
|
1329
|
+
`enable :sessions`とすることで、クッキー内の全てのデータを実際に保存してしまうことに注意してください。
|
1330
|
+
これは、あなたが望む挙動ではない(例えば、大量のデータを保存することでトラフィックが増大してしまう)かもしれません。
|
1331
|
+
あなたは、次のいずれかの方法によって、任意のRackセッションミドルウェアを使用することができます。
|
1332
|
+
|
1333
|
+
```ruby
|
1334
|
+
enable :sessions
|
1335
|
+
set :session_store, Rack::Session::Pool
|
1336
|
+
```
|
1337
|
+
|
1338
|
+
オプションのハッシュを設定するためには、次のようにします。
|
1339
|
+
|
1340
|
+
```ruby
|
1341
|
+
set :sessions, :expire_after => 2592000
|
1342
|
+
set :session_store, Rack::Session::Pool
|
1343
|
+
```
|
1344
|
+
|
1345
|
+
他の方法は`enable :sessions`を**しない**で、他のミドルウェアの選択と同様にあなた自身でミドルウェアを選択することです。
|
1346
|
+
|
1347
|
+
この方法を選択する場合は、セッションベースの保護は**デフォルトで有効にならない**ということに注意することが重要です。
|
1348
|
+
|
1349
|
+
これを満たすためのRackミドルウェアを追加することが必要になります。
|
1350
|
+
|
1351
|
+
```ruby
|
1352
|
+
use Rack::Session::Pool, :expire_after => 2592000
|
1353
|
+
use Rack::Protection::RemoteToken
|
1354
|
+
use Rack::Protection::SessionHijacking
|
1355
|
+
```
|
1356
|
+
|
1357
|
+
より詳しい情報は、「攻撃防御に対する設定」の項を参照してください。
|
1358
|
+
|
1315
1359
|
### 停止(Halting)
|
1316
1360
|
|
1317
1361
|
フィルタまたはルーティング内で直ちにリクエストを止める場合
|
@@ -1369,7 +1413,8 @@ end
|
|
1369
1413
|
|
1370
1414
|
### 別ルーティングの誘発
|
1371
1415
|
|
1372
|
-
`pass
|
1416
|
+
`pass`を使ってルーティングを飛ばすのではなく、他のルーティングを呼んだ結果を得たいという場合があります。
|
1417
|
+
これは`call`を使用することで実現できます。
|
1373
1418
|
|
1374
1419
|
```ruby
|
1375
1420
|
get '/foo' do
|
@@ -1382,12 +1427,11 @@ get '/bar' do
|
|
1382
1427
|
end
|
1383
1428
|
```
|
1384
1429
|
|
1385
|
-
ノート: 先の例において、テストを楽にしパフォーマンスを改善するには、`"bar"`を単にヘルパーに移し、`/foo`および`/bar
|
1430
|
+
ノート: 先の例において、テストを楽にしパフォーマンスを改善するには、`"bar"`を単にヘルパーに移し、`/foo`および`/bar`から使えるようにしたほうが良いです。
|
1386
1431
|
|
1387
1432
|
リクエストが、その複製物でない同じアプリケーションのインスタンスに送られるようにしたいときは、`call`に代えて`call!`を使ってください。
|
1388
1433
|
|
1389
|
-
`call`についての詳細はRack
|
1390
|
-
|
1434
|
+
`call`についての詳細はRackの仕様を参照してください。
|
1391
1435
|
|
1392
1436
|
### ボディ、ステータスコードおよびヘッダの設定
|
1393
1437
|
|
@@ -1476,7 +1520,6 @@ end
|
|
1476
1520
|
|
1477
1521
|
リクエストスコープにおいて、`logger`ヘルパーは`Logger`インスタンスを作り出します。
|
1478
1522
|
|
1479
|
-
|
1480
1523
|
```ruby
|
1481
1524
|
get '/' do
|
1482
1525
|
logger.info "loading data"
|
@@ -1500,7 +1543,7 @@ end
|
|
1500
1543
|
|
1501
1544
|
### MIMEタイプ(Mime Types)
|
1502
1545
|
|
1503
|
-
`send_file`か静的ファイルを使う時、SinatraがMIMEタイプを理解できない場合があります。その時は `mime_type`
|
1546
|
+
`send_file`か静的ファイルを使う時、SinatraがMIMEタイプを理解できない場合があります。その時は `mime_type` を使ってファイル拡張子毎に登録してください。
|
1504
1547
|
|
1505
1548
|
```ruby
|
1506
1549
|
configure do
|
@@ -1561,7 +1604,6 @@ end
|
|
1561
1604
|
|
1562
1605
|
redirectに引数を渡すには、それをクエリーに追加するか、
|
1563
1606
|
|
1564
|
-
|
1565
1607
|
```ruby
|
1566
1608
|
redirect to('/bar?sum=42')
|
1567
1609
|
```
|
@@ -1629,7 +1671,6 @@ etag @article.sha1, :weak
|
|
1629
1671
|
|
1630
1672
|
これらのヘルパーは、キャッシングをしてくれませんが、必要な情報をキャッシュに与えてくれます。もし手早いリバースプロキシキャッシングの解決策をお探しなら、 [rack-cache](https://github.com/rtomayko/rack-cache)を試してください。
|
1631
1673
|
|
1632
|
-
|
1633
1674
|
```ruby
|
1634
1675
|
require "rack/cache"
|
1635
1676
|
require "sinatra"
|
@@ -1706,7 +1747,6 @@ send_file 'foo.png', :type => :jpg
|
|
1706
1747
|
</dd>
|
1707
1748
|
</dl>
|
1708
1749
|
|
1709
|
-
|
1710
1750
|
### リクエストオブジェクトへのアクセス
|
1711
1751
|
|
1712
1752
|
受信するリクエストオブジェクトは、`request`メソッドを通じてリクエストレベル(フィルタ、ルーティング、エラーハンドラ)からアクセスすることができます。
|
@@ -1911,18 +1951,19 @@ end
|
|
1911
1951
|
|
1912
1952
|
### 攻撃防御に対する設定
|
1913
1953
|
|
1914
|
-
Sinatra
|
1954
|
+
Sinatraは[Rack::Protection](https://github.com/sinatra/rack-protection#readme)を使用することで、アプリケーションを一般的な日和見的攻撃から守っています。これは簡単に無効化できます(が、アプリケーションに大量の一般的な脆弱性を埋め込むことになってしまいます)。
|
1915
1955
|
|
1916
1956
|
```ruby
|
1917
1957
|
disable :protection
|
1918
1958
|
```
|
1919
1959
|
|
1920
|
-
|
1960
|
+
ある1つの防御を無効にするには、`protection`にハッシュでオプションを指定します。
|
1921
1961
|
|
1922
1962
|
```ruby
|
1923
1963
|
set :protection, :except => :path_traversal
|
1924
1964
|
```
|
1925
|
-
|
1965
|
+
|
1966
|
+
配列を渡すことで、複数の防御を無効にすることもできます。
|
1926
1967
|
|
1927
1968
|
```ruby
|
1928
1969
|
set :protection, :except => [:path_traversal, :session_hijacking]
|
@@ -2081,7 +2122,7 @@ set :protection, :session => true
|
|
2081
2122
|
|
2082
2123
|
## 環境設定(Environments)
|
2083
2124
|
|
2084
|
-
|
2125
|
+
3種類の既定環境、`"development"`、`"production"`および`"test"`があります。環境は、`APP_ENV`環境変数を通して設定できます。デフォルト値は、`"development"`です。`"development"`環境において、すべてのテンプレートは、各リクエスト間で再ロードされ、そして、特別の`not_found`および`error`ハンドラがブラウザにスタックトレースを表示します。`"production"`および`"test"`環境においては、テンプレートはデフォルトでキャッシュされます。
|
2085
2126
|
|
2086
2127
|
異なる環境を走らせるには、`APP_ENV`環境変数を設定します。
|
2087
2128
|
|
@@ -2117,7 +2158,14 @@ end
|
|
2117
2158
|
|
2118
2159
|
### エラー(Error)
|
2119
2160
|
|
2120
|
-
`error
|
2161
|
+
`error`ハンドラはルーティングブロックまたはフィルタ内で例外が発生したときはいつでも発動します。
|
2162
|
+
しかし、環境設定がdevelopmentの場合は`:after_handler`を設定している場合のみ発動するようになります。
|
2163
|
+
|
2164
|
+
```ruby
|
2165
|
+
set :show_exceptions, :after_handler
|
2166
|
+
```
|
2167
|
+
|
2168
|
+
例外オブジェクトはRack変数`sinatra.error`から取得できます。
|
2121
2169
|
|
2122
2170
|
```ruby
|
2123
2171
|
error do
|
@@ -2169,7 +2217,6 @@ end
|
|
2169
2217
|
|
2170
2218
|
Sinatraを開発環境の下で実行している場合は、特別な`not_found`および`error`ハンドラが導入され、これは親切なスタックトレースと追加のデバッギング情報をブラウザに表示します。
|
2171
2219
|
|
2172
|
-
|
2173
2220
|
## Rackミドルウェア(Rack Middleware)
|
2174
2221
|
|
2175
2222
|
SinatraはRuby製Webフレームワークのミニマルな標準的インタフェースである[Rack](http://rack.github.io/)上に構築されています。アプリケーションデベロッパーにとってRackにおける最も興味深い機能は、「ミドルウェア(middleware)」をサポートしていることであり、これは、サーバとアプリケーションとの間に置かれ、HTTPリクエスト/レスポンスを監視および/または操作することで、各種の汎用的機能を提供するコンポーネントです。
|
@@ -2256,13 +2303,13 @@ class MyApp < Sinatra::Base
|
|
2256
2303
|
end
|
2257
2304
|
```
|
2258
2305
|
|
2259
|
-
`Sinatra::Base`のサブクラスで利用できるメソッドは、トップレベルDSL
|
2306
|
+
`Sinatra::Base`のサブクラスで利用できるメソッドは、トップレベルDSLで利用できるものと全く同じです。ほとんどのトップレベルで記述されたアプリは、以下の2点を修正することで`Sinatra::Base`コンポーネントに変えることができます。
|
2260
2307
|
|
2261
2308
|
* `sinatra`の代わりに`sinatra/base`を読み込む
|
2262
2309
|
(そうしない場合、SinatraのDSLメソッドの全てがmainの名前空間にインポートされます)
|
2263
2310
|
* ルーティング、エラーハンドラ、フィルタ、オプションを`Sinatra::Base`のサブクラスに書く
|
2264
2311
|
|
2265
|
-
`Sinatra::Base`はまっさらです。ビルトインサーバを含む、ほとんどのオプションがデフォルトで無効になっています。利用可能なオプションとその挙動の詳細については[Configuring Settings](http://www.sinatrarb.com/configuration.html)(英語)
|
2312
|
+
`Sinatra::Base`はまっさらです。ビルトインサーバを含む、ほとんどのオプションがデフォルトで無効になっています。利用可能なオプションとその挙動の詳細については[Configuring Settings](http://www.sinatrarb.com/configuration.html)(英語)をご覧ください。
|
2266
2313
|
|
2267
2314
|
もしもクラシックスタイルと同じような挙動のアプリケーションをトップレベルで定義させる必要があれば、`Sinatra::Application`をサブクラス化させてください。
|
2268
2315
|
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Sinatra
|
2
2
|
|
3
|
-
[![Build Status](https://secure.travis-ci.org/sinatra/sinatra.
|
3
|
+
[![Build Status](https://secure.travis-ci.org/sinatra/sinatra.svg)](http://travis-ci.org/sinatra/sinatra)
|
4
4
|
|
5
5
|
Sinatra is a [DSL](https://en.wikipedia.org/wiki/Domain-specific_language) for
|
6
6
|
quickly creating web applications in Ruby with minimal effort:
|
@@ -75,7 +75,9 @@ pick up if available.
|
|
75
75
|
* [Filters](#filters)
|
76
76
|
* [Helpers](#helpers)
|
77
77
|
* [Using Sessions](#using-sessions)
|
78
|
-
|
78
|
+
* [Session Secret Security](#session-secret-security)
|
79
|
+
* [Session Config](#session-config)
|
80
|
+
* [Choosing Your Own Session Middleware](#choosing-your-own-session-middleware)
|
79
81
|
* [Halting](#halting)
|
80
82
|
* [Passing](#passing)
|
81
83
|
* [Triggering Another Route](#triggering-another-route)
|
@@ -116,8 +118,6 @@ pick up if available.
|
|
116
118
|
* [Requirement](#requirement)
|
117
119
|
* [The Bleeding Edge](#the-bleeding-edge)
|
118
120
|
* [With Bundler](#with-bundler)
|
119
|
-
* [Roll Your Own](#roll-your-own)
|
120
|
-
* [Install Globally](#install-globally)
|
121
121
|
* [Versioning](#versioning)
|
122
122
|
* [Further Reading](#further-reading)
|
123
123
|
|
@@ -166,9 +166,9 @@ matches the request is invoked.
|
|
166
166
|
Routes with trailing slashes are different from the ones without:
|
167
167
|
|
168
168
|
```ruby
|
169
|
-
|
170
|
-
|
171
|
-
|
169
|
+
get '/foo' do
|
170
|
+
# Does not match "GET /foo/"
|
171
|
+
end
|
172
172
|
```
|
173
173
|
|
174
174
|
Route patterns may include named parameters, accessible via the
|
@@ -252,8 +252,23 @@ get '/posts' do
|
|
252
252
|
end
|
253
253
|
```
|
254
254
|
|
255
|
-
By the way, unless you disable the path traversal attack protection (see
|
256
|
-
the request path might be modified before matching against your
|
255
|
+
By the way, unless you disable the path traversal attack protection (see
|
256
|
+
below), the request path might be modified before matching against your
|
257
|
+
routes.
|
258
|
+
|
259
|
+
You may customize the Mustermann options used for a given route by passing in a
|
260
|
+
`:mustermann_opts` hash:
|
261
|
+
|
262
|
+
```ruby
|
263
|
+
get '\A/posts\z', :mustermann_opts => { :type => :regexp, :check_anchors => false } do
|
264
|
+
# matches /posts exactly, with explicit anchoring
|
265
|
+
"If you match an anchored pattern clap your hands!"
|
266
|
+
end
|
267
|
+
```
|
268
|
+
|
269
|
+
It looks like a [condition](#conditions), but it isn't one! These options will
|
270
|
+
be merged into the global `:mustermann_opts` hash described
|
271
|
+
[below](#available-settings).
|
257
272
|
|
258
273
|
## Conditions
|
259
274
|
|
@@ -322,10 +337,10 @@ end
|
|
322
337
|
|
323
338
|
## Return Values
|
324
339
|
|
325
|
-
The return value of a route block determines at least the response body
|
326
|
-
on to the HTTP client, or at least the next middleware in the
|
327
|
-
Most commonly, this is a string, as in the above examples.
|
328
|
-
also accepted.
|
340
|
+
The return value of a route block determines at least the response body
|
341
|
+
passed on to the HTTP client, or at least the next middleware in the
|
342
|
+
Rack stack. Most commonly, this is a string, as in the above examples.
|
343
|
+
But other values are also accepted.
|
329
344
|
|
330
345
|
You can return any object that would either be a valid Rack response, Rack
|
331
346
|
body object or HTTP status code:
|
@@ -350,14 +365,14 @@ end
|
|
350
365
|
get('/') { Stream.new }
|
351
366
|
```
|
352
367
|
|
353
|
-
You can also use the `stream` helper method (described below) to reduce
|
354
|
-
plate and embed the streaming logic in the route.
|
368
|
+
You can also use the `stream` helper method (described below) to reduce
|
369
|
+
boiler plate and embed the streaming logic in the route.
|
355
370
|
|
356
371
|
## Custom Route Matchers
|
357
372
|
|
358
|
-
As shown above, Sinatra ships with built-in support for using String
|
359
|
-
and regular expressions as route matches. However, it does not
|
360
|
-
can easily define your own matchers:
|
373
|
+
As shown above, Sinatra ships with built-in support for using String
|
374
|
+
patterns and regular expressions as route matches. However, it does not
|
375
|
+
stop there. You can easily define your own matchers:
|
361
376
|
|
362
377
|
```ruby
|
363
378
|
class AllButPattern
|
@@ -505,15 +520,17 @@ Available Options:
|
|
505
520
|
|
506
521
|
<dt>scope</dt>
|
507
522
|
<dd>
|
508
|
-
Scope to render template under. Defaults to the application
|
509
|
-
change this, instance variables and helper methods
|
523
|
+
Scope to render template under. Defaults to the application
|
524
|
+
instance. If you change this, instance variables and helper methods
|
525
|
+
will not be available.
|
510
526
|
</dd>
|
511
527
|
|
512
528
|
<dt>layout_engine</dt>
|
513
529
|
<dd>
|
514
|
-
Template engine to use for rendering the layout. Useful for
|
515
|
-
do not support layouts otherwise. Defaults to the
|
516
|
-
template. Example: <tt>set :rdoc, :layout_engine
|
530
|
+
Template engine to use for rendering the layout. Useful for
|
531
|
+
languages that do not support layouts otherwise. Defaults to the
|
532
|
+
engine used for the template. Example: <tt>set :rdoc, :layout_engine
|
533
|
+
=> :erb</tt>
|
517
534
|
</dd>
|
518
535
|
|
519
536
|
<dt>layout_options</dt>
|
@@ -523,19 +540,19 @@ Available Options:
|
|
523
540
|
</dd>
|
524
541
|
</dl>
|
525
542
|
|
526
|
-
Templates are assumed to be located directly under the `./views`
|
527
|
-
use a different views directory:
|
543
|
+
Templates are assumed to be located directly under the `./views`
|
544
|
+
directory. To use a different views directory:
|
528
545
|
|
529
546
|
```ruby
|
530
547
|
set :views, settings.root + '/templates'
|
531
548
|
```
|
532
549
|
|
533
550
|
|
534
|
-
One important thing to remember is that you always have to reference
|
535
|
-
with symbols, even if they're in a subdirectory (in this case,
|
536
|
-
`:'subdir/template'` or `'subdir/template'.to_sym`). You must use a
|
537
|
-
because otherwise rendering methods will render any strings
|
538
|
-
directly.
|
551
|
+
One important thing to remember is that you always have to reference
|
552
|
+
templates with symbols, even if they're in a subdirectory (in this case,
|
553
|
+
use: `:'subdir/template'` or `'subdir/template'.to_sym`). You must use a
|
554
|
+
symbol because otherwise rendering methods will render any strings
|
555
|
+
passed to them directly.
|
539
556
|
|
540
557
|
### Literal Templates
|
541
558
|
|
@@ -545,9 +562,9 @@ get '/' do
|
|
545
562
|
end
|
546
563
|
```
|
547
564
|
|
548
|
-
Renders the template string.
|
549
|
-
|
550
|
-
|
565
|
+
Renders the template string. You can optionally specify `:path` and
|
566
|
+
`:line` for a clearer backtrace if there is a filesystem path or line
|
567
|
+
associated with that string:
|
551
568
|
|
552
569
|
```ruby
|
553
570
|
get '/' do
|
@@ -745,7 +762,8 @@ engine:
|
|
745
762
|
erb :overview, :locals => { :text => markdown(:introduction) }
|
746
763
|
```
|
747
764
|
|
748
|
-
Note that you may also call the `markdown` method from within other
|
765
|
+
Note that you may also call the `markdown` method from within other
|
766
|
+
templates:
|
749
767
|
|
750
768
|
```ruby
|
751
769
|
%h1 Hello From Haml!
|
@@ -773,8 +791,9 @@ template than for the layout by passing the `:layout_engine` option.
|
|
773
791
|
</tr>
|
774
792
|
</table>
|
775
793
|
|
776
|
-
It is not possible to call methods from Textile, nor to pass locals to
|
777
|
-
therefore will usually use it in combination with another
|
794
|
+
It is not possible to call methods from Textile, nor to pass locals to
|
795
|
+
it. You therefore will usually use it in combination with another
|
796
|
+
rendering engine:
|
778
797
|
|
779
798
|
```ruby
|
780
799
|
erb :overview, :locals => { :text => textile(:introduction) }
|
@@ -863,8 +882,8 @@ almost always want to pass locals to it.
|
|
863
882
|
</tr>
|
864
883
|
</table>
|
865
884
|
|
866
|
-
Since you cannot call Ruby methods directly from a Radius template, you
|
867
|
-
always want to pass locals to it.
|
885
|
+
Since you cannot call Ruby methods directly from a Radius template, you
|
886
|
+
almost always want to pass locals to it.
|
868
887
|
|
869
888
|
#### Markaby Templates
|
870
889
|
|
@@ -971,15 +990,16 @@ template than for the layout by passing the `:layout_engine` option.
|
|
971
990
|
</tr>
|
972
991
|
</table>
|
973
992
|
|
974
|
-
It is not possible to call methods from MediaWiki markup, nor to pass
|
975
|
-
it. You therefore will usually use it in combination with
|
976
|
-
engine:
|
993
|
+
It is not possible to call methods from MediaWiki markup, nor to pass
|
994
|
+
locals to it. You therefore will usually use it in combination with
|
995
|
+
another rendering engine:
|
977
996
|
|
978
997
|
```ruby
|
979
998
|
erb :overview, :locals => { :text => mediawiki(:introduction) }
|
980
999
|
```
|
981
1000
|
|
982
|
-
Note that you may also call the `mediawiki` method from within other
|
1001
|
+
Note that you may also call the `mediawiki` method from within other
|
1002
|
+
templates:
|
983
1003
|
|
984
1004
|
```ruby
|
985
1005
|
%h1 Hello From Haml!
|
@@ -1109,8 +1129,9 @@ present(resource);
|
|
1109
1129
|
</tr>
|
1110
1130
|
</table>
|
1111
1131
|
|
1112
|
-
Since calling ruby methods is not idiomatic in WLang, you almost always
|
1113
|
-
pass locals to it. Layouts written in WLang and `yield` are
|
1132
|
+
Since calling ruby methods is not idiomatic in WLang, you almost always
|
1133
|
+
want to pass locals to it. Layouts written in WLang and `yield` are
|
1134
|
+
supported, though.
|
1114
1135
|
|
1115
1136
|
### Accessing Variables in Templates
|
1116
1137
|
|
@@ -1150,7 +1171,8 @@ end
|
|
1150
1171
|
|
1151
1172
|
This code is mostly equivalent to `erb :index, :layout => :post`.
|
1152
1173
|
|
1153
|
-
Passing blocks to rendering methods is most useful for creating nested
|
1174
|
+
Passing blocks to rendering methods is most useful for creating nested
|
1175
|
+
layouts:
|
1154
1176
|
|
1155
1177
|
```ruby
|
1156
1178
|
erb :main_layout, :layout => false do
|
@@ -1273,8 +1295,8 @@ end
|
|
1273
1295
|
|
1274
1296
|
## Filters
|
1275
1297
|
|
1276
|
-
Before filters are evaluated before each request within the same
|
1277
|
-
|
1298
|
+
Before filters are evaluated before each request within the same context
|
1299
|
+
as the routes will be and can modify the request and response. Instance
|
1278
1300
|
variables set in filters are accessible by routes and templates:
|
1279
1301
|
|
1280
1302
|
```ruby
|
@@ -1289,9 +1311,10 @@ get '/foo/*' do
|
|
1289
1311
|
end
|
1290
1312
|
```
|
1291
1313
|
|
1292
|
-
After filters are evaluated after each request within the same context
|
1293
|
-
routes will be and can also modify the request and response.
|
1294
|
-
set in before filters and routes are accessible by
|
1314
|
+
After filters are evaluated after each request within the same context
|
1315
|
+
as the routes will be and can also modify the request and response.
|
1316
|
+
Instance variables set in before filters and routes are accessible by
|
1317
|
+
after filters:
|
1295
1318
|
|
1296
1319
|
```ruby
|
1297
1320
|
after do
|
@@ -1299,9 +1322,9 @@ after do
|
|
1299
1322
|
end
|
1300
1323
|
```
|
1301
1324
|
|
1302
|
-
Note: Unless you use the `body` method rather than just returning a
|
1303
|
-
the routes, the body will not yet be available in the after
|
1304
|
-
generated later on.
|
1325
|
+
Note: Unless you use the `body` method rather than just returning a
|
1326
|
+
String from the routes, the body will not yet be available in the after
|
1327
|
+
filter, since it is generated later on.
|
1305
1328
|
|
1306
1329
|
Filters optionally take a pattern, causing them to be evaluated only if the
|
1307
1330
|
request path matches that pattern:
|
@@ -1378,17 +1401,83 @@ get '/:value' do
|
|
1378
1401
|
end
|
1379
1402
|
```
|
1380
1403
|
|
1404
|
+
#### Session Secret Security
|
1405
|
+
|
1381
1406
|
To improve security, the session data in the cookie is signed with a session
|
1382
|
-
secret
|
1383
|
-
|
1384
|
-
|
1407
|
+
secret using `HMAC-SHA1`. This session secret should optimally be a
|
1408
|
+
cryptographically secure random value of an appropriate length which for
|
1409
|
+
`HMAC-SHA1` is greater than or equal to 64 bytes (512 bits, 128 hex
|
1410
|
+
characters). You would be advised not to use a secret that is less than 32
|
1411
|
+
bytes of randomness (256 bits, 64 hex characters). It is therefore **very
|
1412
|
+
important** that you don't just make the secret up, but instead use a secure
|
1413
|
+
random number generator to create it. Humans are extremely bad at generating
|
1414
|
+
random values.
|
1415
|
+
|
1416
|
+
By default, a 32 byte secure random session secret is generated for you by
|
1417
|
+
Sinatra, but it will change with every restart of your application. If you
|
1418
|
+
have multiple instances of your application, and you let Sinatra generate the
|
1419
|
+
key, each instance would then have a different session key which is probably
|
1420
|
+
not what you want.
|
1421
|
+
|
1422
|
+
For better security and usability it's
|
1423
|
+
[recommended](https://12factor.net/config) that you generate a secure random
|
1424
|
+
secret and store it in an environment variable on each host running your
|
1425
|
+
application so that all of your application instances will share the same
|
1426
|
+
secret. You should periodically rotate this session secret to a new value.
|
1427
|
+
Here are some examples of how you might create a 64 byte secret and set it:
|
1428
|
+
|
1429
|
+
**Session Secret Generation**
|
1430
|
+
|
1431
|
+
```text
|
1432
|
+
$ ruby -e "require 'securerandom'; puts SecureRandom.hex(64)"
|
1433
|
+
99ae8af...snip...ec0f262ac
|
1434
|
+
```
|
1435
|
+
|
1436
|
+
**Session Secret Generation (Bonus Points)**
|
1437
|
+
|
1438
|
+
Use the [sysrandom gem](https://github.com/cryptosphere/sysrandom) to prefer
|
1439
|
+
use of system RNG facilities to generate random values instead of
|
1440
|
+
userspace `OpenSSL` which MRI Ruby currently defaults to:
|
1441
|
+
|
1442
|
+
```text
|
1443
|
+
$ gem install sysrandom
|
1444
|
+
Building native extensions. This could take a while...
|
1445
|
+
Successfully installed sysrandom-1.x
|
1446
|
+
1 gem installed
|
1447
|
+
|
1448
|
+
$ ruby -e "require 'sysrandom/securerandom'; puts SecureRandom.hex(64)"
|
1449
|
+
99ae8af...snip...ec0f262ac
|
1450
|
+
```
|
1451
|
+
|
1452
|
+
**Session Secret Environment Variable**
|
1453
|
+
|
1454
|
+
Set a `SESSION_SECRET` environment variable for Sinatra to the value you
|
1455
|
+
generated. Make this value persistent across reboots of your host. Since the
|
1456
|
+
method for doing this will vary across systems this is for illustrative
|
1457
|
+
purposes only:
|
1458
|
+
|
1459
|
+
```bash
|
1460
|
+
# echo "export SESSION_SECRET=99ae8af...snip...ec0f262ac" >> ~/.bashrc
|
1461
|
+
```
|
1462
|
+
|
1463
|
+
**Session Secret App Config**
|
1464
|
+
|
1465
|
+
Setup your app config to fail-safe to a secure random secret
|
1466
|
+
if the `SESSION_SECRET` environment variable is not available.
|
1467
|
+
|
1468
|
+
For bonus points use the [sysrandom
|
1469
|
+
gem](https://github.com/cryptosphere/sysrandom) here as well:
|
1385
1470
|
|
1386
1471
|
```ruby
|
1387
|
-
|
1472
|
+
require 'securerandom'
|
1473
|
+
# -or- require 'sysrandom/securerandom'
|
1474
|
+
set :session_secret, ENV.fetch('SESSION_SECRET') { SecureRandom.hex(64) }
|
1388
1475
|
```
|
1389
1476
|
|
1390
|
-
|
1391
|
-
|
1477
|
+
#### Session Config
|
1478
|
+
|
1479
|
+
If you want to configure it further, you may also store a hash with options
|
1480
|
+
in the `sessions` setting:
|
1392
1481
|
|
1393
1482
|
```ruby
|
1394
1483
|
set :sessions, :domain => 'foo.com'
|
@@ -1405,7 +1494,7 @@ set :sessions, :domain => '.foo.com'
|
|
1405
1494
|
|
1406
1495
|
Note that `enable :sessions` actually stores all data in a cookie. This
|
1407
1496
|
might not always be what you want (storing lots of data will increase your
|
1408
|
-
traffic, for instance). You can use any Rack session middleware
|
1497
|
+
traffic, for instance). You can use any Rack session middleware in order to
|
1409
1498
|
do so, one of the following methods can be used:
|
1410
1499
|
|
1411
1500
|
```ruby
|
@@ -1420,10 +1509,11 @@ set :sessions, :expire_after => 2592000
|
|
1420
1509
|
set :session_store, Rack::Session::Pool
|
1421
1510
|
```
|
1422
1511
|
|
1423
|
-
Another option is to **not** call `enable :sessions`, but instead pull in
|
1424
|
-
middleware of choice as you would any other middleware.
|
1512
|
+
Another option is to **not** call `enable :sessions`, but instead pull in
|
1513
|
+
your middleware of choice as you would any other middleware.
|
1425
1514
|
|
1426
|
-
It is important to note that when using this method, session based
|
1515
|
+
It is important to note that when using this method, session based
|
1516
|
+
protection **will not be enabled by default**.
|
1427
1517
|
|
1428
1518
|
The Rack middleware to do that will also need to be added:
|
1429
1519
|
|
@@ -1493,8 +1583,8 @@ matching route. If no matching route is found, a 404 is returned.
|
|
1493
1583
|
|
1494
1584
|
### Triggering Another Route
|
1495
1585
|
|
1496
|
-
Sometimes `pass` is not what you want, instead you would like to get the
|
1497
|
-
of calling another route. Simply use `call` to achieve this:
|
1586
|
+
Sometimes `pass` is not what you want, instead you would like to get the
|
1587
|
+
result of calling another route. Simply use `call` to achieve this:
|
1498
1588
|
|
1499
1589
|
```ruby
|
1500
1590
|
get '/foo' do
|
@@ -1507,21 +1597,22 @@ get '/bar' do
|
|
1507
1597
|
end
|
1508
1598
|
```
|
1509
1599
|
|
1510
|
-
Note that in the example above, you would ease testing and increase
|
1511
|
-
by simply moving `"bar"` into a helper used by both `/foo` and
|
1600
|
+
Note that in the example above, you would ease testing and increase
|
1601
|
+
performance by simply moving `"bar"` into a helper used by both `/foo` and
|
1602
|
+
`/bar`.
|
1512
1603
|
|
1513
|
-
If you want the request to be sent to the same application instance rather
|
1514
|
-
a duplicate, use `call!` instead of `call`.
|
1604
|
+
If you want the request to be sent to the same application instance rather
|
1605
|
+
than a duplicate, use `call!` instead of `call`.
|
1515
1606
|
|
1516
1607
|
Check out the Rack specification if you want to learn more about `call`.
|
1517
1608
|
|
1518
1609
|
### Setting Body, Status Code and Headers
|
1519
1610
|
|
1520
|
-
It is possible and recommended to set the status code and response body with
|
1521
|
-
return value of the route block. However, in some scenarios you might
|
1522
|
-
set the body at an arbitrary point in the execution flow. You can do
|
1523
|
-
`body` helper method. If you do so, you can use that method from
|
1524
|
-
access the body:
|
1611
|
+
It is possible and recommended to set the status code and response body with
|
1612
|
+
the return value of the route block. However, in some scenarios you might
|
1613
|
+
want to set the body at an arbitrary point in the execution flow. You can do
|
1614
|
+
so with the `body` helper method. If you do so, you can use that method from
|
1615
|
+
there on to access the body:
|
1525
1616
|
|
1526
1617
|
```ruby
|
1527
1618
|
get '/foo' do
|
@@ -1572,15 +1663,15 @@ end
|
|
1572
1663
|
|
1573
1664
|
This allows you to implement streaming APIs,
|
1574
1665
|
[Server Sent Events](https://w3c.github.io/eventsource/), and can be used as
|
1575
|
-
the basis for [WebSockets](https://en.wikipedia.org/wiki/WebSocket). It can
|
1576
|
-
used to increase throughput if some but not all content depends on a
|
1577
|
-
resource.
|
1666
|
+
the basis for [WebSockets](https://en.wikipedia.org/wiki/WebSocket). It can
|
1667
|
+
also be used to increase throughput if some but not all content depends on a
|
1668
|
+
slow resource.
|
1578
1669
|
|
1579
|
-
Note that the streaming behavior, especially the number of concurrent
|
1580
|
-
highly depends on the web server used to serve the application.
|
1581
|
-
might not even support streaming at all. If the server does not
|
1582
|
-
streaming, the body will be sent all at once after the block passed
|
1583
|
-
finishes executing. Streaming does not work at all with Shotgun.
|
1670
|
+
Note that the streaming behavior, especially the number of concurrent
|
1671
|
+
requests, highly depends on the web server used to serve the application.
|
1672
|
+
Some servers might not even support streaming at all. If the server does not
|
1673
|
+
support streaming, the body will be sent all at once after the block passed
|
1674
|
+
to `stream` finishes executing. Streaming does not work at all with Shotgun.
|
1584
1675
|
|
1585
1676
|
If the optional parameter is set to `keep_open`, it will not call `close` on
|
1586
1677
|
the stream object, allowing you to close it at any later point in the
|
@@ -1616,9 +1707,9 @@ post '/:message' do
|
|
1616
1707
|
end
|
1617
1708
|
```
|
1618
1709
|
|
1619
|
-
It's also possible for the client to close the connection when trying to
|
1620
|
-
to the socket. Because of this, it's recommended to check
|
1621
|
-
trying to write.
|
1710
|
+
It's also possible for the client to close the connection when trying to
|
1711
|
+
write to the socket. Because of this, it's recommended to check
|
1712
|
+
`out.closed?` before trying to write.
|
1622
1713
|
|
1623
1714
|
### Logging
|
1624
1715
|
|
@@ -1635,8 +1726,8 @@ This logger will automatically take your Rack handler's logging settings into
|
|
1635
1726
|
account. If logging is disabled, this method will return a dummy object, so
|
1636
1727
|
you do not have to worry about it in your routes and filters.
|
1637
1728
|
|
1638
|
-
Note that logging is only enabled for `Sinatra::Application` by default, so
|
1639
|
-
you inherit from `Sinatra::Base`, you probably want to enable it yourself:
|
1729
|
+
Note that logging is only enabled for `Sinatra::Application` by default, so
|
1730
|
+
if you inherit from `Sinatra::Base`, you probably want to enable it yourself:
|
1640
1731
|
|
1641
1732
|
```ruby
|
1642
1733
|
class MyApp < Sinatra::Base
|
@@ -1788,8 +1879,9 @@ etag @article.sha1, :weak
|
|
1788
1879
|
```
|
1789
1880
|
|
1790
1881
|
These helpers will not do any caching for you, but rather feed the necessary
|
1791
|
-
information to your cache. If you are looking for a quick
|
1792
|
-
solution, try
|
1882
|
+
information to your cache. If you are looking for a quick
|
1883
|
+
reverse-proxy caching solution, try
|
1884
|
+
[rack-cache](https://github.com/rtomayko/rack-cache):
|
1793
1885
|
|
1794
1886
|
```ruby
|
1795
1887
|
require "rack/cache"
|
@@ -1807,12 +1899,13 @@ end
|
|
1807
1899
|
Use the `:static_cache_control` setting (see below) to add
|
1808
1900
|
`Cache-Control` header info to static files.
|
1809
1901
|
|
1810
|
-
According to RFC 2616, your application should behave differently if the
|
1811
|
-
or If-None-Match header is set to `*`, depending on whether the
|
1812
|
-
requested is already in existence. Sinatra assumes resources for
|
1813
|
-
and idempotent (like put) requests are already in existence,
|
1814
|
-
resources (for instance post requests) are treated as new
|
1815
|
-
can change this behavior by passing in a `:new_resource`
|
1902
|
+
According to RFC 2616, your application should behave differently if the
|
1903
|
+
If-Match or If-None-Match header is set to `*`, depending on whether the
|
1904
|
+
resource requested is already in existence. Sinatra assumes resources for
|
1905
|
+
safe (like get) and idempotent (like put) requests are already in existence,
|
1906
|
+
whereas other resources (for instance post requests) are treated as new
|
1907
|
+
resources. You can change this behavior by passing in a `:new_resource`
|
1908
|
+
option:
|
1816
1909
|
|
1817
1910
|
```ruby
|
1818
1911
|
get '/create' do
|
@@ -1849,8 +1942,8 @@ The options are:
|
|
1849
1942
|
|
1850
1943
|
<dl>
|
1851
1944
|
<dt>filename</dt>
|
1852
|
-
<dd>File name to be used in the response,
|
1853
|
-
|
1945
|
+
<dd>File name to be used in the response,
|
1946
|
+
defaults to the real file name.</dd>
|
1854
1947
|
<dt>last_modified</dt>
|
1855
1948
|
<dd>Value for Last-Modified header, defaults to the file's mtime.</dd>
|
1856
1949
|
|
@@ -1869,18 +1962,17 @@ The options are:
|
|
1869
1962
|
|
1870
1963
|
<dt>status</dt>
|
1871
1964
|
<dd>
|
1872
|
-
Status code to be sent. Useful when sending a static file as an error
|
1873
|
-
|
1874
|
-
|
1875
|
-
|
1876
|
-
automatically handle range requests.
|
1965
|
+
Status code to be sent. Useful when sending a static file as an error
|
1966
|
+
page. If supported by the Rack handler, other means than streaming
|
1967
|
+
from the Ruby process will be used. If you use this helper method,
|
1968
|
+
Sinatra will automatically handle range requests.
|
1877
1969
|
</dd>
|
1878
1970
|
</dl>
|
1879
1971
|
|
1880
1972
|
### Accessing the Request Object
|
1881
1973
|
|
1882
|
-
The incoming request object can be accessed from request level (filter,
|
1883
|
-
error handlers) through the `request` method:
|
1974
|
+
The incoming request object can be accessed from request level (filter,
|
1975
|
+
routes, error handlers) through the `request` method:
|
1884
1976
|
|
1885
1977
|
```ruby
|
1886
1978
|
# app running on http://example.com/example
|
@@ -1937,8 +2029,8 @@ end
|
|
1937
2029
|
|
1938
2030
|
### Attachments
|
1939
2031
|
|
1940
|
-
You can use the `attachment` helper to tell the browser the response should
|
1941
|
-
stored on disk rather than displayed in the browser:
|
2032
|
+
You can use the `attachment` helper to tell the browser the response should
|
2033
|
+
be stored on disk rather than displayed in the browser:
|
1942
2034
|
|
1943
2035
|
```ruby
|
1944
2036
|
get '/' do
|
@@ -1958,19 +2050,20 @@ end
|
|
1958
2050
|
|
1959
2051
|
### Dealing with Date and Time
|
1960
2052
|
|
1961
|
-
Sinatra offers a `time_for` helper method that generates a Time object from
|
1962
|
-
given value. It is also able to convert `DateTime`, `Date` and similar
|
2053
|
+
Sinatra offers a `time_for` helper method that generates a Time object from
|
2054
|
+
the given value. It is also able to convert `DateTime`, `Date` and similar
|
2055
|
+
classes:
|
1963
2056
|
|
1964
2057
|
```ruby
|
1965
2058
|
get '/' do
|
1966
|
-
pass if Time.now > time_for('Dec 23,
|
2059
|
+
pass if Time.now > time_for('Dec 23, 2016')
|
1967
2060
|
"still time"
|
1968
2061
|
end
|
1969
2062
|
```
|
1970
2063
|
|
1971
|
-
This method is used internally by `expires`, `last_modified` and akin. You
|
1972
|
-
therefore easily extend the behavior of those methods by overriding
|
1973
|
-
in your application:
|
2064
|
+
This method is used internally by `expires`, `last_modified` and akin. You
|
2065
|
+
can therefore easily extend the behavior of those methods by overriding
|
2066
|
+
`time_for` in your application:
|
1974
2067
|
|
1975
2068
|
```ruby
|
1976
2069
|
helpers do
|
@@ -2000,9 +2093,9 @@ find_template settings.views, 'foo', Tilt[:haml] do |file|
|
|
2000
2093
|
end
|
2001
2094
|
```
|
2002
2095
|
|
2003
|
-
This is not really useful. But it is useful that you can actually override
|
2004
|
-
method to hook in your own lookup mechanism. For instance, if you want
|
2005
|
-
able to use more than one view directory:
|
2096
|
+
This is not really useful. But it is useful that you can actually override
|
2097
|
+
this method to hook in your own lookup mechanism. For instance, if you want
|
2098
|
+
to be able to use more than one view directory:
|
2006
2099
|
|
2007
2100
|
```ruby
|
2008
2101
|
set :views, ['views', 'templates']
|
@@ -2031,11 +2124,11 @@ end
|
|
2031
2124
|
You can also easily wrap this up in an extension and share with others!
|
2032
2125
|
|
2033
2126
|
Note that `find_template` does not check if the file really exists but
|
2034
|
-
rather calls the given block for all possible paths. This is not a
|
2035
|
-
issue, since `render` will use `break` as soon as a file is
|
2036
|
-
template locations (and content) will be cached if you are not
|
2037
|
-
development mode. You should keep that in mind if you write a
|
2038
|
-
method.
|
2127
|
+
rather calls the given block for all possible paths. This is not a
|
2128
|
+
performance issue, since `render` will use `break` as soon as a file is
|
2129
|
+
found. Also, template locations (and content) will be cached if you are not
|
2130
|
+
running in development mode. You should keep that in mind if you write a
|
2131
|
+
really crazy method.
|
2039
2132
|
|
2040
2133
|
## Configuration
|
2041
2134
|
|
@@ -2094,10 +2187,10 @@ end
|
|
2094
2187
|
### Configuring attack protection
|
2095
2188
|
|
2096
2189
|
Sinatra is using
|
2097
|
-
[Rack::Protection](https://github.com/sinatra/rack-protection#readme) to
|
2098
|
-
your application against common, opportunistic attacks. You can
|
2099
|
-
this behavior (which will open up your application to tons
|
2100
|
-
vulnerabilities):
|
2190
|
+
[Rack::Protection](https://github.com/sinatra/rack-protection#readme) to
|
2191
|
+
defend your application against common, opportunistic attacks. You can
|
2192
|
+
easily disable this behavior (which will open up your application to tons
|
2193
|
+
of common vulnerabilities):
|
2101
2194
|
|
2102
2195
|
```ruby
|
2103
2196
|
disable :protection
|
@@ -2117,7 +2210,7 @@ set :protection, :except => [:path_traversal, :session_hijacking]
|
|
2117
2210
|
By default, Sinatra will only set up session based protection if `:sessions`
|
2118
2211
|
have been enabled. See 'Using Sessions'. Sometimes you may want to set up
|
2119
2212
|
sessions "outside" of the Sinatra app, such as in the config.ru or with a
|
2120
|
-
separate Rack::Builder instance. In that case you can still set up session
|
2213
|
+
separate `Rack::Builder` instance. In that case you can still set up session
|
2121
2214
|
based protection by passing the `:session` option:
|
2122
2215
|
|
2123
2216
|
```ruby
|
@@ -2128,194 +2221,213 @@ set :protection, :session => true
|
|
2128
2221
|
|
2129
2222
|
<dl>
|
2130
2223
|
<dt>absolute_redirects</dt>
|
2131
|
-
|
2132
|
-
|
2133
|
-
|
2134
|
-
|
2135
|
-
|
2136
|
-
|
2137
|
-
|
2138
|
-
|
2139
|
-
|
2140
|
-
|
2224
|
+
<dd>
|
2225
|
+
If disabled, Sinatra will allow relative redirects, however, Sinatra
|
2226
|
+
will no longer conform with RFC 2616 (HTTP 1.1), which only allows
|
2227
|
+
absolute redirects.
|
2228
|
+
</dd>
|
2229
|
+
<dd>
|
2230
|
+
Enable if your app is running behind a reverse proxy that has not been
|
2231
|
+
set up properly. Note that the <tt>url</tt> helper will still produce
|
2232
|
+
absolute URLs, unless you pass in <tt>false</tt> as the second
|
2233
|
+
parameter.
|
2234
|
+
</dd>
|
2235
|
+
<dd>Disabled by default.</dd>
|
2141
2236
|
|
2142
2237
|
<dt>add_charset</dt>
|
2143
|
-
|
2144
|
-
|
2145
|
-
|
2146
|
-
|
2147
|
-
|
2238
|
+
<dd>
|
2239
|
+
Mime types the <tt>content_type</tt> helper will automatically add the
|
2240
|
+
charset info to. You should add to it rather than overriding this
|
2241
|
+
option: <tt>settings.add_charset << "application/foobar"</tt>
|
2242
|
+
</dd>
|
2148
2243
|
|
2149
2244
|
<dt>app_file</dt>
|
2150
|
-
|
2151
|
-
|
2152
|
-
|
2153
|
-
|
2245
|
+
<dd>
|
2246
|
+
Path to the main application file, used to detect project root, views
|
2247
|
+
and public folder and inline templates.
|
2248
|
+
</dd>
|
2154
2249
|
|
2155
2250
|
<dt>bind</dt>
|
2156
|
-
|
2157
|
-
|
2158
|
-
|
2251
|
+
<dd>
|
2252
|
+
IP address to bind to (default: <tt>0.0.0.0</tt> <em>or</em>
|
2253
|
+
<tt>localhost</tt> if your `environment` is set to development). Only
|
2254
|
+
used for built-in server.
|
2255
|
+
</dd>
|
2159
2256
|
|
2160
2257
|
<dt>default_encoding</dt>
|
2161
|
-
|
2258
|
+
<dd>Encoding to assume if unknown (defaults to <tt>"utf-8"</tt>).</dd>
|
2162
2259
|
|
2163
2260
|
<dt>dump_errors</dt>
|
2164
|
-
|
2261
|
+
<dd>Display errors in the log.</dd>
|
2165
2262
|
|
2166
2263
|
<dt>environment</dt>
|
2167
|
-
|
2168
|
-
|
2169
|
-
|
2170
|
-
|
2264
|
+
<dd>
|
2265
|
+
Current environment. Defaults to <tt>ENV['APP_ENV']</tt>, or
|
2266
|
+
<tt>"development"</tt> if not available.
|
2267
|
+
</dd>
|
2171
2268
|
|
2172
2269
|
<dt>logging</dt>
|
2173
|
-
|
2270
|
+
<dd>Use the logger.</dd>
|
2174
2271
|
|
2175
2272
|
<dt>lock</dt>
|
2176
|
-
|
2177
|
-
|
2178
|
-
|
2179
|
-
|
2180
|
-
|
2273
|
+
<dd>
|
2274
|
+
Places a lock around every request, only running processing on request
|
2275
|
+
per Ruby process concurrently.
|
2276
|
+
</dd>
|
2277
|
+
<dd>Enabled if your app is not thread-safe. Disabled by default.</dd>
|
2181
2278
|
|
2182
2279
|
<dt>method_override</dt>
|
2280
|
+
<dd>
|
2281
|
+
Use <tt>_method</tt> magic to allow put/delete forms in browsers that
|
2282
|
+
don't support it.
|
2283
|
+
</dd>
|
2284
|
+
|
2285
|
+
<dt>mustermann_opts</dt>
|
2183
2286
|
<dd>
|
2184
|
-
|
2185
|
-
|
2287
|
+
A default hash of options to pass to Mustermann.new when compiling routing
|
2288
|
+
paths.
|
2186
2289
|
</dd>
|
2187
2290
|
|
2188
2291
|
<dt>port</dt>
|
2189
|
-
|
2292
|
+
<dd>Port to listen on. Only used for built-in server.</dd>
|
2190
2293
|
|
2191
2294
|
<dt>prefixed_redirects</dt>
|
2192
|
-
|
2193
|
-
|
2194
|
-
|
2195
|
-
|
2196
|
-
|
2295
|
+
<dd>
|
2296
|
+
Whether or not to insert <tt>request.script_name</tt> into redirects
|
2297
|
+
if no absolute path is given. That way <tt>redirect '/foo'</tt> would
|
2298
|
+
behave like <tt>redirect to('/foo')</tt>. Disabled by default.
|
2299
|
+
</dd>
|
2197
2300
|
|
2198
2301
|
<dt>protection</dt>
|
2199
|
-
|
2200
|
-
|
2302
|
+
<dd>
|
2303
|
+
Whether or not to enable web attack protections. See protection section
|
2304
|
+
above.
|
2305
|
+
</dd>
|
2201
2306
|
|
2202
2307
|
<dt>public_dir</dt>
|
2203
|
-
|
2308
|
+
<dd>Alias for <tt>public_folder</tt>. See below.</dd>
|
2204
2309
|
|
2205
2310
|
<dt>public_folder</dt>
|
2206
|
-
|
2207
|
-
|
2208
|
-
|
2209
|
-
|
2210
|
-
|
2311
|
+
<dd>
|
2312
|
+
Path to the folder public files are served from. Only used if static
|
2313
|
+
file serving is enabled (see <tt>static</tt> setting below). Inferred
|
2314
|
+
from <tt>app_file</tt> setting if not set.
|
2315
|
+
</dd>
|
2211
2316
|
|
2212
2317
|
<dt>quiet</dt>
|
2213
|
-
|
2214
|
-
|
2215
|
-
|
2318
|
+
<dd>
|
2319
|
+
Disables logs generated by Sinatra's start and stop commands.
|
2320
|
+
<tt>false</tt> by default.
|
2321
|
+
</dd>
|
2216
2322
|
|
2217
2323
|
<dt>reload_templates</dt>
|
2218
|
-
|
2219
|
-
|
2220
|
-
|
2221
|
-
|
2324
|
+
<dd>
|
2325
|
+
Whether or not to reload templates between requests. Enabled in
|
2326
|
+
development mode.
|
2327
|
+
</dd>
|
2222
2328
|
|
2223
2329
|
<dt>root</dt>
|
2224
|
-
|
2225
|
-
|
2226
|
-
|
2227
|
-
|
2330
|
+
<dd>
|
2331
|
+
Path to project root folder. Inferred from <tt>app_file</tt> setting
|
2332
|
+
if not set.
|
2333
|
+
</dd>
|
2228
2334
|
|
2229
2335
|
<dt>raise_errors</dt>
|
2230
|
-
|
2231
|
-
|
2232
|
-
|
2233
|
-
|
2336
|
+
<dd>
|
2337
|
+
Raise exceptions (will stop application). Enabled by default when
|
2338
|
+
<tt>environment</tt> is set to <tt>"test"</tt>, disabled otherwise.
|
2339
|
+
</dd>
|
2234
2340
|
|
2235
2341
|
<dt>run</dt>
|
2236
|
-
|
2237
|
-
|
2238
|
-
|
2239
|
-
|
2342
|
+
<dd>
|
2343
|
+
If enabled, Sinatra will handle starting the web server. Do not
|
2344
|
+
enable if using rackup or other means.
|
2345
|
+
</dd>
|
2240
2346
|
|
2241
2347
|
<dt>running</dt>
|
2242
|
-
|
2348
|
+
<dd>Is the built-in server running now? Do not change this setting!</dd>
|
2243
2349
|
|
2244
2350
|
<dt>server</dt>
|
2245
|
-
|
2246
|
-
|
2247
|
-
|
2248
|
-
|
2351
|
+
<dd>
|
2352
|
+
Server or list of servers to use for built-in server. Order indicates
|
2353
|
+
priority, default depends on Ruby implementation.
|
2354
|
+
</dd>
|
2249
2355
|
|
2250
2356
|
<dt>sessions</dt>
|
2251
|
-
|
2252
|
-
|
2253
|
-
|
2254
|
-
|
2357
|
+
<dd>
|
2358
|
+
Enable cookie-based sessions support using
|
2359
|
+
<tt>Rack::Session::Cookie</tt>. See 'Using Sessions' section for more
|
2360
|
+
information.
|
2361
|
+
</dd>
|
2255
2362
|
|
2256
2363
|
<dt>session_store</dt>
|
2257
|
-
|
2364
|
+
<dd>
|
2365
|
+
The Rack session middleware used. Defaults to
|
2366
|
+
<tt>Rack::Session::Cookie</tt>. See 'Using Sessions' section for more
|
2367
|
+
information.
|
2368
|
+
</dd>
|
2258
2369
|
|
2259
2370
|
<dt>show_exceptions</dt>
|
2260
|
-
|
2261
|
-
|
2262
|
-
|
2263
|
-
|
2264
|
-
|
2265
|
-
|
2266
|
-
|
2267
|
-
|
2268
|
-
|
2371
|
+
<dd>
|
2372
|
+
Show a stack trace in the browser when an exception happens. Enabled by
|
2373
|
+
default when <tt>environment</tt> is set to <tt>"development"</tt>,
|
2374
|
+
disabled otherwise.
|
2375
|
+
</dd>
|
2376
|
+
<dd>
|
2377
|
+
Can also be set to <tt>:after_handler</tt> to trigger app-specified
|
2378
|
+
error handling before showing a stack trace in the browser.
|
2379
|
+
</dd>
|
2269
2380
|
|
2270
2381
|
<dt>static</dt>
|
2271
|
-
|
2272
|
-
|
2273
|
-
|
2274
|
-
|
2275
|
-
|
2276
|
-
|
2382
|
+
<dd>Whether Sinatra should handle serving static files.</dd>
|
2383
|
+
<dd>Disable when using a server able to do this on its own.</dd>
|
2384
|
+
<dd>Disabling will boost performance.</dd>
|
2385
|
+
<dd>
|
2386
|
+
Enabled by default in classic style, disabled for modular apps.
|
2387
|
+
</dd>
|
2277
2388
|
|
2278
2389
|
<dt>static_cache_control</dt>
|
2279
|
-
|
2280
|
-
|
2281
|
-
|
2282
|
-
|
2283
|
-
|
2284
|
-
|
2285
|
-
|
2286
|
-
|
2287
|
-
|
2390
|
+
<dd>
|
2391
|
+
When Sinatra is serving static files, set this to add
|
2392
|
+
<tt>Cache-Control</tt> headers to the responses. Uses the
|
2393
|
+
<tt>cache_control</tt> helper. Disabled by default.
|
2394
|
+
</dd>
|
2395
|
+
<dd>
|
2396
|
+
Use an explicit array when setting multiple values:
|
2397
|
+
<tt>set :static_cache_control, [:public, :max_age => 300]</tt>
|
2398
|
+
</dd>
|
2288
2399
|
|
2289
2400
|
<dt>threaded</dt>
|
2290
|
-
|
2291
|
-
|
2292
|
-
|
2293
|
-
|
2401
|
+
<dd>
|
2402
|
+
If set to <tt>true</tt>, will tell Thin to use
|
2403
|
+
<tt>EventMachine.defer</tt> for processing the request.
|
2404
|
+
</dd>
|
2294
2405
|
|
2295
2406
|
<dt>traps</dt>
|
2296
|
-
|
2407
|
+
<dd>Whether Sinatra should handle system signals.</dd>
|
2297
2408
|
|
2298
2409
|
<dt>views</dt>
|
2299
|
-
|
2300
|
-
|
2301
|
-
|
2302
|
-
|
2410
|
+
<dd>
|
2411
|
+
Path to the views folder. Inferred from <tt>app_file</tt> setting if
|
2412
|
+
not set.
|
2413
|
+
</dd>
|
2303
2414
|
|
2304
2415
|
<dt>x_cascade</dt>
|
2305
|
-
|
2306
|
-
|
2307
|
-
|
2308
|
-
|
2416
|
+
<dd>
|
2417
|
+
Whether or not to set the X-Cascade header if no route matches.
|
2418
|
+
Defaults to <tt>true</tt>.
|
2419
|
+
</dd>
|
2309
2420
|
</dl>
|
2310
2421
|
|
2311
2422
|
## Environments
|
2312
2423
|
|
2313
|
-
There are three predefined `environments`: `"development"`,
|
2314
|
-
`"test"`. Environments can be set through the
|
2315
|
-
The default value is `"development"`.
|
2316
|
-
|
2317
|
-
|
2318
|
-
`"test"` environments,
|
2424
|
+
There are three predefined `environments`: `"development"`,
|
2425
|
+
`"production"` and `"test"`. Environments can be set through the
|
2426
|
+
`APP_ENV` environment variable. The default value is `"development"`.
|
2427
|
+
In the `"development"` environment all templates are reloaded between
|
2428
|
+
requests, and special `not_found` and `error` handlers display stack
|
2429
|
+
traces in your browser. In the `"production"` and `"test"` environments,
|
2430
|
+
templates are cached by default.
|
2319
2431
|
|
2320
2432
|
To run different environments, set the `APP_ENV` environment variable:
|
2321
2433
|
|
@@ -2338,9 +2450,9 @@ end
|
|
2338
2450
|
|
2339
2451
|
## Error Handling
|
2340
2452
|
|
2341
|
-
Error handlers run within the same context as routes and before filters,
|
2342
|
-
means you get all the goodies it has to offer, like `haml`,
|
2343
|
-
`
|
2453
|
+
Error handlers run within the same context as routes and before filters,
|
2454
|
+
which means you get all the goodies it has to offer, like `haml`, `erb`,
|
2455
|
+
`halt`, etc.
|
2344
2456
|
|
2345
2457
|
### Not Found
|
2346
2458
|
|
@@ -2420,10 +2532,11 @@ and additional debugging information in your browser.
|
|
2420
2532
|
## Rack Middleware
|
2421
2533
|
|
2422
2534
|
Sinatra rides on [Rack](http://rack.github.io/), a minimal standard
|
2423
|
-
interface for Ruby web frameworks. One of Rack's most interesting
|
2424
|
-
for application developers is support for "middleware" --
|
2425
|
-
between the server and your application monitoring
|
2426
|
-
HTTP request/response to provide various types
|
2535
|
+
interface for Ruby web frameworks. One of Rack's most interesting
|
2536
|
+
capabilities for application developers is support for "middleware" --
|
2537
|
+
components that sit between the server and your application monitoring
|
2538
|
+
and/or manipulating the HTTP request/response to provide various types
|
2539
|
+
of common functionality.
|
2427
2540
|
|
2428
2541
|
Sinatra makes building Rack middleware pipelines a cinch via a top-level
|
2429
2542
|
`use` method:
|
@@ -2463,7 +2576,8 @@ or in the [Rack wiki](https://github.com/rack/rack/wiki/List-of-Middleware).
|
|
2463
2576
|
|
2464
2577
|
## Testing
|
2465
2578
|
|
2466
|
-
Sinatra tests can be written using any Rack-based testing library or
|
2579
|
+
Sinatra tests can be written using any Rack-based testing library or
|
2580
|
+
framework.
|
2467
2581
|
[Rack::Test](http://www.rubydoc.info/github/brynary/rack-test/master/frames)
|
2468
2582
|
is recommended:
|
2469
2583
|
|
@@ -2522,9 +2636,9 @@ class MyApp < Sinatra::Base
|
|
2522
2636
|
end
|
2523
2637
|
```
|
2524
2638
|
|
2525
|
-
The methods available to `Sinatra::Base` subclasses are exactly the same
|
2526
|
-
those available via the top-level DSL. Most top-level apps can be
|
2527
|
-
`Sinatra::Base` components with two modifications:
|
2639
|
+
The methods available to `Sinatra::Base` subclasses are exactly the same
|
2640
|
+
as those available via the top-level DSL. Most top-level apps can be
|
2641
|
+
converted to `Sinatra::Base` components with two modifications:
|
2528
2642
|
|
2529
2643
|
* Your file should require `sinatra/base` instead of `sinatra`;
|
2530
2644
|
otherwise, all of Sinatra's DSL methods are imported into the main
|
@@ -2533,12 +2647,11 @@ those available via the top-level DSL. Most top-level apps can be converted to
|
|
2533
2647
|
of `Sinatra::Base`.
|
2534
2648
|
|
2535
2649
|
`Sinatra::Base` is a blank slate. Most options are disabled by default,
|
2536
|
-
including the built-in server. See
|
2537
|
-
|
2538
|
-
|
2539
|
-
|
2540
|
-
|
2541
|
-
can subclass `Sinatra::Application`.
|
2650
|
+
including the built-in server. See [Configuring
|
2651
|
+
Settings](http://www.sinatrarb.com/configuration.html) for details on
|
2652
|
+
available options and their behavior. If you want behavior more similar
|
2653
|
+
to when you define your app at the top level (also known as Classic
|
2654
|
+
style), you can subclass `Sinatra::Application`:
|
2542
2655
|
|
2543
2656
|
```ruby
|
2544
2657
|
require 'sinatra/base'
|
@@ -2552,16 +2665,17 @@ end
|
|
2552
2665
|
|
2553
2666
|
### Modular vs. Classic Style
|
2554
2667
|
|
2555
|
-
Contrary to common belief, there is nothing wrong with the classic
|
2556
|
-
suits your application, you do not have to switch to a
|
2668
|
+
Contrary to common belief, there is nothing wrong with the classic
|
2669
|
+
style. If it suits your application, you do not have to switch to a
|
2670
|
+
modular application.
|
2557
2671
|
|
2558
|
-
The main disadvantage of using the classic style rather than the modular
|
2559
|
-
is that you will only have one Sinatra application per Ruby
|
2560
|
-
plan to use more than one, switch to the modular style.
|
2561
|
-
cannot mix the modular and the classic styles.
|
2672
|
+
The main disadvantage of using the classic style rather than the modular
|
2673
|
+
style is that you will only have one Sinatra application per Ruby
|
2674
|
+
process. If you plan to use more than one, switch to the modular style.
|
2675
|
+
There is no reason you cannot mix the modular and the classic styles.
|
2562
2676
|
|
2563
|
-
If switching from one style to the other, you should be aware of
|
2564
|
-
different default settings:
|
2677
|
+
If switching from one style to the other, you should be aware of
|
2678
|
+
slightly different default settings:
|
2565
2679
|
|
2566
2680
|
<table>
|
2567
2681
|
<tr>
|
@@ -2616,8 +2730,8 @@ different default settings:
|
|
2616
2730
|
|
2617
2731
|
### Serving a Modular Application
|
2618
2732
|
|
2619
|
-
There are two common options for starting a modular app, actively
|
2620
|
-
`run!`:
|
2733
|
+
There are two common options for starting a modular app, actively
|
2734
|
+
starting with `run!`:
|
2621
2735
|
|
2622
2736
|
```ruby
|
2623
2737
|
# my_app.rb
|
@@ -2680,16 +2794,16 @@ A `config.ru` file is recommended if:
|
|
2680
2794
|
* You want to use more than one subclass of `Sinatra::Base`.
|
2681
2795
|
* You want to use Sinatra only for middleware, and not as an endpoint.
|
2682
2796
|
|
2683
|
-
**There is no need to switch to a `config.ru` simply because you
|
2684
|
-
the modular style, and you don't have to use the modular
|
2685
|
-
a `config.ru`.**
|
2797
|
+
**There is no need to switch to a `config.ru` simply because you
|
2798
|
+
switched to the modular style, and you don't have to use the modular
|
2799
|
+
style for running with a `config.ru`.**
|
2686
2800
|
|
2687
2801
|
### Using Sinatra as Middleware
|
2688
2802
|
|
2689
|
-
Not only is Sinatra able to use other Rack middleware, any Sinatra
|
2690
|
-
can in turn be added in front of any Rack endpoint as
|
2691
|
-
endpoint could be another Sinatra application,
|
2692
|
-
application (Rails/
|
2803
|
+
Not only is Sinatra able to use other Rack middleware, any Sinatra
|
2804
|
+
application can in turn be added in front of any Rack endpoint as
|
2805
|
+
middleware itself. This endpoint could be another Sinatra application,
|
2806
|
+
or any other Rack-based application (Rails/Hanami/Roda/...):
|
2693
2807
|
|
2694
2808
|
```ruby
|
2695
2809
|
require 'sinatra/base'
|
@@ -2778,9 +2892,9 @@ available.
|
|
2778
2892
|
Every Sinatra application corresponds to a subclass of `Sinatra::Base`.
|
2779
2893
|
If you are using the top-level DSL (`require 'sinatra'`), then this
|
2780
2894
|
class is `Sinatra::Application`, otherwise it is the subclass you
|
2781
|
-
created explicitly. At class level you have methods like `get` or
|
2782
|
-
you cannot access the `request` or `session` objects, as
|
2783
|
-
single application class for all requests.
|
2895
|
+
created explicitly. At class level you have methods like `get` or
|
2896
|
+
`before`, but you cannot access the `request` or `session` objects, as
|
2897
|
+
there is only a single application class for all requests.
|
2784
2898
|
|
2785
2899
|
Options created via `set` are methods at class level:
|
2786
2900
|
|
@@ -2954,9 +3068,9 @@ known to run Sinatra:
|
|
2954
3068
|
Not being officially supported means if things only break there and not on a
|
2955
3069
|
supported platform, we assume it's not our issue but theirs.
|
2956
3070
|
|
2957
|
-
We also run our CI against ruby-head (future releases of MRI), but we
|
2958
|
-
guarantee anything, since it is constantly moving. Expect upcoming
|
2959
|
-
to be fully supported.
|
3071
|
+
We also run our CI against ruby-head (future releases of MRI), but we
|
3072
|
+
can't guarantee anything, since it is constantly moving. Expect upcoming
|
3073
|
+
2.x releases to be fully supported.
|
2960
3074
|
|
2961
3075
|
Sinatra should work on any operating system supported by the chosen Ruby
|
2962
3076
|
implementation.
|
@@ -2968,8 +3082,9 @@ Ruby version prior to 2.2.
|
|
2968
3082
|
|
2969
3083
|
## The Bleeding Edge
|
2970
3084
|
|
2971
|
-
If you would like to use Sinatra's latest bleeding-edge code, feel free
|
2972
|
-
application against the master branch, it should be rather
|
3085
|
+
If you would like to use Sinatra's latest bleeding-edge code, feel free
|
3086
|
+
to run your application against the master branch, it should be rather
|
3087
|
+
stable.
|
2973
3088
|
|
2974
3089
|
We also push out prerelease gems from time to time, so you can do a
|
2975
3090
|
|
@@ -2998,12 +3113,11 @@ gem 'sinatra', :github => 'sinatra/sinatra'
|
|
2998
3113
|
|
2999
3114
|
# other dependencies
|
3000
3115
|
gem 'haml' # for instance, if you use haml
|
3001
|
-
gem 'activerecord', '~> 3.0' # maybe you also need ActiveRecord 3.x
|
3002
3116
|
```
|
3003
3117
|
|
3004
|
-
Note that you will have to list all your application's dependencies in
|
3005
|
-
Sinatra's direct dependencies (Rack and Tilt) will,
|
3006
|
-
fetched and added by Bundler.
|
3118
|
+
Note that you will have to list all your application's dependencies in
|
3119
|
+
the `Gemfile`. Sinatra's direct dependencies (Rack and Tilt) will,
|
3120
|
+
however, be automatically fetched and added by Bundler.
|
3007
3121
|
|
3008
3122
|
Now you can run your app like this:
|
3009
3123
|
|
@@ -3011,41 +3125,6 @@ Now you can run your app like this:
|
|
3011
3125
|
bundle exec ruby myapp.rb
|
3012
3126
|
```
|
3013
3127
|
|
3014
|
-
### Roll Your Own
|
3015
|
-
|
3016
|
-
Create a local clone and run your app with the `sinatra/lib` directory
|
3017
|
-
on the `$LOAD_PATH`:
|
3018
|
-
|
3019
|
-
```shell
|
3020
|
-
cd myapp
|
3021
|
-
git clone git://github.com/sinatra/sinatra.git
|
3022
|
-
ruby -I sinatra/lib myapp.rb
|
3023
|
-
```
|
3024
|
-
|
3025
|
-
To update the Sinatra sources in the future:
|
3026
|
-
|
3027
|
-
```shell
|
3028
|
-
cd myapp/sinatra
|
3029
|
-
git pull
|
3030
|
-
```
|
3031
|
-
|
3032
|
-
### Install Globally
|
3033
|
-
|
3034
|
-
You can build the gem on your own:
|
3035
|
-
|
3036
|
-
```shell
|
3037
|
-
git clone git://github.com/sinatra/sinatra.git
|
3038
|
-
cd sinatra
|
3039
|
-
rake sinatra.gemspec
|
3040
|
-
rake install
|
3041
|
-
```
|
3042
|
-
|
3043
|
-
If you install gems as root, the last step should be:
|
3044
|
-
|
3045
|
-
```shell
|
3046
|
-
sudo rake install
|
3047
|
-
```
|
3048
|
-
|
3049
3128
|
## Versioning
|
3050
3129
|
|
3051
3130
|
Sinatra follows [Semantic Versioning](http://semver.org/), both SemVer and
|