Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Building with Rack

Building with Rack

Let's build a Rack app!

Alexander Clark

February 23, 2016
Tweet

More Decks by Alexander Clark

Other Decks in Programming

Transcript

  1. Building with Rack
    Alexander Clark

    View Slide

  2. What is Rack?

    View Slide

  3. ?

    View Slide

  4. View Slide

  5. ?

    View Slide

  6. View Slide

  7. ?

    View Slide

  8. ?

    View Slide

  9. What does a Rack app look
    like?
    ?

    View Slide

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

    ?

    View Slide

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

    ?




    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

  15. Let’s build something!

    View Slide

  16. View Slide




  17. Is it April Fools?


    <%= @april_fools ? 'YES' : 'NO' %>


    View Slide

  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

    View Slide

  19. require './my_rack_app'
    run MyRackApp.new

    View Slide

  20. View Slide

  21. ?

    View Slide

  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

    View Slide

  23. run MyRackApp.new
    run Rails.application

    View Slide

  24. Middleware

    View Slide

  25. TL;DL

    View Slide

  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/

    View Slide

  27. Thanks for Listening
    Alexander Clark
    atheclark
    alexander-clark.com
    alexander-clark

    View Slide