Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Rails Request
&
Middlewares
Santosh Wadghule
@mechanicles
http://www.mechanicles.com/
I do love to take photos
https://www.flickr.com/photos/santoshwadghule/
CASIO F-91W
Coolest watch in the world
That’s it.
Lets talk about the Rails Request
What is a request?
Request is a…
• It is set of instructions that tells a server what
kind of response we want.
• It is a part of HTTP (reque...
Rails Request Life Cycle
Rails Request Life Cycle
Browser
Web Server
Routes
Controller
Model
View
Database
Rails App
Middlewares
App Server
App Server & Rails
App
Server Rails
Rack
Web Server
What is a Web Server?
• Web Server is strictly HTTP based, it just takes
HTTP requests and sends back HTTP responses
to th...
App Server
What is an App Server?
• App Server actually runs your Rails app.
• App Server is mostly known for to serve dynamic
pages....
Request to Rails app
• Rails isn’t just one application, it has lots of
independent Rack applications (middlewares).
• Whe...
Request to Rails app
• Based on request, Rails decides which controller
& action need to be executed from the routes file....
Middlewares
What is a Middleware?
• Middleware is a Rack application.
• Middleware is basically a filter for request and
response.
Rails Middlewares
App
Server
Rails App
A B C D
Routes
Controller
Models
Views
Here A, B, C & D are middlewares
Each of the...
Why Rails uses Middlewares?
• Before rails 3, Rails was very tightly coupled.
• As Rails was growing, apps built on Rails ...
Rack - The Webserver Interface
What is a Rack?
• Rack is not Rake.
• Rack is simple but powerful spec. Yes it is just
spec.
• It is not web framework or ...
Rack - Just an Interface
• It is an interface that sits between your web/app
server and your application. It wraps HTTP
re...
Rack Specification
• Specification: A Rack application is a Ruby object
(not a class) that responds to `call`. It takes
ex...
Why do we need Rack?
• Rack promotes low coupling.
• If there was’t Rack, then each framework had to
add separate handler ...
Let’s see examples of Rack
Simple Rack App
Rack it up
Using Middleware in a Rack app
• Using ‘use’ method of Rack, you can use
middleware component in Rack application.
• You c...
Directly using Middleware
in config.ru
Rack app + Middleware + config.ru
Using Middleware in
Rack::Builder
What is Rack::Builder?
• Rack::Builder implements a small DSL to
iteratively construct Rack applications.
• Rack::Builder ...
Rack app + Middleware + Rack::Builder
Rails on Rack
Rails on Rack
• Rails application is the primary Rack
application object.
• It has ‘call’ method similar to rack’s ‘call’
...
Rails’ Rack::Builder
• Same like Rack::Builder, we have similar
concept in Rails, it is called as
ActionDispatch::Middlewa...
Rails’ Rack::Builder
• Rails::Application uses
ActionDispatch::MiddlewareStack to
combine various internal and external
mi...
Inspecting Rails Middlewares
• Rails provides a task for inspecting the
middleware stack in use.
• $ bin/rake middleware
$ bin/rake middleware
Note: List of the middlewares may be different for your Rails app
Using Middleware in Rails
• Rails provides a simple configuration interface
config.middleware for adding, removing and
mod...
Add this file under config/initializers directory
And under config/application.rb file, add this following line
config.mid...
Always Remember That…
• When request comes to your Rails app, it goes
through these middlewares. From top to bottom.
• At ...
Thanks :)
Santosh Wadghule
@mechanicles
Questions?
Rails request & middlewares
Upcoming SlideShare
Loading in …5
×

Rails request & middlewares

774 views

Published on

This slides will help you to understand request flow in Rails app and will give you an idea of middlewares, how it helps to process request in Rails.

Published in: Technology
  • Be the first to comment

Rails request & middlewares

  1. 1. Rails Request & Middlewares
  2. 2. Santosh Wadghule @mechanicles http://www.mechanicles.com/
  3. 3. I do love to take photos https://www.flickr.com/photos/santoshwadghule/
  4. 4. CASIO F-91W Coolest watch in the world
  5. 5. That’s it. Lets talk about the Rails Request
  6. 6. What is a request?
  7. 7. Request is a… • It is set of instructions that tells a server what kind of response we want. • It is a part of HTTP (request/response) protocol. • HTTP uses one of the verbs like GET, POST, PUT & DELETE when you perform the request to the server.
  8. 8. Rails Request Life Cycle
  9. 9. Rails Request Life Cycle Browser Web Server Routes Controller Model View Database Rails App Middlewares App Server
  10. 10. App Server & Rails App Server Rails Rack
  11. 11. Web Server
  12. 12. What is a Web Server? • Web Server is strictly HTTP based, it just takes HTTP requests and sends back HTTP responses to the clients(browsers). • It is mostly designed to serve static files. • It also has other functionality like request pipeline, load balancing etc. App servers lack these functionalities. • E.g. Ngnix, Apache
  13. 13. App Server
  14. 14. What is an App Server? • App Server actually runs your Rails app. • App Server is mostly known for to serve dynamic pages. • E.g. Webrick, Passenger, Mongrel, Unicorn, Thin, Puma & etc.
  15. 15. Request to Rails app • Rails isn’t just one application, it has lots of independent Rack applications (middlewares). • When request comes to the Rails app, it goes through the list of middleware series. • Last part of that series, sends request to the routes file.
  16. 16. Request to Rails app • Based on request, Rails decides which controller & action need to be executed from the routes file. • After executing the controller’s action, Rails sends back response to the the client. • Web Server & App Server actually handle the job of sending response back to the proper client.
  17. 17. Middlewares
  18. 18. What is a Middleware? • Middleware is a Rack application. • Middleware is basically a filter for request and response.
  19. 19. Rails Middlewares App Server Rails App A B C D Routes Controller Models Views Here A, B, C & D are middlewares Each of these does processing on request & response
  20. 20. Why Rails uses Middlewares? • Before rails 3, Rails was very tightly coupled. • As Rails was growing, apps built on Rails had more demanding requirements. For some apps, Rails gave lots of additional stuffs by default, which was not required like cookies/flash. For some other apps, to implement new filter on the the request/response was not possible. • In Rails 3 and after, all these issues have got solved by using a concept of Rack.
  21. 21. Rack - The Webserver Interface
  22. 22. What is a Rack? • Rack is not Rake. • Rack is simple but powerful spec. Yes it is just spec. • It is not web framework or web server. • Rack is created by Christian Neukirchen
  23. 23. Rack - Just an Interface • It is an interface that sits between your web/app server and your application. It wraps HTTP requests and responses in the simplest way possible, it unifies and distills the API for web servers, web frameworks and software in between (i.e. middleware) into a single method call.
  24. 24. Rack Specification • Specification: A Rack application is a Ruby object (not a class) that responds to `call`. It takes exactly one argument, the environment and returns an Array of three values, The status, the headers, and the body. That’s it.
  25. 25. Why do we need Rack? • Rack promotes low coupling. • If there was’t Rack, then each framework had to add separate handler for Thin, Mongrel, Unicorn and etc to run the app on these servers. • In reality it’s not a job of framework. They should not need to take care of it.
  26. 26. Let’s see examples of Rack
  27. 27. Simple Rack App
  28. 28. Rack it up
  29. 29. Using Middleware in a Rack app • Using ‘use’ method of Rack, you can use middleware component in Rack application. • You can use multiple middleware components which help to change request/response as you need. • You can add middlewares by using two methods. By directly adding middleware in config.ru and using Rack::Builder.
  30. 30. Directly using Middleware in config.ru
  31. 31. Rack app + Middleware + config.ru
  32. 32. Using Middleware in Rack::Builder
  33. 33. What is Rack::Builder? • Rack::Builder implements a small DSL to iteratively construct Rack applications. • Rack::Builder is the thing that glues Rack middlewares and application together and converts them into single entity/rack application. • Under the hood, ‘rackup’ command converts your config.ru script to an instance of Rack::Builder.
  34. 34. Rack app + Middleware + Rack::Builder
  35. 35. Rails on Rack
  36. 36. Rails on Rack • Rails application is the primary Rack application object. • It has ‘call’ method similar to rack’s ‘call’ method in its source code (i.e. in Rails::Application).
  37. 37. Rails’ Rack::Builder • Same like Rack::Builder, we have similar concept in Rails, it is called as ActionDispatch::MiddlewareStack. • Better flexibility and more features to meet Rails’ requirement.
  38. 38. Rails’ Rack::Builder • Rails::Application uses ActionDispatch::MiddlewareStack to combine various internal and external middlewares to form a complete Rails Rack application.
  39. 39. Inspecting Rails Middlewares • Rails provides a task for inspecting the middleware stack in use. • $ bin/rake middleware
  40. 40. $ bin/rake middleware Note: List of the middlewares may be different for your Rails app
  41. 41. Using Middleware in Rails • Rails provides a simple configuration interface config.middleware for adding, removing and modifying the middlewares in the middleware stack via application.rb or the environment specific configuration file environments/<environment>.rb. • Using these methods config.middleware.use, config.middleware.insert_before, config.middleware.insert_after, you can add new middleware to the middleware stack. • It also provides ‘swap’ and ‘delete’ methods for swapping and deleting a middleware.
  42. 42. Add this file under config/initializers directory And under config/application.rb file, add this following line config.middleware.use "SayHelloMiddlware"
  43. 43. Always Remember That… • When request comes to your Rails app, it goes through these middlewares. From top to bottom. • At the bottom, request enters into the your Rails’ MVC area.
  44. 44. Thanks :) Santosh Wadghule @mechanicles Questions?

×