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

Rack - A interface por trás dos frameworks web ...

Rack - A interface por trás dos frameworks web em Ruby

Avatar for Philippe Hardardt

Philippe Hardardt

November 17, 2017
Tweet

More Decks by Philippe Hardardt

Other Decks in Programming

Transcript

  1. class MyApp def call(env) status = 200 # ok headers

    = {'Content-Type' => 'text/html'} body = ['<p>Hello Rubyconf!</p>'] [status, headers, body] end end
  2. class MyApp def call(env) status = 200 # ok headers

    = {'Content-Type' => 'text/html'} body = ['<p>Hello Rubyconf!</p>’] [status, headers, body] end end { "HTTP_ACCEPT" => “text/html, ...", "HTTP_USER_AGENT" => "Mozilla/5.0...", "REQUEST_PATH" => "/posts", "REQUEST_METHOD" => "GET", "rack.version" => [1, 3], "rack.request.query_string" => "", "rack.request.query_hash" => {}, "rack.request.cookie_hash" => {} ... }
  3. class MyApp def call(env) status = 200 # ok headers

    = {'Content-Type' => 'text/html'} body = ['<p>Hello Rubyconf!</p>'] [status, headers, body] end end
  4. M iddlew are 1 M iddlew are 2 M iddlew

    are 3 M iddlew are 4 REQUISIÇÃO RESPOSTA
  5. Com m onLogger M iddlew are 2 M iddlew are

    3 M iddlew are 4 127.0.0.1 - - [07/Nov/2017:00:46:36 -0200] "GET / HTTP/1.1" 200 - 0.0003 127.0.0.1 - - [07/Nov/2017:00:46:41 -0200] "GET / HTTP/1.1" 200 - 0.0003 127.0.0.1 - - [07/Nov/2017:00:46:41 -0200] "GET / HTTP/1.1" 200 - 0.0003 127.0.0.1 - - [07/Nov/2017:00:46:45 -0200] "GET / HTTP/1.1" 200 - 0.0003 127.0.0.1 - - [07/Nov/2017:00:46:55 -0200] "GET /products/2 HTTP/1.1" 200 - 0.0003 127.0.0.1 - - [07/Nov/2017:00:47:02 -0200] "GET /products/2 HTTP/1.1" 200 - 0.0003 127.0.0.1 - - [07/Nov/2017:00:47:04 -0200] "GET /products HTTP/1.1" 200 - 0.0005 127.0.0.1 - - [07/Nov/2017:00:47:05 -0200] "GET /products/1 HTTP/1.1" 200 - 0.0003 127.0.0.1 - - [07/Nov/2017:00:47:07 -0200] "GET /products/3 HTTP/1.1" 200 - 0.0003 127.0.0.1 - - [07/Nov/2017:00:47:09 -0200] "GET /products/2 HTTP/1.1" 200 - 0.0005
  6. M iddlew are 3 M iddlew are 4 a Com

    m onLogger Autenticação 401 UNAUTHORIZED ⛔
  7. class MyMiddleware def initialize(app) @app = app end def call(env)

    # before logic @app.call(env) # after* logic end end
  8. class MyMiddleware def initialize(app) @app = app end def call(env)

    # before logic @app.call(env) # after* logic end end
  9. class MyMiddleware def initialize(app) @app = app end def call(env)

    # before logic @app.call(env) # after* logic end end
  10. class MyMiddleware def initialize(app) @app = app end def call(env)

    # before logic @app.call(env) # after* logic end end
  11. class MyMiddleware def initialize(app) @app = app end def call(env)

    # before logic @app.call(env) # after* logic end end
  12. • Conexão com o banco de dados • Autenticação •

    Telas de erro (exceção) • Rate limit • Controle de cookies • Roteamento • Recarregamento automático de código • Profiling • Arquivos estáticos
  13. use Rack::Sendfile use ActionDispatch::Static use ActionDispatch::Executor use Rack::Runtime use Rack::MethodOverride

    use ActionDispatch::RequestId use ActionDispatch::RemoteIp use Sprockets::Rails::QuietAssets use Rails::Rack::Logger use ActionDispatch::ShowExceptions use WebConsole::Middleware use ActionDispatch::DebugExceptions use ActionDispatch::Reloader use ActionDispatch::Callbacks use ActiveRecord::Migration::CheckPending use ActionDispatch::Cookies ...
  14. class ActiveRecord::Migration::CheckPending def initialize(app) @app = app end def call(env)

    # raises error if there are pending migrations ActiveRecord::Migration.check_pending!(connection) @app.call(env) end def connection ActiveRecord::Base.connection end end
  15. class ActiveRecord::Migration::CheckPending def initialize(app) @app = app end def call(env)

    # raises error if there are pending migrations ActiveRecord::Migration.check_pending!(connection) @app.call(env) end def connection ActiveRecord::Base.connection end end
  16. class MyApp def call(env) status = 200 # ok headers

    = {'Content-Type' => 'text/html'} body_html = [ 'My e-commerce!’, lazy_view ] [status, headers, body_html] end end
  17. class MyApp def call(env) status = 200 # ok headers

    = {'Content-Type' => 'text/html'} body_html = [ 'My e-commerce!', lazy_view ] [status, headers, body_html] end end
  18. class MyApp def call(env) status = 200 # ok headers

    = {'Content-Type' => 'text/html'} body_html = [ 'My e-commerce!', lazy_view ] [status, headers, body_html] end end
  19. class MyApp def call(env) status = 200 # ok headers

    = {'Content-Type' => 'text/html'} body_html = [ 'My e-commerce!', lazy_view ] [status, headers, body_html] end end
  20. class MyApp def call(env) status = 200 # ok headers

    = {'Content-Type' => 'text/html'} body = ['<p>Hello Rubyconf!</p>’] [status, headers, body] end end 1 requisição 1 resposta