Rack Thomas R. Koll http://ananasblau.com
[email protected]
What is Rack? ●
Rack is written in Ruby
●
Rack is a webserver framework
●
Rack is between the webserver and your app
●
Rack can be a webapp, if it's a simple task
●
Apps can be anything: Rails, Sinatra, merb etc.
●
Rack is dead simple, even I understood it
Internetto
Webserver Rack
Rails Application
sinatra Application
Specification ●
Rack is an Object
●
Rack has one method: call
●
call accepts one parameter: environment
●
environment is an hash with request info
●
Call must return an array with: ●
status code (1xx, 2xx, 3xx, 4xx, 5xx)
●
response headers is a hash with e.g. 'Content-Type'
●
body is used for the actual output
Example 1: simple server
Or use Rack::Directory
Example 2: root_path per domain ●
Allow customers to point domains to your app
●
Allow them to set a different root_path. E.g.
●
●
●
/albums/32
●
/blog/why-this-website-is-no-more
Dispatch subdomains to different controllers ●
/static/jobs for jobs.example.com
●
/blog for blog.example.com
●
/dashboard for dashboard.example.com
Could be done by mod_rewrite
DomainDispatcher
Example 3: Rack::Cache ●
Usually separate app/server like squid
●
In mephisto done via rewrite rules in webserver: ●
Not very flexible
●
Not very portable friendly
●
Separated from your app
RewriteCond %{REQUEST_URI} ^/[^.]+$ RewriteCond %{DOCUMENT_ROOT}/cache/%{HTTP_HOST}% {REQUEST_FILENAME}.html -f RewriteRule ^/(.*)$ /cache/%{HTTP_HOST}%{REQUEST_FILENAME}.html [QSA,L] RewriteCond %{REQUEST_URI} ^/.+$ RewriteCond %{DOCUMENT_ROOT}/cache/%{HTTP_HOST}% {REQUEST_FILENAME} -f RewriteRule ^/(.*)$ /cache/%{HTTP_HOST}%{REQUEST_FILENAME} [QSA,L]
Existing middleware There's plenty of stuff other contributed: ●
Rack::Cache
●
Rack::ETag
●
Rack::MailExceptions
●
Rack::Backstage
●
Rack::Auth::OpenID and Basic
●
Rack::Static
●
and many more...
Weblinks ●
http://rack.rubyforge.org
●
http://github.com/rack/rack-contrib/
●
http://tomayko.com/src/rack-cache/
●
http://guides.rubyonrails.org/rails_on_rack
●
http://railscasts.com/episodes/151-rack-middleware