sinatra-base 1.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.
- data/AUTHORS +43 -0
- data/CHANGES +511 -0
- data/LICENSE +22 -0
- data/README.jp.rdoc +552 -0
- data/README.rdoc +636 -0
- data/Rakefile +116 -0
- data/lib/sinatra.rb +7 -0
- data/lib/sinatra/base.rb +1167 -0
- data/lib/sinatra/images/404.png +0 -0
- data/lib/sinatra/images/500.png +0 -0
- data/lib/sinatra/main.rb +28 -0
- data/lib/sinatra/showexceptions.rb +307 -0
- data/lib/sinatra/tilt.rb +746 -0
- data/sinatra-base.gemspec +94 -0
- data/test/base_test.rb +160 -0
- data/test/builder_test.rb +65 -0
- data/test/contest.rb +64 -0
- data/test/erb_test.rb +81 -0
- data/test/erubis_test.rb +82 -0
- data/test/extensions_test.rb +100 -0
- data/test/filter_test.rb +221 -0
- data/test/haml_test.rb +95 -0
- data/test/helper.rb +76 -0
- data/test/helpers_test.rb +582 -0
- data/test/less_test.rb +37 -0
- data/test/mapped_error_test.rb +197 -0
- data/test/middleware_test.rb +68 -0
- data/test/public/favicon.ico +0 -0
- data/test/request_test.rb +33 -0
- data/test/response_test.rb +42 -0
- data/test/result_test.rb +98 -0
- data/test/route_added_hook_test.rb +59 -0
- data/test/routing_test.rb +860 -0
- data/test/sass_test.rb +85 -0
- data/test/server_test.rb +47 -0
- data/test/settings_test.rb +368 -0
- data/test/sinatra_test.rb +13 -0
- data/test/static_test.rb +93 -0
- data/test/templates_test.rb +159 -0
- data/test/views/error.builder +3 -0
- data/test/views/error.erb +3 -0
- data/test/views/error.erubis +3 -0
- data/test/views/error.haml +3 -0
- data/test/views/error.sass +2 -0
- data/test/views/foo/hello.test +1 -0
- data/test/views/hello.builder +1 -0
- data/test/views/hello.erb +1 -0
- data/test/views/hello.erubis +1 -0
- data/test/views/hello.haml +1 -0
- data/test/views/hello.less +5 -0
- data/test/views/hello.sass +2 -0
- data/test/views/hello.test +1 -0
- data/test/views/layout2.builder +3 -0
- data/test/views/layout2.erb +2 -0
- data/test/views/layout2.erubis +2 -0
- data/test/views/layout2.haml +2 -0
- data/test/views/layout2.test +1 -0
- metadata +257 -0
data/README.rdoc
ADDED
@@ -0,0 +1,636 @@
|
|
1
|
+
= Sinatra
|
2
|
+
|
3
|
+
Sinatra is a DSL for quickly creating web applications in Ruby with minimal
|
4
|
+
effort:
|
5
|
+
|
6
|
+
# myapp.rb
|
7
|
+
require 'rubygems'
|
8
|
+
require 'sinatra'
|
9
|
+
get '/' do
|
10
|
+
'Hello world!'
|
11
|
+
end
|
12
|
+
|
13
|
+
Install the gem and run with:
|
14
|
+
|
15
|
+
sudo gem install sinatra
|
16
|
+
ruby myapp.rb
|
17
|
+
|
18
|
+
View at: http://localhost:4567
|
19
|
+
|
20
|
+
== Routes
|
21
|
+
|
22
|
+
In Sinatra, a route is an HTTP method paired with an URL matching pattern.
|
23
|
+
Each route is associated with a block:
|
24
|
+
|
25
|
+
get '/' do
|
26
|
+
.. show something ..
|
27
|
+
end
|
28
|
+
|
29
|
+
post '/' do
|
30
|
+
.. create something ..
|
31
|
+
end
|
32
|
+
|
33
|
+
put '/' do
|
34
|
+
.. update something ..
|
35
|
+
end
|
36
|
+
|
37
|
+
delete '/' do
|
38
|
+
.. annihilate something ..
|
39
|
+
end
|
40
|
+
|
41
|
+
Routes are matched in the order they are defined. The first route that
|
42
|
+
matches the request is invoked.
|
43
|
+
|
44
|
+
Route patterns may include named parameters, accessible via the
|
45
|
+
<tt>params</tt> hash:
|
46
|
+
|
47
|
+
get '/hello/:name' do
|
48
|
+
# matches "GET /hello/foo" and "GET /hello/bar"
|
49
|
+
# params[:name] is 'foo' or 'bar'
|
50
|
+
"Hello #{params[:name]}!"
|
51
|
+
end
|
52
|
+
|
53
|
+
You can also access named parameters via block parameters:
|
54
|
+
|
55
|
+
get '/hello/:name' do |n|
|
56
|
+
"Hello #{n}!"
|
57
|
+
end
|
58
|
+
|
59
|
+
Route patterns may also include splat (or wildcard) parameters, accessible
|
60
|
+
via the <tt>params[:splat]</tt> array.
|
61
|
+
|
62
|
+
get '/say/*/to/*' do
|
63
|
+
# matches /say/hello/to/world
|
64
|
+
params[:splat] # => ["hello", "world"]
|
65
|
+
end
|
66
|
+
|
67
|
+
get '/download/*.*' do
|
68
|
+
# matches /download/path/to/file.xml
|
69
|
+
params[:splat] # => ["path/to/file", "xml"]
|
70
|
+
end
|
71
|
+
|
72
|
+
Route matching with Regular Expressions:
|
73
|
+
|
74
|
+
get %r{/hello/([\w]+)} do
|
75
|
+
"Hello, #{params[:captures].first}!"
|
76
|
+
end
|
77
|
+
|
78
|
+
Or with a block parameter:
|
79
|
+
|
80
|
+
get %r{/hello/([\w]+)} do |c|
|
81
|
+
"Hello, #{c}!"
|
82
|
+
end
|
83
|
+
|
84
|
+
Routes may include a variety of matching conditions, such as the user agent:
|
85
|
+
|
86
|
+
get '/foo', :agent => /Songbird (\d\.\d)[\d\/]*?/ do
|
87
|
+
"You're using Songbird version #{params[:agent][0]}"
|
88
|
+
end
|
89
|
+
|
90
|
+
get '/foo' do
|
91
|
+
# Matches non-songbird browsers
|
92
|
+
end
|
93
|
+
|
94
|
+
== Static Files
|
95
|
+
|
96
|
+
Static files are served from the <tt>./public</tt> directory. You can specify
|
97
|
+
a different location by setting the <tt>:public</tt> option:
|
98
|
+
|
99
|
+
set :public, File.dirname(__FILE__) + '/static'
|
100
|
+
|
101
|
+
Note that the public directory name is not included in the URL. A file
|
102
|
+
<tt>./public/css/style.css</tt> is made available as
|
103
|
+
<tt>http://example.com/css/style.css</tt>.
|
104
|
+
|
105
|
+
== Views / Templates
|
106
|
+
|
107
|
+
Templates are assumed to be located directly under the <tt>./views</tt>
|
108
|
+
directory. To use a different views directory:
|
109
|
+
|
110
|
+
set :views, File.dirname(__FILE__) + '/templates'
|
111
|
+
|
112
|
+
One important thing to remember is that you always have to reference
|
113
|
+
templates with symbols, even if they're in a subdirectory (in this
|
114
|
+
case use <tt>:'subdir/template'</tt>). Rendering methods will render
|
115
|
+
any strings passed to them directly.
|
116
|
+
|
117
|
+
=== Haml Templates
|
118
|
+
|
119
|
+
The haml gem/library is required to render HAML templates:
|
120
|
+
|
121
|
+
## You'll need to require haml in your app
|
122
|
+
require 'haml'
|
123
|
+
|
124
|
+
get '/' do
|
125
|
+
haml :index
|
126
|
+
end
|
127
|
+
|
128
|
+
Renders <tt>./views/index.haml</tt>.
|
129
|
+
|
130
|
+
{Haml's options}[http://haml.hamptoncatlin.com/docs/rdoc/classes/Haml.html]
|
131
|
+
can be set globally through Sinatra's configurations,
|
132
|
+
see {Options and Configurations}[http://www.sinatrarb.com/configuration.html],
|
133
|
+
and overridden on an individual basis.
|
134
|
+
|
135
|
+
set :haml, {:format => :html5 } # default Haml format is :xhtml
|
136
|
+
|
137
|
+
get '/' do
|
138
|
+
haml :index, :haml_options => {:format => :html4 } # overridden
|
139
|
+
end
|
140
|
+
|
141
|
+
|
142
|
+
=== Erb Templates
|
143
|
+
|
144
|
+
## You'll need to require erb in your app
|
145
|
+
require 'erb'
|
146
|
+
|
147
|
+
get '/' do
|
148
|
+
erb :index
|
149
|
+
end
|
150
|
+
|
151
|
+
Renders <tt>./views/index.erb</tt>
|
152
|
+
|
153
|
+
=== Erubis
|
154
|
+
|
155
|
+
The erubis gem/library is required to render builder templates:
|
156
|
+
|
157
|
+
## You'll need to require erubis in your app
|
158
|
+
require 'erubis'
|
159
|
+
|
160
|
+
get '/' do
|
161
|
+
erubis :index
|
162
|
+
end
|
163
|
+
|
164
|
+
Renders <tt>./views/index.erubis</tt>
|
165
|
+
|
166
|
+
=== Builder Templates
|
167
|
+
|
168
|
+
The builder gem/library is required to render builder templates:
|
169
|
+
|
170
|
+
## You'll need to require builder in your app
|
171
|
+
require 'builder'
|
172
|
+
|
173
|
+
get '/' do
|
174
|
+
content_type 'application/xml', :charset => 'utf-8'
|
175
|
+
builder :index
|
176
|
+
end
|
177
|
+
|
178
|
+
Renders <tt>./views/index.builder</tt>.
|
179
|
+
|
180
|
+
=== Sass Templates
|
181
|
+
|
182
|
+
The sass gem/library is required to render Sass templates:
|
183
|
+
|
184
|
+
## You'll need to require haml or sass in your app
|
185
|
+
require 'sass'
|
186
|
+
|
187
|
+
get '/stylesheet.css' do
|
188
|
+
content_type 'text/css', :charset => 'utf-8'
|
189
|
+
sass :stylesheet
|
190
|
+
end
|
191
|
+
|
192
|
+
Renders <tt>./views/stylesheet.sass</tt>.
|
193
|
+
|
194
|
+
{Sass' options}[http://haml.hamptoncatlin.com/docs/rdoc/classes/Sass.html]
|
195
|
+
can be set globally through Sinatra's configurations,
|
196
|
+
see {Options and Configurations}[http://www.sinatrarb.com/configuration.html],
|
197
|
+
and overridden on an individual basis.
|
198
|
+
|
199
|
+
set :sass, {:style => :compact } # default Sass style is :nested
|
200
|
+
|
201
|
+
get '/stylesheet.css' do
|
202
|
+
content_type 'text/css', :charset => 'utf-8'
|
203
|
+
sass :stylesheet, :style => :expanded # overridden
|
204
|
+
end
|
205
|
+
|
206
|
+
=== Less Templates
|
207
|
+
|
208
|
+
The less gem/library is required to render Less templates:
|
209
|
+
|
210
|
+
## You'll need to require less in your app
|
211
|
+
require 'less'
|
212
|
+
|
213
|
+
get '/stylesheet.css' do
|
214
|
+
content_type 'text/css', :charset => 'utf-8'
|
215
|
+
less :stylesheet
|
216
|
+
end
|
217
|
+
|
218
|
+
Renders <tt>./views/stylesheet.less</tt>.
|
219
|
+
|
220
|
+
=== Inline Templates
|
221
|
+
|
222
|
+
get '/' do
|
223
|
+
haml '%div.title Hello World'
|
224
|
+
end
|
225
|
+
|
226
|
+
Renders the inlined template string.
|
227
|
+
|
228
|
+
=== Accessing Variables in Templates
|
229
|
+
|
230
|
+
Templates are evaluated within the same context as route handlers. Instance
|
231
|
+
variables set in route handlers are direcly accessible by templates:
|
232
|
+
|
233
|
+
get '/:id' do
|
234
|
+
@foo = Foo.find(params[:id])
|
235
|
+
haml '%h1= @foo.name'
|
236
|
+
end
|
237
|
+
|
238
|
+
Or, specify an explicit Hash of local variables:
|
239
|
+
|
240
|
+
get '/:id' do
|
241
|
+
foo = Foo.find(params[:id])
|
242
|
+
haml '%h1= foo.name', :locals => { :foo => foo }
|
243
|
+
end
|
244
|
+
|
245
|
+
This is typically used when rendering templates as partials from within
|
246
|
+
other templates.
|
247
|
+
|
248
|
+
=== Inline Templates
|
249
|
+
|
250
|
+
Templates may be defined at the end of the source file:
|
251
|
+
|
252
|
+
require 'rubygems'
|
253
|
+
require 'sinatra'
|
254
|
+
|
255
|
+
get '/' do
|
256
|
+
haml :index
|
257
|
+
end
|
258
|
+
|
259
|
+
__END__
|
260
|
+
|
261
|
+
@@ layout
|
262
|
+
%html
|
263
|
+
= yield
|
264
|
+
|
265
|
+
@@ index
|
266
|
+
%div.title Hello world!!!!!
|
267
|
+
|
268
|
+
NOTE: Inline templates defined in the source file that requires sinatra
|
269
|
+
are automatically loaded. Call `enable :inline_templates` explicitly if you
|
270
|
+
have inline templates in other source files.
|
271
|
+
|
272
|
+
=== Named Templates
|
273
|
+
|
274
|
+
Templates may also be defined using the top-level <tt>template</tt> method:
|
275
|
+
|
276
|
+
template :layout do
|
277
|
+
"%html\n =yield\n"
|
278
|
+
end
|
279
|
+
|
280
|
+
template :index do
|
281
|
+
'%div.title Hello World!'
|
282
|
+
end
|
283
|
+
|
284
|
+
get '/' do
|
285
|
+
haml :index
|
286
|
+
end
|
287
|
+
|
288
|
+
If a template named "layout" exists, it will be used each time a template
|
289
|
+
is rendered. You can disable layouts by passing <tt>:layout => false</tt>.
|
290
|
+
|
291
|
+
get '/' do
|
292
|
+
haml :index, :layout => !request.xhr?
|
293
|
+
end
|
294
|
+
|
295
|
+
== Helpers
|
296
|
+
|
297
|
+
Use the top-level <tt>helpers</tt> method to define helper methods for use in
|
298
|
+
route handlers and templates:
|
299
|
+
|
300
|
+
helpers do
|
301
|
+
def bar(name)
|
302
|
+
"#{name}bar"
|
303
|
+
end
|
304
|
+
end
|
305
|
+
|
306
|
+
get '/:name' do
|
307
|
+
bar(params[:name])
|
308
|
+
end
|
309
|
+
|
310
|
+
== Filters
|
311
|
+
|
312
|
+
Before filters are evaluated before each request within the context of the
|
313
|
+
request and can modify the request and response. Instance variables set in
|
314
|
+
filters are accessible by routes and templates:
|
315
|
+
|
316
|
+
before do
|
317
|
+
@note = 'Hi!'
|
318
|
+
request.path_info = '/foo/bar/baz'
|
319
|
+
end
|
320
|
+
|
321
|
+
get '/foo/*' do
|
322
|
+
@note #=> 'Hi!'
|
323
|
+
params[:splat] #=> 'bar/baz'
|
324
|
+
end
|
325
|
+
|
326
|
+
After filter are evaluated after each request within the context of the
|
327
|
+
request and can also modify the request and response. Instance variables
|
328
|
+
set in before filters and routes are accessible by after filters:
|
329
|
+
|
330
|
+
after do
|
331
|
+
puts response.status
|
332
|
+
end
|
333
|
+
|
334
|
+
== Halting
|
335
|
+
|
336
|
+
To immediately stop a request within a filter or route use:
|
337
|
+
|
338
|
+
halt
|
339
|
+
|
340
|
+
You can also specify the status when halting ...
|
341
|
+
|
342
|
+
halt 410
|
343
|
+
|
344
|
+
Or the body ...
|
345
|
+
|
346
|
+
halt 'this will be the body'
|
347
|
+
|
348
|
+
Or both ...
|
349
|
+
|
350
|
+
halt 401, 'go away!'
|
351
|
+
|
352
|
+
With headers ...
|
353
|
+
|
354
|
+
halt 402, {'Content-Type' => 'text/plain'}, 'revenge'
|
355
|
+
|
356
|
+
== Passing
|
357
|
+
|
358
|
+
A route can punt processing to the next matching route using <tt>pass</tt>:
|
359
|
+
|
360
|
+
get '/guess/:who' do
|
361
|
+
pass unless params[:who] == 'Frank'
|
362
|
+
'You got me!'
|
363
|
+
end
|
364
|
+
|
365
|
+
get '/guess/*' do
|
366
|
+
'You missed!'
|
367
|
+
end
|
368
|
+
|
369
|
+
The route block is immediately exited and control continues with the next
|
370
|
+
matching route. If no matching route is found, a 404 is returned.
|
371
|
+
|
372
|
+
== Configuration
|
373
|
+
|
374
|
+
Run once, at startup, in any environment:
|
375
|
+
|
376
|
+
configure do
|
377
|
+
...
|
378
|
+
end
|
379
|
+
|
380
|
+
Run only when the environment (RACK_ENV environment variable) is set to
|
381
|
+
<tt>:production</tt>:
|
382
|
+
|
383
|
+
configure :production do
|
384
|
+
...
|
385
|
+
end
|
386
|
+
|
387
|
+
Run when the environment is set to either <tt>:production</tt> or
|
388
|
+
<tt>:test</tt>:
|
389
|
+
|
390
|
+
configure :production, :test do
|
391
|
+
...
|
392
|
+
end
|
393
|
+
|
394
|
+
== Error handling
|
395
|
+
|
396
|
+
Error handlers run within the same context as routes and before filters, which
|
397
|
+
means you get all the goodies it has to offer, like <tt>haml</tt>, <tt>erb</tt>,
|
398
|
+
<tt>halt</tt>, etc.
|
399
|
+
|
400
|
+
=== Not Found
|
401
|
+
|
402
|
+
When a <tt>Sinatra::NotFound</tt> exception is raised, or the response's status
|
403
|
+
code is 404, the <tt>not_found</tt> handler is invoked:
|
404
|
+
|
405
|
+
not_found do
|
406
|
+
'This is nowhere to be found'
|
407
|
+
end
|
408
|
+
|
409
|
+
=== Error
|
410
|
+
|
411
|
+
The +error+ handler is invoked any time an exception is raised from a route
|
412
|
+
block or a filter. The exception object can be obtained from the
|
413
|
+
<tt>sinatra.error</tt> Rack variable:
|
414
|
+
|
415
|
+
error do
|
416
|
+
'Sorry there was a nasty error - ' + env['sinatra.error'].name
|
417
|
+
end
|
418
|
+
|
419
|
+
Custom errors:
|
420
|
+
|
421
|
+
error MyCustomError do
|
422
|
+
'So what happened was...' + request.env['sinatra.error'].message
|
423
|
+
end
|
424
|
+
|
425
|
+
Then, if this happens:
|
426
|
+
|
427
|
+
get '/' do
|
428
|
+
raise MyCustomError, 'something bad'
|
429
|
+
end
|
430
|
+
|
431
|
+
You get this:
|
432
|
+
|
433
|
+
So what happened was... something bad
|
434
|
+
|
435
|
+
Alternatively, you can install error handler for a status code:
|
436
|
+
|
437
|
+
error 403 do
|
438
|
+
'Access forbidden'
|
439
|
+
end
|
440
|
+
|
441
|
+
get '/secret' do
|
442
|
+
403
|
443
|
+
end
|
444
|
+
|
445
|
+
Or a range:
|
446
|
+
|
447
|
+
error 400..510 do
|
448
|
+
'Boom'
|
449
|
+
end
|
450
|
+
|
451
|
+
Sinatra installs special <tt>not_found</tt> and <tt>error</tt> handlers when
|
452
|
+
running under the development environment.
|
453
|
+
|
454
|
+
== Mime types
|
455
|
+
|
456
|
+
When using <tt>send_file</tt> or static files you may have mime types Sinatra
|
457
|
+
doesn't understand. Use +mime_type+ to register them by file extension:
|
458
|
+
|
459
|
+
mime_type :foo, 'text/foo'
|
460
|
+
|
461
|
+
You can also use it with the +content_type+ helper:
|
462
|
+
|
463
|
+
content_type :foo
|
464
|
+
|
465
|
+
== Rack Middleware
|
466
|
+
|
467
|
+
Sinatra rides on Rack[http://rack.rubyforge.org/], a minimal standard
|
468
|
+
interface for Ruby web frameworks. One of Rack's most interesting capabilities
|
469
|
+
for application developers is support for "middleware" -- components that sit
|
470
|
+
between the server and your application monitoring and/or manipulating the
|
471
|
+
HTTP request/response to provide various types of common functionality.
|
472
|
+
|
473
|
+
Sinatra makes building Rack middleware pipelines a cinch via a top-level
|
474
|
+
+use+ method:
|
475
|
+
|
476
|
+
require 'sinatra'
|
477
|
+
require 'my_custom_middleware'
|
478
|
+
|
479
|
+
use Rack::Lint
|
480
|
+
use MyCustomMiddleware
|
481
|
+
|
482
|
+
get '/hello' do
|
483
|
+
'Hello World'
|
484
|
+
end
|
485
|
+
|
486
|
+
The semantics of +use+ are identical to those defined for the
|
487
|
+
Rack::Builder[http://rack.rubyforge.org/doc/classes/Rack/Builder.html] DSL
|
488
|
+
(most frequently used from rackup files). For example, the +use+ method
|
489
|
+
accepts multiple/variable args as well as blocks:
|
490
|
+
|
491
|
+
use Rack::Auth::Basic do |username, password|
|
492
|
+
username == 'admin' && password == 'secret'
|
493
|
+
end
|
494
|
+
|
495
|
+
Rack is distributed with a variety of standard middleware for logging,
|
496
|
+
debugging, URL routing, authentication, and session handling. Sinatra uses
|
497
|
+
many of of these components automatically based on configuration so you
|
498
|
+
typically don't have to +use+ them explicitly.
|
499
|
+
|
500
|
+
== Testing
|
501
|
+
|
502
|
+
Sinatra tests can be written using any Rack-based testing library
|
503
|
+
or framework. {Rack::Test}[http://gitrdoc.com/brynary/rack-test] is
|
504
|
+
recommended:
|
505
|
+
|
506
|
+
require 'my_sinatra_app'
|
507
|
+
require 'rack/test'
|
508
|
+
|
509
|
+
class MyAppTest < Test::Unit::TestCase
|
510
|
+
include Rack::Test::Methods
|
511
|
+
|
512
|
+
def app
|
513
|
+
Sinatra::Application
|
514
|
+
end
|
515
|
+
|
516
|
+
def test_my_default
|
517
|
+
get '/'
|
518
|
+
assert_equal 'Hello World!', last_response.body
|
519
|
+
end
|
520
|
+
|
521
|
+
def test_with_params
|
522
|
+
get '/meet', :name => 'Frank'
|
523
|
+
assert_equal 'Hello Frank!', last_response.body
|
524
|
+
end
|
525
|
+
|
526
|
+
def test_with_rack_env
|
527
|
+
get '/', {}, 'HTTP_USER_AGENT' => 'Songbird'
|
528
|
+
assert_equal "You're using Songbird!", last_response.body
|
529
|
+
end
|
530
|
+
end
|
531
|
+
|
532
|
+
NOTE: The built-in Sinatra::Test module and Sinatra::TestHarness class
|
533
|
+
are deprecated as of the 0.9.2 release.
|
534
|
+
|
535
|
+
== Sinatra::Base - Middleware, Libraries, and Modular Apps
|
536
|
+
|
537
|
+
Defining your app at the top-level works well for micro-apps but has
|
538
|
+
considerable drawbacks when building reuseable components such as Rack
|
539
|
+
middleware, Rails metal, simple libraries with a server component, or
|
540
|
+
even Sinatra extensions. The top-level DSL pollutes the Object namespace
|
541
|
+
and assumes a micro-app style configuration (e.g., a single application
|
542
|
+
file, ./public and ./views directories, logging, exception detail page,
|
543
|
+
etc.). That's where Sinatra::Base comes into play:
|
544
|
+
|
545
|
+
require 'sinatra/base'
|
546
|
+
|
547
|
+
class MyApp < Sinatra::Base
|
548
|
+
set :sessions, true
|
549
|
+
set :foo, 'bar'
|
550
|
+
|
551
|
+
get '/' do
|
552
|
+
'Hello world!'
|
553
|
+
end
|
554
|
+
end
|
555
|
+
|
556
|
+
The MyApp class is an independent Rack component that can act as
|
557
|
+
Rack middleware, a Rack application, or Rails metal. You can +use+ or
|
558
|
+
+run+ this class from a rackup +config.ru+ file; or, control a server
|
559
|
+
component shipped as a library:
|
560
|
+
|
561
|
+
MyApp.run! :host => 'localhost', :port => 9090
|
562
|
+
|
563
|
+
The methods available to Sinatra::Base subclasses are exactly as those
|
564
|
+
available via the top-level DSL. Most top-level apps can be converted to
|
565
|
+
Sinatra::Base components with two modifications:
|
566
|
+
|
567
|
+
* Your file should require +sinatra/base+ instead of +sinatra+;
|
568
|
+
otherwise, all of Sinatra's DSL methods are imported into the main
|
569
|
+
namespace.
|
570
|
+
* Put your app's routes, error handlers, filters, and options in a subclass
|
571
|
+
of Sinatra::Base.
|
572
|
+
|
573
|
+
+Sinatra::Base+ is a blank slate. Most options are disabled by default,
|
574
|
+
including the built-in server. See {Options and Configuration}[http://sinatra.github.com/configuration.html]
|
575
|
+
for details on available options and their behavior.
|
576
|
+
|
577
|
+
SIDEBAR: Sinatra's top-level DSL is implemented using a simple delegation
|
578
|
+
system. The +Sinatra::Application+ class -- a special subclass of
|
579
|
+
Sinatra::Base -- receives all :get, :put, :post, :delete, :before,
|
580
|
+
:error, :not_found, :configure, and :set messages sent to the
|
581
|
+
top-level. Have a look at the code for yourself: here's the
|
582
|
+
{Sinatra::Delegator mixin}[http://github.com/sinatra/sinatra/blob/ceac46f0bc129a6e994a06100aa854f606fe5992/lib/sinatra/base.rb#L1128]
|
583
|
+
being {included into the main namespace}[http://github.com/sinatra/sinatra/blob/ceac46f0bc129a6e994a06100aa854f606fe5992/lib/sinatra/main.rb#L28]
|
584
|
+
|
585
|
+
== Command line
|
586
|
+
|
587
|
+
Sinatra applications can be run directly:
|
588
|
+
|
589
|
+
ruby myapp.rb [-h] [-x] [-e ENVIRONMENT] [-p PORT] [-o HOST] [-s HANDLER]
|
590
|
+
|
591
|
+
Options are:
|
592
|
+
|
593
|
+
-h # help
|
594
|
+
-p # set the port (default is 4567)
|
595
|
+
-o # set the host (default is 0.0.0.0)
|
596
|
+
-e # set the environment (default is development)
|
597
|
+
-s # specify rack server/handler (default is thin)
|
598
|
+
-x # turn on the mutex lock (default is off)
|
599
|
+
|
600
|
+
== The Bleeding Edge
|
601
|
+
|
602
|
+
If you would like to use Sinatra's latest bleeding code, create a local
|
603
|
+
clone and run your app with the <tt>sinatra/lib</tt> directory on the
|
604
|
+
<tt>LOAD_PATH</tt>:
|
605
|
+
|
606
|
+
cd myapp
|
607
|
+
git clone git://github.com/sinatra/sinatra.git
|
608
|
+
ruby -Isinatra/lib myapp.rb
|
609
|
+
|
610
|
+
Alternatively, you can add the <tt>sinatra/lib</tt> directory to the
|
611
|
+
<tt>LOAD_PATH</tt> in your application:
|
612
|
+
|
613
|
+
$LOAD_PATH.unshift File.dirname(__FILE__) + '/sinatra/lib'
|
614
|
+
require 'rubygems'
|
615
|
+
require 'sinatra'
|
616
|
+
|
617
|
+
get '/about' do
|
618
|
+
"I'm running version " + Sinatra::VERSION
|
619
|
+
end
|
620
|
+
|
621
|
+
To update the Sinatra sources in the future:
|
622
|
+
|
623
|
+
cd myproject/sinatra
|
624
|
+
git pull
|
625
|
+
|
626
|
+
== More
|
627
|
+
|
628
|
+
* {Project Website}[http://www.sinatrarb.com/] - Additional documentation,
|
629
|
+
news, and links to other resources.
|
630
|
+
* {Contributing}[http://www.sinatrarb.com/contributing] - Find a bug? Need
|
631
|
+
help? Have a patch?
|
632
|
+
* {Lighthouse}[http://sinatra.lighthouseapp.com] - Issue tracking and release
|
633
|
+
planning.
|
634
|
+
* {Twitter}[http://twitter.com/sinatra]
|
635
|
+
* {Mailing List}[http://groups.google.com/group/sinatrarb/topics]
|
636
|
+
* {IRC: #sinatra}[irc://chat.freenode.net/#sinatra] on http://freenode.net
|