Rack provides a minimal and modular interface for building web applications in Ruby. It handles HTTP requests and responses, acting as an interface between web servers and frameworks. Rails uses Rack middleware to handle requests, with Action Controller implemented as middleware. The routing table maps routes to controller actions. When a request comes in, it is passed through middleware before being routed and dispatched to the controller action to generate a response.
3. Things that nobody(or almost nobody) talk
about.
Keep the secret or DIE!!(just kidding S2)
4. ● Rails on Rack (What the RACK is going on?!)
● ‘Action Dispatch: Where it all begins’
5. ‘Rack aims to provide a minimal API for
connecting web server and web frameworks’
Rack provides a minimal, modular and adaptable interface for
developing web applications in Ruby. By wrapping HTTP requests
and responses in the simplest way possible, it unifies and
distills the API for web servers, web frameworks, and software
in between (the so-called middleware) into a single method call.
6. ● Example: rails server
RACK
(process
and build)
Web Server
and
App Server
(Webrick)
Browser
(Client side)
Web
Framework
8. ● Rails 2.3, request handling was moved to
Rack and the concept of middleware was
introduced.
● Classes that satisfy Rack's call interface can
be chained together as filters. (Rack
Middleware)
9. ● Much of Action Controller is implemented
as Rack middleware modules.
$ rake middleware
10.
11.
12. ● You can create your own middleware
● config/application.rb ou environment/*.rb
● config.middleware.use(new_middleware, args)
● config.middleware.insert_before(existing_middl
eware, new_middleware, args)
● Don't forget to initizalize and call app.call(env)
13. ● Rack is a framework to roll your own ruby framework.
● Rack provides an interface between different web servers and your framework/application.
Making it very simple for your framework/application to be compatible with any webserver
that supports Rack – Phusion Passenger, Litespeed, Mongrel, Thin, Ebb, Webrick to name a
few.
● Rack cuts your chase. You get request, response, cookies, params & sessions for free.
● Makes it possible to use multiple frameworks for the same application, provided there is no
class collision. Rails and sinatra integration is a good example of this.
● Middlewares ! Think of middlewares as Rails’s before_filter/after_filter that are reusable across
different rack supported frameworks/applications. For example, you can use the same Anti-
spamming rack middleware for your Rails app, Sinatra app and your custom Rack application
too!
14. ● The entry point to a request is an instance of
ActionDispatch::Routing::RouteSet
● Example:
route: get 'foo', to: 'foo#index'
instance: FooController.action(:index).call
15.
16.
17.
18. endpoint is our application's routes, an instance of ActionDispatch::Routing::RouteSet
21. ● Journey is the core routing module in
ActionDispatch.
● It is constructed by the code you define in
config/routes.rb
22.
23. ● When constructing the routing table, the
ActionDispatch::Routing::Mapper class calls
add_route, which will construct a
Journey::Route instance associated with a
controller endpoint (the Rack app) and add it
to the routing table.
24.
25. ● @set is the RouteSet
● app referenced here is an instance of
ActionDispatch::Routing::Dispatcher
28. ● Rack Creator:
http://chneukirchen.org/blog/archive/2007/02/introducing-rack.html
● Inside Rack and ActionDispatcher: http://pothibo.com/2013/11/ruby-on-
rails-inside-actiondispatch-and-rack/
● Understaing Rails from request to response in 3 parts(part I):
http://andrewberls.com/blog/post/rails-from-request-to-response-part-
1--introduction
● How to create a Ruby web server:
https://github.com/macournoyer/tube
● The Rails 4 Way, O. Fernandez, K. Faustino, June 2014
Notas do Editor
That app parameter during the initialization is actually the next middleware in the chain.