SlideShare a Scribd company logo
Rails Request
&
Middlewares
Santosh Wadghule
@mechanicles
http://www.mechanicles.com/
Rails request & middlewares
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 (request/response) protocol.
• HTTP uses one of the verbs like GET, POST, PUT
& DELETE when you perform the request to the
server.
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 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
App Server
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.
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.
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.
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 these does processing on request & response
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.
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 web server.
• Rack is created by Christian Neukirchen
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.
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.
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.
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 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.
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 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.
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’
method in its source code (i.e. in
Rails::Application).
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.
Rails’ Rack::Builder
• Rails::Application uses
ActionDispatch::MiddlewareStack to
combine various internal and external
middlewares to form a complete Rails Rack
application.
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
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.
Add this file under config/initializers directory
And under config/application.rb file, add this following line
config.middleware.use "SayHelloMiddlware"
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.
Thanks :)
Santosh Wadghule
@mechanicles
Questions?

More Related Content

PDF
Uml diagrama clase objeto
Facultad de Ciencias y Sistemas
 
PPTX
Fundamentos de BD - Unidad 5 algebra relacional
José Antonio Sandoval Acosta
 
PDF
React Native
Craig Jolicoeur
 
PDF
React Training.pdf
kishorebrolly123
 
PPTX
Express JS
Designveloper
 
PPTX
Google Firebase
AliZaidi94
 
PPTX
Welcome to Blazor
dark_wisdom
 
PDF
Documentación
FSILSCA
 
Uml diagrama clase objeto
Facultad de Ciencias y Sistemas
 
Fundamentos de BD - Unidad 5 algebra relacional
José Antonio Sandoval Acosta
 
React Native
Craig Jolicoeur
 
React Training.pdf
kishorebrolly123
 
Express JS
Designveloper
 
Google Firebase
AliZaidi94
 
Welcome to Blazor
dark_wisdom
 
Documentación
FSILSCA
 

Similar to Rails request & middlewares (20)

PPTX
Rails Request & Middlewares
Santosh Wadghule
 
PPTX
Rails in the bowels
Creditas
 
PPTX
12 Introduction to Rails
Deepak Hagadur Bheemaraju
 
PDF
An Overview of the React Ecosystem
FITC
 
PPT
Rails
SHC
 
PDF
Introduction to Rails by Evgeniy Hinyuk
Pivorak MeetUp
 
PDF
Lecture #5 Introduction to rails
Evgeniy Hinyuk
 
PDF
Rack
shen liu
 
PPT
Ruby On Rails Siddhesh
Siddhesh Bhobe
 
PDF
Aspose pdf
Jim Jones
 
PDF
TorqueBox
bobmcwhirter
 
PDF
React on rails v4
Justin Gordon
 
PPT
Ruby On Rails Seminar Basis Softexpo Feb2010
arif44
 
PPTX
RoR guide_p1
Brady Cheng
 
PDF
React on rails v6.1 at LA Ruby, November 2016
Justin Gordon
 
PDF
Introduction to Ruby on Rails
Alessandro DS
 
ODP
Introduction to Ruby on Rails
hasan2000
 
PDF
Ruby On Rails
anides
 
PDF
Web Clients for Ruby and What they should be in the future
Toru Kawamura
 
PDF
How to Build a Big Data Application: Serverless Edition
ecobold
 
Rails Request & Middlewares
Santosh Wadghule
 
Rails in the bowels
Creditas
 
12 Introduction to Rails
Deepak Hagadur Bheemaraju
 
An Overview of the React Ecosystem
FITC
 
Rails
SHC
 
Introduction to Rails by Evgeniy Hinyuk
Pivorak MeetUp
 
Lecture #5 Introduction to rails
Evgeniy Hinyuk
 
Rack
shen liu
 
Ruby On Rails Siddhesh
Siddhesh Bhobe
 
Aspose pdf
Jim Jones
 
TorqueBox
bobmcwhirter
 
React on rails v4
Justin Gordon
 
Ruby On Rails Seminar Basis Softexpo Feb2010
arif44
 
RoR guide_p1
Brady Cheng
 
React on rails v6.1 at LA Ruby, November 2016
Justin Gordon
 
Introduction to Ruby on Rails
Alessandro DS
 
Introduction to Ruby on Rails
hasan2000
 
Ruby On Rails
anides
 
Web Clients for Ruby and What they should be in the future
Toru Kawamura
 
How to Build a Big Data Application: Serverless Edition
ecobold
 
Ad

Recently uploaded (20)

PDF
Doc9.....................................
SofiaCollazos
 
PDF
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
PDF
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
PDF
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
PDF
Data_Analytics_vs_Data_Science_vs_BI_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 
PDF
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
PPTX
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PDF
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 
PDF
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
PPTX
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
PPTX
Dev Dives: Automate, test, and deploy in one place—with Unified Developer Exp...
AndreeaTom
 
PPTX
IT Runs Better with ThousandEyes AI-driven Assurance
ThousandEyes
 
PPTX
Agile Chennai 18-19 July 2025 | Emerging patterns in Agentic AI by Bharani Su...
AgileNetwork
 
PDF
The Future of Artificial Intelligence (AI)
Mukul
 
PDF
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
 
PPTX
AI in Daily Life: How Artificial Intelligence Helps Us Every Day
vanshrpatil7
 
PDF
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
PDF
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
PPTX
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
Doc9.....................................
SofiaCollazos
 
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
Data_Analytics_vs_Data_Science_vs_BI_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
Dev Dives: Automate, test, and deploy in one place—with Unified Developer Exp...
AndreeaTom
 
IT Runs Better with ThousandEyes AI-driven Assurance
ThousandEyes
 
Agile Chennai 18-19 July 2025 | Emerging patterns in Agentic AI by Bharani Su...
AgileNetwork
 
The Future of Artificial Intelligence (AI)
Mukul
 
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
 
AI in Daily Life: How Artificial Intelligence Helps Us Every Day
vanshrpatil7
 
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
Ad

Rails request & middlewares

  • 4. I do love to take photos https://www.flickr.com/photos/santoshwadghule/
  • 6. That’s it. Lets talk about the Rails Request
  • 7. What is a request?
  • 8. 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.
  • 10. Rails Request Life Cycle Browser Web Server Routes Controller Model View Database Rails App Middlewares App Server
  • 11. App Server & Rails App Server Rails Rack
  • 13. 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
  • 15. 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.
  • 16. 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.
  • 17. 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.
  • 19. What is a Middleware? • Middleware is a Rack application. • Middleware is basically a filter for request and response.
  • 20. 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
  • 21. 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.
  • 22. Rack - The Webserver Interface
  • 23. 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
  • 24. 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.
  • 25. 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.
  • 26. 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.
  • 30. 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.
  • 32. Rack app + Middleware + config.ru
  • 34. 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.
  • 35. Rack app + Middleware + Rack::Builder
  • 37. 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).
  • 38. 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.
  • 39. Rails’ Rack::Builder • Rails::Application uses ActionDispatch::MiddlewareStack to combine various internal and external middlewares to form a complete Rails Rack application.
  • 40. Inspecting Rails Middlewares • Rails provides a task for inspecting the middleware stack in use. • $ bin/rake middleware
  • 41. $ bin/rake middleware Note: List of the middlewares may be different for your Rails app
  • 42. 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.
  • 43. Add this file under config/initializers directory And under config/application.rb file, add this following line config.middleware.use "SayHelloMiddlware"
  • 44. 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.

Editor's Notes

  • #3: I’m a software developer.
  • #6: I’m a great fan of this watch, Casio f-91W.
  • #9: In simple way, when we hit any url in the browser for some file, that browser converts that url into the request message and sends to the server and that request message is your actual request :) When you want to get some data from the server, you use GET method. When we want to create new data on the server, we use POST method. When we want to update data like username we use PUT method. For destroying the data we use DELETE verb. There are also other methods like, HEAD, PATCH, OPTIONS, CONNECT & ETC which HTTP protocol provides.
  • #11: This is a basic and standard diagram of Rails Request Life Cycle. You can see here, When browser initiates the requests it goes to Web Server, From the Web Server It goes through the App server, From App Server, request enters into the Rails app. Now if you see in the Rails app, request goes through the series of middlewares, From there request goes to the routes file and Based on request, Rails decides which controller & action need to be executed from the routes file. Now in controller if there is any business logic, it goes to model and from there it will fetch data from database or store the data in the database. If there any view like html, then request goes to view. After processing it, controller sends back the response to Browser.
  • #12: In this diagram, I’m showing how app server and rails app communicate with each other. Communications between App Server and Rails app happen using Rack. Rack is a gem and acts as an interface between them. We will see more on rack in next slides.
  • #14: It recognises Ruby application request and then it forwards its request to app server.
  • #16: Web Server forwards its request to the App Server, and App Server in turn forwards that request to the Rails app. In development mode, App Server can play role of web server too. In production it does not scale too much so we need web server in between.
  • #18: This is all about basics of Rails request life cycle. Now lets see how rails handles the request under the hood.
  • #20: You can think of middlewares isolate the different stages of processing on the request and response. One of the best things of Rack, you customise request/response very easily, by chaining middleware components in between the app server and the rails app.
  • #22: Before Rails 3, handling of session, parsing for parameters and etc were too difficult.
  • #24: Rake is “Make for Ruby” and it is task runner for Ruby.
  • #26: Argument environment is nothing but environmental variables hash like PATH_INFO, QURY_STRING, REMOTE_ADDR, REQUEST_URI and all that junk. You can say this environment variable has all information of the request.
  • #27: Using low coupling, you can replace any component with another one without having to reimplement code.
  • #29: Here is a simple example of rack application.
  • #30: This is another way to write rack application using rackup command which is given by Rack. The only thing you need is, to write your code in config.ru and run it using rackup config.ru.
  • #33: In this code, I have defined rack application using MyRackApp and I have defined one middleware using MyMiddleware class. When you want to apply middleware to your rack application you have use ‘use’ method of Rack. When request comes, it comes from top to bottom. Means from the middleware to the actual rack application. Always remember that, middleware’s initialize method always takes an argument i.e. app that app is nothing but your next middleware or next rack application.
  • #35: Think of Rack::Builder object as stack in which your actual rack application is at bottom and all middlewares on top of it. The whole stack you can call it as rack application too.
  • #39: Many of Action Dispatcher’s internal components are implemented as Rack middleware.
  • #41: You can inspect middleware stack of your rails application by using
  • #42: This list is quite big, So I’m not going to explain what each of these middleware does here. But there is one middleware, I think most of you have seen it, that is CheckPending. Anyone knows what it does?
  • #44: Lets write one middleware in the rails app. I mostly prefer to write middleware in config/initializers folder. In this middleware, mostly in call method, if request_path is ‘say_hello’, then this middleware will send this response back to the client. So here, our rails app will get called. If request_path ‘say_hello’ then our rails app gets called. To add this middleware in rails’s middleware stack, we need to set middleware to config.middleware.use or….config.middleware.insert_after But when we use this config.middleware.use then that middleware will get applied at the bottom of rails middleware series.