/intro-rack-notes

Intro to Rack

Primary LanguageRuby

Rack apps and middleware

What is Rack?

  • Number 2 most downloaded gem on rubygems.org
  • A spec for code that responds to HTTP:
    1. Object that responds to call
    2. Takes one argument (env)
    3. Returns [status, headers, body] where body responds to each

Why Rack?

  • Universal adapter
    • Original design
    • Only useful for framework/webserver designers

Without Rack

without rack

With Rack

with rack

Other uses

  • Good model for HTTP requests
  • Middleware

Basic Rack app

Middleware

  • Sits between the user and the app
  • Can intercept requests/responses and modify them
  • Can be chained together

middleware stack

Example middleware (logs time):

Rack::Builder

  • Shortcut to all the nesting
  • use for Middleware (classes)
  • run for apps (instance)
  • run via the command-line with rackup

Example middleware (logs time):

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