sinatra 3.0.5 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/AUTHORS.md +14 -8
- data/CHANGELOG.md +75 -2
- data/CONTRIBUTING.md +11 -11
- data/Gemfile +22 -20
- data/MAINTENANCE.md +2 -2
- data/README.md +137 -129
- data/Rakefile +25 -22
- data/SECURITY.md +1 -1
- data/VERSION +1 -1
- data/examples/chat.rb +20 -9
- data/examples/lifecycle_events.rb +20 -0
- data/examples/stream.ru +0 -2
- data/lib/sinatra/base.rb +154 -46
- data/lib/sinatra/indifferent_hash.rb +10 -8
- data/lib/sinatra/show_exceptions.rb +2 -2
- data/lib/sinatra/version.rb +1 -1
- data/sinatra.gemspec +4 -5
- metadata +37 -32
- data/examples/rainbows.conf +0 -3
- data/examples/rainbows.rb +0 -22
data/README.md
CHANGED
|
@@ -54,6 +54,8 @@ pick up if available.
|
|
|
54
54
|
- [Erb Templates](#erb-templates)
|
|
55
55
|
- [Builder Templates](#builder-templates)
|
|
56
56
|
- [Nokogiri Templates](#nokogiri-templates)
|
|
57
|
+
- [Sass Templates](#sass-templates)
|
|
58
|
+
- [Scss Templates](#scss-templates)
|
|
57
59
|
- [Liquid Templates](#liquid-templates)
|
|
58
60
|
- [Markdown Templates](#markdown-templates)
|
|
59
61
|
- [RDoc Templates](#rdoc-templates)
|
|
@@ -93,6 +95,7 @@ pick up if available.
|
|
|
93
95
|
- [Configuration](#configuration)
|
|
94
96
|
- [Configuring attack protection](#configuring-attack-protection)
|
|
95
97
|
- [Available Settings](#available-settings)
|
|
98
|
+
- [Lifecycle Events](#lifecycle-events)
|
|
96
99
|
- [Environments](#environments)
|
|
97
100
|
- [Error Handling](#error-handling)
|
|
98
101
|
- [Not Found](#not-found)
|
|
@@ -649,6 +652,39 @@ It also takes a block for inline templates (see [example](#inline-templates)).
|
|
|
649
652
|
|
|
650
653
|
It also takes a block for inline templates (see [example](#inline-templates)).
|
|
651
654
|
|
|
655
|
+
#### Sass Templates
|
|
656
|
+
|
|
657
|
+
<table>
|
|
658
|
+
<tr>
|
|
659
|
+
<td>Dependency</td>
|
|
660
|
+
<td><a href="https://github.com/ntkme/sass-embedded-host-ruby" title="sass-embedded">sass-embedded</a></td>
|
|
661
|
+
</tr>
|
|
662
|
+
<tr>
|
|
663
|
+
<td>File Extension</td>
|
|
664
|
+
<td><tt>.sass</tt></td>
|
|
665
|
+
</tr>
|
|
666
|
+
<tr>
|
|
667
|
+
<td>Example</td>
|
|
668
|
+
<td><tt>sass :stylesheet, :style => :expanded</tt></td>
|
|
669
|
+
</tr>
|
|
670
|
+
</table>
|
|
671
|
+
|
|
672
|
+
#### Scss Templates
|
|
673
|
+
|
|
674
|
+
<table>
|
|
675
|
+
<tr>
|
|
676
|
+
<td>Dependency</td>
|
|
677
|
+
<td><a href="https://github.com/ntkme/sass-embedded-host-ruby" title="sass-embedded">sass-embedded</a></td>
|
|
678
|
+
</tr>
|
|
679
|
+
<tr>
|
|
680
|
+
<td>File Extension</td>
|
|
681
|
+
<td><tt>.scss</tt></td>
|
|
682
|
+
</tr>
|
|
683
|
+
<tr>
|
|
684
|
+
<td>Example</td>
|
|
685
|
+
<td><tt>scss :stylesheet, :style => :expanded</tt></td>
|
|
686
|
+
</tr>
|
|
687
|
+
</table>
|
|
652
688
|
|
|
653
689
|
#### Liquid Templates
|
|
654
690
|
|
|
@@ -810,7 +846,7 @@ It also takes a block for inline templates (see [example](#inline-templates)).
|
|
|
810
846
|
<table>
|
|
811
847
|
<tr>
|
|
812
848
|
<td>Dependency</td>
|
|
813
|
-
<td><a href="
|
|
849
|
+
<td><a href="https://slim-template.github.io/" title="Slim Lang">Slim Lang</a></td>
|
|
814
850
|
</tr>
|
|
815
851
|
<tr>
|
|
816
852
|
<td>File Extension</td>
|
|
@@ -1163,22 +1199,6 @@ $ ruby -e "require 'securerandom'; puts SecureRandom.hex(64)"
|
|
|
1163
1199
|
99ae8af...snip...ec0f262ac
|
|
1164
1200
|
```
|
|
1165
1201
|
|
|
1166
|
-
**Session Secret Generation (Bonus Points)**
|
|
1167
|
-
|
|
1168
|
-
Use the [sysrandom gem](https://github.com/cryptosphere/sysrandom#readme) to
|
|
1169
|
-
use the system RNG facilities to generate random values instead of
|
|
1170
|
-
userspace `OpenSSL` which MRI Ruby currently defaults to:
|
|
1171
|
-
|
|
1172
|
-
```text
|
|
1173
|
-
$ gem install sysrandom
|
|
1174
|
-
Building native extensions. This could take a while...
|
|
1175
|
-
Successfully installed sysrandom-1.x
|
|
1176
|
-
1 gem installed
|
|
1177
|
-
|
|
1178
|
-
$ ruby -e "require 'sysrandom/securerandom'; puts SecureRandom.hex(64)"
|
|
1179
|
-
99ae8af...snip...ec0f262ac
|
|
1180
|
-
```
|
|
1181
|
-
|
|
1182
1202
|
**Session Secret Environment Variable**
|
|
1183
1203
|
|
|
1184
1204
|
Set a `SESSION_SECRET` environment variable for Sinatra to the value you
|
|
@@ -1193,14 +1213,10 @@ purposes only:
|
|
|
1193
1213
|
**Session Secret App Config**
|
|
1194
1214
|
|
|
1195
1215
|
Set up your app config to fail-safe to a secure random secret
|
|
1196
|
-
if the `SESSION_SECRET` environment variable is not available
|
|
1197
|
-
|
|
1198
|
-
For bonus points use the [sysrandom
|
|
1199
|
-
gem](https://github.com/cryptosphere/sysrandom#readme) here as well:
|
|
1216
|
+
if the `SESSION_SECRET` environment variable is not available:
|
|
1200
1217
|
|
|
1201
1218
|
```ruby
|
|
1202
1219
|
require 'securerandom'
|
|
1203
|
-
# -or- require 'sysrandom/securerandom'
|
|
1204
1220
|
set :session_secret, ENV.fetch('SESSION_SECRET') { SecureRandom.hex(64) }
|
|
1205
1221
|
```
|
|
1206
1222
|
|
|
@@ -1405,53 +1421,9 @@ to `stream` finishes executing. Streaming does not work at all with Shotgun.
|
|
|
1405
1421
|
|
|
1406
1422
|
If the optional parameter is set to `keep_open`, it will not call `close` on
|
|
1407
1423
|
the stream object, allowing you to close it at any later point in the
|
|
1408
|
-
execution flow.
|
|
1409
|
-
Other servers will still close the stream:
|
|
1410
|
-
|
|
1411
|
-
```ruby
|
|
1412
|
-
# config.ru
|
|
1413
|
-
require 'sinatra/base'
|
|
1414
|
-
|
|
1415
|
-
class App < Sinatra::Base
|
|
1416
|
-
connections = []
|
|
1424
|
+
execution flow.
|
|
1417
1425
|
|
|
1418
|
-
|
|
1419
|
-
# register a client's interest in server events
|
|
1420
|
-
stream(:keep_open) do |out|
|
|
1421
|
-
connections << out
|
|
1422
|
-
# purge dead connections
|
|
1423
|
-
connections.reject!(&:closed?)
|
|
1424
|
-
end
|
|
1425
|
-
end
|
|
1426
|
-
|
|
1427
|
-
post '/' do
|
|
1428
|
-
connections.each do |out|
|
|
1429
|
-
# notify client that a new message has arrived
|
|
1430
|
-
out << "data: #{params[:msg]}\n\n"
|
|
1431
|
-
|
|
1432
|
-
# indicate client to connect again
|
|
1433
|
-
out.close
|
|
1434
|
-
end
|
|
1435
|
-
|
|
1436
|
-
204 # response without entity body
|
|
1437
|
-
end
|
|
1438
|
-
end
|
|
1439
|
-
|
|
1440
|
-
run App
|
|
1441
|
-
```
|
|
1442
|
-
|
|
1443
|
-
```ruby
|
|
1444
|
-
# rainbows.conf
|
|
1445
|
-
Rainbows! do
|
|
1446
|
-
use :EventMachine
|
|
1447
|
-
end
|
|
1448
|
-
````
|
|
1449
|
-
|
|
1450
|
-
Run:
|
|
1451
|
-
|
|
1452
|
-
```shell
|
|
1453
|
-
rainbows -c rainbows.conf
|
|
1454
|
-
```
|
|
1426
|
+
You can have a look at the [chat example](https://github.com/sinatra/sinatra/blob/main/examples/chat.rb)
|
|
1455
1427
|
|
|
1456
1428
|
It's also possible for the client to close the connection when trying to
|
|
1457
1429
|
write to the socket. Because of this, it's recommended to check
|
|
@@ -1933,7 +1905,7 @@ end
|
|
|
1933
1905
|
### Configuring attack protection
|
|
1934
1906
|
|
|
1935
1907
|
Sinatra is using
|
|
1936
|
-
[Rack::Protection](https://github.com/sinatra/sinatra/tree/
|
|
1908
|
+
[Rack::Protection](https://github.com/sinatra/sinatra/tree/main/rack-protection#readme) to
|
|
1937
1909
|
defend your application against common, opportunistic attacks. You can
|
|
1938
1910
|
easily disable this behavior (which will open up your application to tons
|
|
1939
1911
|
of common vulnerabilities):
|
|
@@ -1950,7 +1922,7 @@ set :protection, :except => :path_traversal
|
|
|
1950
1922
|
You can also hand in an array in order to disable a list of protections:
|
|
1951
1923
|
|
|
1952
1924
|
```ruby
|
|
1953
|
-
set :protection, :except => [:path_traversal, :
|
|
1925
|
+
set :protection, :except => [:path_traversal, :remote_token]
|
|
1954
1926
|
```
|
|
1955
1927
|
|
|
1956
1928
|
By default, Sinatra will only set up session based protection if `:sessions`
|
|
@@ -2089,9 +2061,13 @@ set :protection, :session => true
|
|
|
2089
2061
|
|
|
2090
2062
|
<dt>raise_errors</dt>
|
|
2091
2063
|
<dd>
|
|
2092
|
-
Raise
|
|
2064
|
+
Raise unhandled errors (will stop application). Enabled by default when
|
|
2093
2065
|
<tt>environment</tt> is set to <tt>"test"</tt>, disabled otherwise.
|
|
2094
2066
|
</dd>
|
|
2067
|
+
<dd>
|
|
2068
|
+
Any explicitly defined error handlers always override this setting. See
|
|
2069
|
+
the "Error" section below.
|
|
2070
|
+
</dd>
|
|
2095
2071
|
|
|
2096
2072
|
<dt>run</dt>
|
|
2097
2073
|
<dd>
|
|
@@ -2184,6 +2160,24 @@ set :protection, :session => true
|
|
|
2184
2160
|
</dd>
|
|
2185
2161
|
</dl>
|
|
2186
2162
|
|
|
2163
|
+
## Lifecycle Events
|
|
2164
|
+
|
|
2165
|
+
There are 2 lifecycle events currently exposed by Sinatra. One when the server starts and one when it stops.
|
|
2166
|
+
|
|
2167
|
+
They can be used like this:
|
|
2168
|
+
|
|
2169
|
+
```ruby
|
|
2170
|
+
on_start do
|
|
2171
|
+
puts "===== Booting up ====="
|
|
2172
|
+
end
|
|
2173
|
+
|
|
2174
|
+
on_stop do
|
|
2175
|
+
puts "===== Shutting down ====="
|
|
2176
|
+
end
|
|
2177
|
+
```
|
|
2178
|
+
|
|
2179
|
+
Note that these callbacks only work when using Sinatra to start the web server.
|
|
2180
|
+
|
|
2187
2181
|
## Environments
|
|
2188
2182
|
|
|
2189
2183
|
There are three predefined `environments`: `"development"`,
|
|
@@ -2240,6 +2234,14 @@ show exceptions option to `:after_handler`:
|
|
|
2240
2234
|
set :show_exceptions, :after_handler
|
|
2241
2235
|
```
|
|
2242
2236
|
|
|
2237
|
+
A catch-all error handler can be defined with `error` and a block:
|
|
2238
|
+
|
|
2239
|
+
```ruby
|
|
2240
|
+
error do
|
|
2241
|
+
'Sorry there was a nasty error'
|
|
2242
|
+
end
|
|
2243
|
+
```
|
|
2244
|
+
|
|
2243
2245
|
The exception object can be obtained from the `sinatra.error` Rack variable:
|
|
2244
2246
|
|
|
2245
2247
|
```ruby
|
|
@@ -2248,7 +2250,7 @@ error do
|
|
|
2248
2250
|
end
|
|
2249
2251
|
```
|
|
2250
2252
|
|
|
2251
|
-
|
|
2253
|
+
Pass an error class as an argument to create handlers for custom errors:
|
|
2252
2254
|
|
|
2253
2255
|
```ruby
|
|
2254
2256
|
error MyCustomError do
|
|
@@ -2294,6 +2296,51 @@ Sinatra installs special `not_found` and `error` handlers when
|
|
|
2294
2296
|
running under the development environment to display nice stack traces
|
|
2295
2297
|
and additional debugging information in your browser.
|
|
2296
2298
|
|
|
2299
|
+
### Behavior with `raise_errors` option
|
|
2300
|
+
|
|
2301
|
+
When `raise_errors` option is `true`, errors that are unhandled are raised
|
|
2302
|
+
outside of the application. Additionally, any errors that would have been
|
|
2303
|
+
caught by the catch-all error handler are raised.
|
|
2304
|
+
|
|
2305
|
+
For example, consider the following configuration:
|
|
2306
|
+
|
|
2307
|
+
```ruby
|
|
2308
|
+
# First handler
|
|
2309
|
+
error MyCustomError do
|
|
2310
|
+
'A custom message'
|
|
2311
|
+
end
|
|
2312
|
+
|
|
2313
|
+
# Second handler
|
|
2314
|
+
error do
|
|
2315
|
+
'A catch-all message'
|
|
2316
|
+
end
|
|
2317
|
+
```
|
|
2318
|
+
|
|
2319
|
+
If `raise_errors` is `false`:
|
|
2320
|
+
|
|
2321
|
+
* When `MyCustomError` or descendant is raised, the first handler is invoked.
|
|
2322
|
+
The HTTP response body will contain `"A custom message"`.
|
|
2323
|
+
* When any other error is raised, the second handler is invoked. The HTTP
|
|
2324
|
+
response body will contain `"A catch-all message"`.
|
|
2325
|
+
|
|
2326
|
+
If `raise_errors` is `true`:
|
|
2327
|
+
|
|
2328
|
+
* When `MyCustomError` or descendant is raised, the behavior is identical to
|
|
2329
|
+
when `raise_errors` is `false`, described above.
|
|
2330
|
+
* When any other error is raised, the second handler is *not* invoked, and
|
|
2331
|
+
the error is raised outside of the application.
|
|
2332
|
+
* If the environment is `production`, the HTTP response body will contain
|
|
2333
|
+
a generic error message, e.g. `"An unhandled lowlevel error occurred. The
|
|
2334
|
+
application logs may have details."`
|
|
2335
|
+
* If the environment is not `production`, the HTTP response body will contain
|
|
2336
|
+
the verbose error backtrace.
|
|
2337
|
+
* Regardless of environment, if `show_exceptions` is set to `:after_handler`,
|
|
2338
|
+
the HTTP response body will contain the verbose error backtrace.
|
|
2339
|
+
|
|
2340
|
+
In the `test` environment, `raise_errors` is set to `true` by default. This
|
|
2341
|
+
means that in order to write a test for a catch-all error handler,
|
|
2342
|
+
`raise_errors` must temporarily be set to `false` for that particular test.
|
|
2343
|
+
|
|
2297
2344
|
## Rack Middleware
|
|
2298
2345
|
|
|
2299
2346
|
Sinatra rides on [Rack](https://rack.github.io/), a minimal standard
|
|
@@ -2319,7 +2366,7 @@ end
|
|
|
2319
2366
|
```
|
|
2320
2367
|
|
|
2321
2368
|
The semantics of `use` are identical to those defined for the
|
|
2322
|
-
[Rack::Builder](
|
|
2369
|
+
[Rack::Builder](https://www.rubydoc.info/github/rack/rack/main/Rack/Builder) DSL
|
|
2323
2370
|
(most frequently used from rackup files). For example, the `use` method
|
|
2324
2371
|
accepts multiple/variable args as well as blocks:
|
|
2325
2372
|
|
|
@@ -2335,7 +2382,7 @@ many of these components automatically based on configuration so you
|
|
|
2335
2382
|
typically don't have to `use` them explicitly.
|
|
2336
2383
|
|
|
2337
2384
|
You can find useful middleware in
|
|
2338
|
-
[rack](https://github.com/rack/rack/tree/
|
|
2385
|
+
[rack](https://github.com/rack/rack/tree/main/lib/rack),
|
|
2339
2386
|
[rack-contrib](https://github.com/rack/rack-contrib#readme),
|
|
2340
2387
|
or in the [Rack wiki](https://github.com/rack/rack/wiki/List-of-Middleware).
|
|
2341
2388
|
|
|
@@ -2343,7 +2390,7 @@ or in the [Rack wiki](https://github.com/rack/rack/wiki/List-of-Middleware).
|
|
|
2343
2390
|
|
|
2344
2391
|
Sinatra tests can be written using any Rack-based testing library or
|
|
2345
2392
|
framework.
|
|
2346
|
-
[Rack::Test](
|
|
2393
|
+
[Rack::Test](https://www.rubydoc.info/github/rack/rack-test/main/frames)
|
|
2347
2394
|
is recommended:
|
|
2348
2395
|
|
|
2349
2396
|
```ruby
|
|
@@ -2767,68 +2814,30 @@ by Konstantin_
|
|
|
2767
2814
|
Sinatra doesn't impose any concurrency model but leaves that to the
|
|
2768
2815
|
underlying Rack handler (server) like Puma or WEBrick. Sinatra
|
|
2769
2816
|
itself is thread-safe, so there won't be any problem if the Rack handler
|
|
2770
|
-
uses a threaded model of concurrency.
|
|
2771
|
-
the server, you'd have to specify the correct invocation method for the
|
|
2772
|
-
specific Rack handler. The following example is a demonstration of how
|
|
2773
|
-
to start a multi-threaded Rainbows server:
|
|
2774
|
-
|
|
2775
|
-
```ruby
|
|
2776
|
-
# config.ru
|
|
2777
|
-
|
|
2778
|
-
require 'sinatra/base'
|
|
2779
|
-
|
|
2780
|
-
class App < Sinatra::Base
|
|
2781
|
-
get '/' do
|
|
2782
|
-
"Hello, World"
|
|
2783
|
-
end
|
|
2784
|
-
end
|
|
2785
|
-
|
|
2786
|
-
run App
|
|
2787
|
-
```
|
|
2788
|
-
|
|
2789
|
-
```ruby
|
|
2790
|
-
# rainbows.conf
|
|
2791
|
-
|
|
2792
|
-
# Rainbows configurator is based on Unicorn.
|
|
2793
|
-
Rainbows! do
|
|
2794
|
-
use :ThreadSpawn
|
|
2795
|
-
end
|
|
2796
|
-
```
|
|
2797
|
-
|
|
2798
|
-
To start the server, the command would be:
|
|
2799
|
-
|
|
2800
|
-
```shell
|
|
2801
|
-
rainbows -c rainbows.conf
|
|
2802
|
-
```
|
|
2817
|
+
uses a threaded model of concurrency.
|
|
2803
2818
|
|
|
2804
2819
|
## Requirement
|
|
2805
2820
|
|
|
2806
2821
|
The following Ruby versions are officially supported:
|
|
2807
2822
|
<dl>
|
|
2808
|
-
<dt>Ruby
|
|
2823
|
+
<dt>Ruby</dt>
|
|
2809
2824
|
<dd>
|
|
2810
|
-
|
|
2811
|
-
drop official support for it.
|
|
2825
|
+
<a href="https://www.ruby-lang.org/en/downloads/">The stable releases</a> are fully supported and recommended.
|
|
2812
2826
|
</dd>
|
|
2813
2827
|
|
|
2814
|
-
<dt>
|
|
2828
|
+
<dt>TruffleRuby</dt>
|
|
2815
2829
|
<dd>
|
|
2816
|
-
|
|
2817
|
-
<tt>gem install puma</tt>.
|
|
2830
|
+
The latest stable release of TruffleRuby is supported.
|
|
2818
2831
|
</dd>
|
|
2819
2832
|
|
|
2820
2833
|
<dt>JRuby</dt>
|
|
2821
2834
|
<dd>
|
|
2822
|
-
The latest stable release of JRuby is
|
|
2823
|
-
recommended to use C extensions with JRuby.
|
|
2824
|
-
<tt>gem install trinidad</tt>.
|
|
2835
|
+
The latest stable release of JRuby is supported. It is not
|
|
2836
|
+
recommended to use C extensions with JRuby.
|
|
2825
2837
|
</dd>
|
|
2826
2838
|
</dl>
|
|
2827
2839
|
|
|
2828
|
-
Versions of Ruby before 2.
|
|
2829
|
-
|
|
2830
|
-
We also keep an eye on upcoming Ruby versions. Expect upcoming
|
|
2831
|
-
3.x releases to be fully supported.
|
|
2840
|
+
Versions of Ruby before 2.7.8 are no longer supported as of Sinatra 4.0.0.
|
|
2832
2841
|
|
|
2833
2842
|
Sinatra should work on any operating system supported by the chosen Ruby
|
|
2834
2843
|
implementation.
|
|
@@ -2838,7 +2847,7 @@ Running Sinatra on a not officially supported Ruby flavor means that if things o
|
|
|
2838
2847
|
## The Bleeding Edge
|
|
2839
2848
|
|
|
2840
2849
|
If you would like to use Sinatra's latest bleeding-edge code, feel free
|
|
2841
|
-
to run your application against the
|
|
2850
|
+
to run your application against the main branch, it should be rather
|
|
2842
2851
|
stable.
|
|
2843
2852
|
|
|
2844
2853
|
We also push out prerelease gems from time to time, so you can do a
|
|
@@ -2887,20 +2896,19 @@ SemVerTag.
|
|
|
2887
2896
|
|
|
2888
2897
|
## Further Reading
|
|
2889
2898
|
|
|
2890
|
-
* [Project Website](
|
|
2899
|
+
* [Project Website](https://sinatrarb.com/) - Additional documentation,
|
|
2891
2900
|
news, and links to other resources.
|
|
2892
|
-
* [Contributing](
|
|
2901
|
+
* [Contributing](https://sinatrarb.com/contributing) - Find a bug? Need
|
|
2893
2902
|
help? Have a patch?
|
|
2894
2903
|
* [Issue tracker](https://github.com/sinatra/sinatra/issues)
|
|
2895
2904
|
* [Twitter](https://twitter.com/sinatra)
|
|
2896
2905
|
* [Mailing List](https://groups.google.com/forum/#!forum/sinatrarb)
|
|
2897
2906
|
* IRC: [#sinatra](irc://chat.freenode.net/#sinatra) on [Freenode](https://freenode.net)
|
|
2898
|
-
* [Sinatra & Friends](https://
|
|
2899
|
-
([get an invite](https://sinatra-slack.herokuapp.com/))
|
|
2907
|
+
* [Sinatra & Friends](https://discord.gg/ncjsfsNHh7) on Discord
|
|
2900
2908
|
* [Sinatra Book](https://github.com/sinatra/sinatra-book) - Cookbook Tutorial
|
|
2901
2909
|
* [Sinatra Recipes](http://recipes.sinatrarb.com/) - Community contributed
|
|
2902
2910
|
recipes
|
|
2903
|
-
* API documentation for the [latest release](
|
|
2904
|
-
or the [current HEAD](
|
|
2905
|
-
[RubyDoc](
|
|
2911
|
+
* API documentation for the [latest release](https://www.rubydoc.info/gems/sinatra)
|
|
2912
|
+
or the [current HEAD](https://www.rubydoc.info/github/sinatra/sinatra) on
|
|
2913
|
+
[RubyDoc](https://www.rubydoc.info/)
|
|
2906
2914
|
* [CI Actions](https://github.com/sinatra/sinatra/actions)
|
data/Rakefile
CHANGED
|
@@ -1,14 +1,11 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require 'rake/clean'
|
|
4
|
-
require '
|
|
4
|
+
require 'minitest/test_task'
|
|
5
5
|
require 'fileutils'
|
|
6
6
|
require 'date'
|
|
7
7
|
|
|
8
8
|
task default: :test
|
|
9
|
-
task spec: :test
|
|
10
|
-
|
|
11
|
-
CLEAN.include '**/*.rbc'
|
|
12
9
|
|
|
13
10
|
def source_version
|
|
14
11
|
@source_version ||= File.read(File.expand_path('VERSION', __dir__)).strip
|
|
@@ -24,27 +21,20 @@ def prev_version
|
|
|
24
21
|
source_version.gsub(/\d+$/) { |s| s.to_i - 1 }
|
|
25
22
|
end
|
|
26
23
|
|
|
27
|
-
#
|
|
24
|
+
# Tests ===============================================================
|
|
28
25
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
t.ruby_opts = ['-r rubygems'] if defined? Gem
|
|
26
|
+
Minitest::TestTask.create # Default `test` task
|
|
27
|
+
Minitest::TestTask.create(:'test:core') do |t|
|
|
32
28
|
t.warning = true
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
Rake::TestTask.new(:'test:core') do |t|
|
|
36
|
-
core_tests = %w[
|
|
29
|
+
t.test_globs = %w[
|
|
37
30
|
base delegator encoding extensions filter
|
|
38
31
|
helpers mapped_error middleware rdoc
|
|
39
32
|
readme request response result route_added_hook
|
|
40
33
|
routing server settings sinatra static templates
|
|
41
|
-
]
|
|
42
|
-
t.test_files = core_tests.map { |n| "test/#{n}_test.rb" }
|
|
43
|
-
t.ruby_opts = ['-r rubygems'] if defined? Gem
|
|
44
|
-
t.warning = true
|
|
34
|
+
].map { |n| "test/#{n}_test.rb" }
|
|
45
35
|
end
|
|
46
36
|
|
|
47
|
-
#
|
|
37
|
+
# Test code coverage ==================================================
|
|
48
38
|
|
|
49
39
|
namespace :test do
|
|
50
40
|
desc 'Measures test coverage'
|
|
@@ -54,6 +44,7 @@ namespace :test do
|
|
|
54
44
|
Rake::Task['test'].invoke
|
|
55
45
|
end
|
|
56
46
|
end
|
|
47
|
+
CLEAN.include('coverage')
|
|
57
48
|
|
|
58
49
|
# Website =============================================================
|
|
59
50
|
|
|
@@ -200,7 +191,7 @@ if defined?(Gem)
|
|
|
200
191
|
end
|
|
201
192
|
end
|
|
202
193
|
|
|
203
|
-
desc 'Commits the version to
|
|
194
|
+
desc 'Commits the version to git (no push)'
|
|
204
195
|
task :commit_version do
|
|
205
196
|
%w[
|
|
206
197
|
lib/sinatra
|
|
@@ -212,10 +203,22 @@ if defined?(Gem)
|
|
|
212
203
|
end
|
|
213
204
|
|
|
214
205
|
sh <<-SH
|
|
215
|
-
git commit --allow-empty
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
206
|
+
git commit --allow-empty --all --message '#{source_version} release'
|
|
207
|
+
SH
|
|
208
|
+
end
|
|
209
|
+
|
|
210
|
+
desc 'Tags the version in git (no push)'
|
|
211
|
+
task :tag_version do
|
|
212
|
+
sh <<-SH
|
|
213
|
+
git tag --sign v#{source_version} --message '#{source_version} release'
|
|
214
|
+
SH
|
|
215
|
+
end
|
|
216
|
+
|
|
217
|
+
desc 'Watch the release workflow run'
|
|
218
|
+
task :watch do
|
|
219
|
+
sh <<-SH
|
|
220
|
+
runId=$(gh run list --workflow=release.yml --limit 1 --json databaseId --jq '.[].databaseId')
|
|
221
|
+
gh run watch --interval 1 --exit-status $runId
|
|
219
222
|
SH
|
|
220
223
|
end
|
|
221
224
|
|
data/SECURITY.md
CHANGED
|
@@ -6,7 +6,7 @@ After the initial reply to your report the security team will endeavor to keep y
|
|
|
6
6
|
|
|
7
7
|
If you have not received a reply to your email within 48 hours, or have not heard from the security team for the past five days there are a few steps you can take:
|
|
8
8
|
|
|
9
|
-
*
|
|
9
|
+
* Reach out to us on [discord](https://discord.gg/ncjsfsNHh7)
|
|
10
10
|
|
|
11
11
|
## Disclosure Policy
|
|
12
12
|
|
data/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
4.0.0
|
data/examples/chat.rb
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
#!/usr/bin/env ruby -I ../lib -I lib
|
|
2
2
|
# frozen_string_literal: true
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
# This example does *not* work properly with WEBrick or other
|
|
5
|
+
# servers that buffer output. To shut down the server, close any
|
|
6
|
+
# open browser tabs that are connected to the chat server.
|
|
5
7
|
|
|
6
8
|
require 'sinatra'
|
|
7
|
-
set :server, :
|
|
8
|
-
connections =
|
|
9
|
+
set :server, :puma
|
|
10
|
+
connections = Set.new
|
|
9
11
|
|
|
10
12
|
get '/' do
|
|
11
13
|
halt erb(:login) unless params[:user]
|
|
@@ -14,13 +16,22 @@ end
|
|
|
14
16
|
|
|
15
17
|
get '/stream', provides: 'text/event-stream' do
|
|
16
18
|
stream :keep_open do |out|
|
|
17
|
-
connections
|
|
18
|
-
|
|
19
|
+
if connections.add?(out)
|
|
20
|
+
out.callback { connections.delete(out) }
|
|
21
|
+
end
|
|
22
|
+
out << "heartbeat:\n"
|
|
23
|
+
sleep 1
|
|
24
|
+
rescue
|
|
25
|
+
out.close
|
|
19
26
|
end
|
|
20
27
|
end
|
|
21
28
|
|
|
22
29
|
post '/' do
|
|
23
|
-
connections.each
|
|
30
|
+
connections.each do |out|
|
|
31
|
+
out << "data: #{params[:msg]}\n\n"
|
|
32
|
+
rescue
|
|
33
|
+
out.close
|
|
34
|
+
end
|
|
24
35
|
204 # response without entity body
|
|
25
36
|
end
|
|
26
37
|
|
|
@@ -37,10 +48,10 @@ __END__
|
|
|
37
48
|
</html>
|
|
38
49
|
|
|
39
50
|
@@ login
|
|
40
|
-
<form action=
|
|
51
|
+
<form action="/">
|
|
41
52
|
<label for='user'>User Name:</label>
|
|
42
|
-
<input name=
|
|
43
|
-
<input type=
|
|
53
|
+
<input name="user" value="" />
|
|
54
|
+
<input type="submit" value="GO!" />
|
|
44
55
|
</form>
|
|
45
56
|
|
|
46
57
|
@@ chat
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
#!/usr/bin/env ruby -I ../lib -I lib
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
require 'sinatra'
|
|
5
|
+
|
|
6
|
+
get('/') do
|
|
7
|
+
'This shows how lifecycle events work'
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
on_start do
|
|
11
|
+
puts "=============="
|
|
12
|
+
puts " Booting up"
|
|
13
|
+
puts "=============="
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
on_stop do
|
|
17
|
+
puts "================="
|
|
18
|
+
puts " Shutting down"
|
|
19
|
+
puts "================="
|
|
20
|
+
end
|
data/examples/stream.ru
CHANGED
|
@@ -4,10 +4,8 @@
|
|
|
4
4
|
#
|
|
5
5
|
# run *one* of these:
|
|
6
6
|
#
|
|
7
|
-
# rackup -s mongrel stream.ru # gem install mongrel
|
|
8
7
|
# unicorn stream.ru # gem install unicorn
|
|
9
8
|
# puma stream.ru # gem install puma
|
|
10
|
-
# rainbows -c rainbows.conf stream.ru # gem install rainbows eventmachine
|
|
11
9
|
|
|
12
10
|
require 'sinatra/base'
|
|
13
11
|
|