Building with Rack

Building with Rack

Let's build a Rack app!

4109276dec776c9999af7ba023d902b6?s=128

Alexander Clark

February 23, 2016
Tweet

Transcript

  1. Building with Rack Alexander Clark

  2. What is Rack?

  3. ?

  4. None
  5. ?

  6. None
  7. ?

  8. ?

  9. What does a Rack app look like? ?

  10. What does a Rack app look like? GET /foo HTTP/1.1

    Host: localhost:9292 Accept: text/html … ?
  11. What does a Rack app look like? GET /foo HTTP/1.1

    Host: localhost:9292 Accept: text/html … ? <!DOCTYPE html> <html> <head> …
  12. run Proc.new { |env| ['200', {'Content-Type' => 'text/html'}, ['get rack\'d']]

    }
  13. run Proc.new { |env| ['200', {'Content-Type' => 'text/html'}, ['get rack\'d']]

    } Responds to #call HTTP Status code Headers Response body Run
  14. run Proc.new { |env| ['200', {'Content-Type' => 'text/html'}, ['get rack\'d']]

    } Responds to #call Much HTTP Status code Such Headers Response body Wow Run
  15. Let’s build something!

  16. None
  17. <!DOCTYPE html> <html> <head> <title>Is it April Fools?</title> </head> <body>

    <h1><%= @april_fools ? 'YES' : 'NO' %></h1> </body> </html>
  18. require 'erb' class MyRackApp def initialize @april_fools = Time.now.strftime('%m%d') ==

    '0401' end def call(_env) ['200', {'Content-Type' => 'text/html'}, view] end def view [ERB.new(template).result(binding)] end def template File.open('index.html.erb', 'r').read end end
  19. require './my_rack_app' run MyRackApp.new

  20. None
  21. ?

  22. run Proc.new { |env| ['200', {'Content-Type' => 'text/html'}, ['get rack\'d']]

    } run MyRackApp.new ... def call(_env) ['200', {'Content-Type' => 'text/html'}, view] end
  23. run MyRackApp.new run Rails.application

  24. Middleware

  25. TL;DL

  26. Further Reading • https://rack.github.io/ • http://alexander-clark.com/blog/building-a-rack-app/ • http://www.justinweiss.com/articles/a-web-server-vs- an-app-server/ •

    https://www.amberbit.com/blog/2011/07/13/ introduction-to-rack-middleware/
  27. Thanks for Listening Alexander Clark atheclark alexander-clark.com alexander-clark