intro-rack-notes
intro-rack-notes copied to clipboard
Intro to Rack
Rack apps and middleware
What is Rack?
- Number 2 most downloaded gem on rubygems.org
- A spec for code that responds to HTTP:
- Object that responds to
call - Takes one argument (
env) - Returns
[status, headers, body]wherebodyresponds toeach
- Object that responds to
Why Rack?
- Universal adapter
- Original design
- Only useful for framework/webserver designers
Without Rack

With Rack

Other uses
- Good model for HTTP requests
- Middleware
Basic Rack app
- Class or proc based.
envis a big hash of stuff from the request. See app the outputs itsenvand list of keys in such an env
Middleware
- Sits between the user and the app
- Can intercept requests/responses and modify them
- Can be chained together

Example middleware (logs time):
- App
- Logger middleware
- Runner
Rack::Builder
- Shortcut to all the nesting
usefor Middleware (classes)runfor apps (instance)- run via the command-line with
rackup
Example middleware (logs time):
- App
- Logger middleware
- Rackup file
Rails on Rack
- Rails apps are rack apps (see
config.ru) - Rails has middleware (see
rake middleware) - Cookies, sessions, and params are handled by middleware
- Routes can point to any Rack app (or stack)
In the wild
Rack::Attack(rate limiting)Rack::Honeypot(spam trap)