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’,
        ...
HOW DO YOU USE IT?
HOW DO YOU USE IT?
How to call it?
lambda { |env|
  [
      200,
      {
         ‘Content-Type’ => ‘text/plain’,
        ...
HOW DO YOU USE IT?
How to call it?
lambda { |env|
  [
      200,
      {
         ‘Content-Type’ => ‘text/plain’,
        ...
HOW DO YOU USE IT?
How to call it?
lambda { |env|
  [
     200,
     {
        ‘Content-Type’ => ‘text/plain’,
        ‘Co...
HOW DO YOU USE IT?
How to call it?
lambda { |env|
  [
      200,
      {
         ‘Content-Type’ => ‘text/plain’,
        ...
HOW DO YOU USE IT?
How to call it?
lambda { |env|
  [
    200,
        {
            ‘Content-Type’ => ‘text/plain’,
     ...
HOW DO YOU USE IT?
How to call it?
lambda { |env|
  [
      200,
      {
         ‘Content-Type’ => ‘text/plain’,
        ...
HOW DO YOU USE IT?
How to call it?
run lambda { |env|
  [
      200,
      {
         ‘Content-Type’ => ‘text/plain’,
    ...
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”,
    :verbos...
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?
Upcoming SlideShare
Loading in …5
×

Something something rack

1,991 views

Published on

My introduction talk to Rack at ArrrrCamp (http://arrrrcamp.be)

Published in: Technology
  • Be the first to comment

Something something rack

  1. 1. SOMETHING SOMETHING RACK
  2. 2. OH, HI! • Joren • Openminds • @joren • tech.jorendegroof.be
  3. 3. OVERVIEW • Rack what? • Why Rack? • How do you use it? • Middleware
  4. 4. RACK WHAT?
  5. 5. RACK WHAT? Rack is a specification (and implementation) of a minimal abstract Ruby API that models HTTP
  6. 6. WHY RACK? • Stop writing handlers for every webserver • Keep the request and response simple
  7. 7. WHY RACK? Supported web servers
  8. 8. WHY RACK? Supported web frameworks
  9. 9. HOW DO YOU USE IT? How to call it? lambda { |env| [ 200, { ‘Content-Type’ => ‘text/plain’, ‘Content-Lenght’ => ‘5’, }, [‘Hello’] ] }
  10. 10. HOW DO YOU USE IT?
  11. 11. HOW DO YOU USE IT? How to call it? lambda { |env| [ 200, { ‘Content-Type’ => ‘text/plain’, ‘Content-Lenght’ => ‘5’, }, [‘Hello’] ] }
  12. 12. HOW DO YOU USE IT? How to call it? lambda { |env| [ 200, { ‘Content-Type’ => ‘text/plain’, ‘Content-Lenght’ => ‘5’, }, [‘Hello’] ] }
  13. 13. HOW DO YOU USE IT? How to call it? lambda { |env| [ 200, { ‘Content-Type’ => ‘text/plain’, ‘Content-Lenght’ => ‘5’, }, [‘Hello’] ] }
  14. 14. HOW DO YOU USE IT? How to call it? lambda { |env| [ 200, { ‘Content-Type’ => ‘text/plain’, ‘Content-Lenght’ => ‘5’, }, [‘Hello’] ] }
  15. 15. HOW DO YOU USE IT? How to call it? lambda { |env| [ 200, { ‘Content-Type’ => ‘text/plain’, ‘Content-Lenght’ => ‘5’, }, [‘Hello’] ] }
  16. 16. HOW DO YOU USE IT? How to call it? lambda { |env| [ 200, { ‘Content-Type’ => ‘text/plain’, ‘Content-Lenght’ => ‘5’, }, [‘Hello’] ] }
  17. 17. HOW DO YOU USE IT? How to call it? run lambda { |env| [ 200, { ‘Content-Type’ => ‘text/plain’, ‘Content-Lenght’ => ‘5’, }, [‘Hello’] ] }
  18. 18. config.ru
  19. 19. $ rackup config.ru
  20. 20. $ curl http://localhost:9292
  21. 21. Hello
  22. 22. HOW DO YOU USE IT? class HelloWorld def call(env) [200, {"Content-Type" => "text/plain"}, ["Hello World!"]] end end
  23. 23. env
  24. 24. REQUEST_METHOD
  25. 25. env[‘REQUEST_METHOD’]
  26. 26. GET PUT POST DELETE HEAD OPTIONS TRACE
  27. 27. PATH_INFO
  28. 28. /items/123
  29. 29. HTTP_*
  30. 30. HTTP_ACCEPT
  31. 31. rack.*
  32. 32. yournamespace.*
  33. 33. request = Rack::Request.new(env)
  34. 34. request.post?
  35. 35. HOW DO YOU USE IT? Example
  36. 36. MIDDLEWARE Middleware App HTTP
  37. 37. use Middleware A use Middleware B use Middleware C run app
  38. 38. class GoSlower def initialize(app) @app = app end def call(env) sleep(1) @app.call(env) end end
  39. 39. class GoSlower def initialize(app) @app = app end def call(env) sleep(1) @app.call(env) end end
  40. 40. class GoSlower def initialize(app) @app = app end def call(env) sleep(1) @app.call(env) end end
  41. 41. class GoSlower def initialize(app) @app = app end def call(env) sleep(1) @app.call(env) end end
  42. 42. 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'
  43. 43. EXAMPLES
  44. 44. RACK::HONEYPOT http://github.com/sunlightlabs/rack-honeypot
  45. 45. RACK::CACHE http://github.com/rtomayko/rack-cache
  46. 46. WARDEN + DEVISE http://github.com/hassox/warden http://github.com/plataformatec/devise
  47. 47. RACK::DEBUG http://github.com/ddollar/rack-debug
  48. 48. RACK::GEOIP http://github.com/b/rack-geoip
  49. 49. RACK::GOOGLE-ANALYTICS http://github.com/leehambley/rack-google-analytics
  50. 50. RACK::REWRITE http://github.com/jtrupiano/rack-rewrite
  51. 51. RACK::MOBILE-DETECT http://github.com/talison/rack-mobile-detect
  52. 52. RACK::CONTRIB http://github.com/rack/rack-contrib
  53. 53. QUESTIONS?

×