Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Upcoming SlideShare
Testing
Testing
Loading in …3
×
1 of 53

Something something rack

2

Share

Download to read offline

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

Related Books

Free with a 30 day trial from Scribd

See all

Related Audiobooks

Free with a 30 day trial from Scribd

See all

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?

Editor's Notes





  • wrapping HTTP requests and responses in the simplest way possible


  • rack includes several handlers for the first web servers, the other distributions have included rack handlers on their own
  • 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
  • accepts basic ruby object
    like a lambda
  • accepts basic ruby object
    like a lambda
  • basic ruby object respond to call
    like a lambda
  • take 1 argument
    the environment
  • accepts basic ruby object
    like a lambda
  • accepts basic ruby object
    like a lambda
  • hash
    key value pairs for header
  • body
    must respond to each
    now just a string in an Array





  • don’t need to use lambda’s






  • http style header params
  • like http accept
  • rack namespaced protected keys in the environment

  • your own environment namespace
  • wrap it in a request object

  • normal
    builder
    toto
  • everytinh in front of our app




  • call down the next thing down stream
    this is the power of the middleware
    returning the result












  • ×