SOMETHING SOMETHING RACK
OH, HI!


• Joren

• Openminds

• @joren

• tech.jorendegroof.be
OVERVIEW


• Rack   what?

• Why    Rack?

• How    do you use it?

• Middleware
RACK WHAT?
RACK WHAT?


Rack is a specification (and implementation) of a minimal
           abstract Ruby API that models HTTP
WHY RACK?



• Stop   writing handlers for every webserver

• Keep   the request and response simple
WHY RACK?
Supported web servers
WHY RACK?
Supported web frameworks
HOW DO YOU USE IT?
How to call it?
lambda { |env|
  [
      200,
      {
         ‘Content-Type’ => ‘text/plain’,
         ‘Content-Lenght’ => ‘5’,
      },
      [‘Hello’]
    ]
}
HOW DO YOU USE IT?
HOW DO YOU USE IT?
How to call it?
lambda { |env|
  [
      200,
      {
         ‘Content-Type’ => ‘text/plain’,
         ‘Content-Lenght’ => ‘5’,
      },
      [‘Hello’]
    ]
}
HOW DO YOU USE IT?
How to call it?
lambda { |env|
  [
      200,
      {
         ‘Content-Type’ => ‘text/plain’,
         ‘Content-Lenght’ => ‘5’,
      },
      [‘Hello’]
    ]
}
HOW DO YOU USE IT?
How to call it?
lambda { |env|
  [
     200,
     {
        ‘Content-Type’ => ‘text/plain’,
        ‘Content-Lenght’ => ‘5’,
     },
     [‘Hello’]
   ]
}
HOW DO YOU USE IT?
How to call it?
lambda { |env|
  [
      200,
      {
         ‘Content-Type’ => ‘text/plain’,
         ‘Content-Lenght’ => ‘5’,
      },
      [‘Hello’]
    ]
}
HOW DO YOU USE IT?
How to call it?
lambda { |env|
  [
    200,
        {
            ‘Content-Type’ => ‘text/plain’,
            ‘Content-Lenght’ => ‘5’,
        },
        [‘Hello’]
    ]
}
HOW DO YOU USE IT?
How to call it?
lambda { |env|
  [
      200,
      {
         ‘Content-Type’ => ‘text/plain’,
         ‘Content-Lenght’ => ‘5’,
      },
      [‘Hello’]
    ]
}
HOW DO YOU USE IT?
How to call it?
run lambda { |env|
  [
      200,
      {
         ‘Content-Type’ => ‘text/plain’,
         ‘Content-Lenght’ => ‘5’,
      },
      [‘Hello’]
    ]
}
config.ru
$ rackup config.ru
$ curl http://localhost:9292
Hello
HOW DO YOU USE IT?

class HelloWorld
  def call(env)
   [200, {"Content-Type" => "text/plain"}, ["Hello World!"]]
  end
end
env
REQUEST_METHOD
env[‘REQUEST_METHOD’]
GET
PUT
POST
DELETE
HEAD
OPTIONS
TRACE
PATH_INFO
/items/123
HTTP_*
HTTP_ACCEPT
rack.*
yournamespace.*
request = Rack::Request.new(env)
request.post?
HOW DO YOU USE IT?


       Example
MIDDLEWARE
        Middleware   App



HTTP
use Middleware A
use Middleware B
use Middleware C
run app
class GoSlower
  def initialize(app)
   @app = app
  end

 def call(env)
  sleep(1)
  @app.call(env)
 end
end
class GoSlower
  def initialize(app)
   @app = app
  end

 def call(env)
  sleep(1)
  @app.call(env)
 end
end
class GoSlower
  def initialize(app)
   @app = app
  end

 def call(env)
  sleep(1)
  @app.call(env)
 end
end
class GoSlower
  def initialize(app)
   @app = app
  end

 def call(env)
  sleep(1)
  @app.call(env)
 end
end
IN YOUR RAILS APP

#environment.rb

config.middleware.use “Rack::GoSlower”

config.middleware.use “Rack::Cache”,
    :verbose => true,
    :metastrore => ‘file:/path/to/dir’,
    :entitystore => 'memcached://localhost:11211/body'
EXAMPLES
RACK::HONEYPOT
http://github.com/sunlightlabs/rack-honeypot
RACK::CACHE
http://github.com/rtomayko/rack-cache
WARDEN + DEVISE
  http://github.com/hassox/warden
http://github.com/plataformatec/devise
RACK::DEBUG
http://github.com/ddollar/rack-debug
RACK::GEOIP
http://github.com/b/rack-geoip
RACK::GOOGLE-ANALYTICS
http://github.com/leehambley/rack-google-analytics
RACK::REWRITE
http://github.com/jtrupiano/rack-rewrite
RACK::MOBILE-DETECT
http://github.com/talison/rack-mobile-detect
RACK::CONTRIB
http://github.com/rack/rack-contrib
QUESTIONS?

Something something rack

Editor's Notes

  • #6 wrapping HTTP requests and responses in the simplest way possible
  • #8 rack includes several handlers for the first web servers, the other distributions have included rack handlers on their own
  • #9 this means, you can run any of these frameworks on any of the previous web servers! without any changes Camping support is included in rack
  • #10 accepts basic ruby object like a lambda
  • #11 accepts basic ruby object like a lambda
  • #12 basic ruby object respond to call like a lambda
  • #13 take 1 argument the environment
  • #14 accepts basic ruby object like a lambda
  • #15 accepts basic ruby object like a lambda
  • #16 hash key value pairs for header
  • #17 body must respond to each now just a string in an Array
  • #23 don’t need to use lambda’s
  • #30 http style header params
  • #31 like http accept
  • #32 rack namespaced protected keys in the environment
  • #33 your own environment namespace
  • #34 wrap it in a request object
  • #36 normal builder toto
  • #37 everytinh in front of our app
  • #42 call down the next thing down stream this is the power of the middleware returning the result