- HTTP Request/Response Cycle
- MVC
How does the application know how to map HTTP requests to Controller actions?
Rails routes are a middleware that map the HTTP Verb and Path (within a HTTP request) to a Controller action.
Wobbe::Application.routes.draw do
get '/profile', to: 'users#show' # UsersController.action(:show).call
end
A chain of classes that are initialized with a Rack Application, and respond to
#call
with [status_code, headers, body]
.
$ rake middleware
use Rails::Rack::Logger # responsible for the files in ./log/
use Rollbar::Middleware::Rails::RollbarMiddleware # responsible for reporting 400/500 status codes.
use ActionDispatch::Cookies
use ActionDispatch::Session::CookieStore
use ActionDispatch::Flash
...
run Wobbe::Application.routes # the HTTP Verbs and Paths that Wobbe can respond to
- Development/Debugging
- Logging/Error reporting
- Session management
- Routing
Two common types: When given a HTTP request, perform a side effect, or return some information (respond).
- ActiveRecord::Migration::CheckPending
- Rack::CommonLogger
- ActionDispatch::Flash
- Fun with Rack
- Rollbar::Middleware
class Middleware
def initialize(app)
@app = app
end
def call(env)
# before HTTP response
status, headers, body = @app.call(env) # call the next middleware application
# after building HTTP response
[status, headers, body]
end
end
Questions:
- What is stored in
env
? - Where is
@app.call(env)
located in the middleware? - What happens in the
#call
method? - What does the
#call
method return? - What could we do with this?