SlideShare a Scribd company logo
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 (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

What's hot

Firebase Auth Tutorial
Firebase Auth TutorialFirebase Auth Tutorial
Firebase Auth Tutorial
Bukhori Aqid
 
What is Application Performance Management?
What is Application Performance Management?What is Application Performance Management?
What is Application Performance Management?
CA Technologies
 
AWS Cloud Computing Tutorial | Migrating on Premise VM to AWS Cloud | AWS Tra...
AWS Cloud Computing Tutorial | Migrating on Premise VM to AWS Cloud | AWS Tra...AWS Cloud Computing Tutorial | Migrating on Premise VM to AWS Cloud | AWS Tra...
AWS Cloud Computing Tutorial | Migrating on Premise VM to AWS Cloud | AWS Tra...
Edureka!
 
Cross Platform Mobile Development
Cross Platform Mobile DevelopmentCross Platform Mobile Development
Cross Platform Mobile Development
Software Infrastructure
 
Amazon Redshift의 이해와 활용 (김용우) - AWS DB Day
Amazon Redshift의 이해와 활용 (김용우) - AWS DB DayAmazon Redshift의 이해와 활용 (김용우) - AWS DB Day
Amazon Redshift의 이해와 활용 (김용우) - AWS DB Day
Amazon Web Services Korea
 
Introduction to Firebase
Introduction to FirebaseIntroduction to Firebase
Introduction to Firebase
Mustafa Şenel
 
Introduction to AWS Database Services
Introduction to AWS Database ServicesIntroduction to AWS Database Services
Introduction to AWS Database Services
Amazon Web Services
 
Android intents
Android intentsAndroid intents
Android intents
Siva Ramakrishna kv
 
Rapid application developmet
Rapid application developmetRapid application developmet
Rapid application developmet
Rohana K Amarakoon
 
CHAPTER 6 REQUIREMENTS MODELING: SCENARIO based Model , Class based moddel
CHAPTER 6 REQUIREMENTS MODELING: SCENARIO based Model , Class based moddelCHAPTER 6 REQUIREMENTS MODELING: SCENARIO based Model , Class based moddel
CHAPTER 6 REQUIREMENTS MODELING: SCENARIO based Model , Class based moddel
mohamed khalaf alla mohamedain
 
Google App Engine ppt
Google App Engine  pptGoogle App Engine  ppt
Introducing AWS Elastic Beanstalk
Introducing AWS Elastic BeanstalkIntroducing AWS Elastic Beanstalk
Introducing AWS Elastic Beanstalk
Amazon Web Services
 
Customer case - Dynatrace Monitoring Redefined
Customer case - Dynatrace Monitoring RedefinedCustomer case - Dynatrace Monitoring Redefined
Customer case - Dynatrace Monitoring Redefined
Michel Duruel
 
Lessons Learned from Using Next.js in Production
Lessons Learned from Using Next.js in ProductionLessons Learned from Using Next.js in Production
Lessons Learned from Using Next.js in Production
Panjamapong Sermsawatsri
 
(DVO401) Deep Dive into Blue/Green Deployments on AWS
(DVO401) Deep Dive into Blue/Green Deployments on AWS(DVO401) Deep Dive into Blue/Green Deployments on AWS
(DVO401) Deep Dive into Blue/Green Deployments on AWS
Amazon Web Services
 
How To be a Backend developer
How To be a Backend developer    How To be a Backend developer
How To be a Backend developer
Ramy Hakam
 
pyspark.pdf
pyspark.pdfpyspark.pdf
pyspark.pdf
snowflakebatch
 
Cloud computing notes
Cloud computing notesCloud computing notes
Cloud computing notes
Srinivasa Rao
 
SE2023 0101 Software Development Process.pptx
SE2023 0101 Software Development Process.pptxSE2023 0101 Software Development Process.pptx
SE2023 0101 Software Development Process.pptx
Bharat Chawda
 
[DevSecOps Live] DevSecOps: Challenges and Opportunities
[DevSecOps Live] DevSecOps: Challenges and Opportunities[DevSecOps Live] DevSecOps: Challenges and Opportunities
[DevSecOps Live] DevSecOps: Challenges and Opportunities
Mohammed A. Imran
 

What's hot (20)

Firebase Auth Tutorial
Firebase Auth TutorialFirebase Auth Tutorial
Firebase Auth Tutorial
 
What is Application Performance Management?
What is Application Performance Management?What is Application Performance Management?
What is Application Performance Management?
 
AWS Cloud Computing Tutorial | Migrating on Premise VM to AWS Cloud | AWS Tra...
AWS Cloud Computing Tutorial | Migrating on Premise VM to AWS Cloud | AWS Tra...AWS Cloud Computing Tutorial | Migrating on Premise VM to AWS Cloud | AWS Tra...
AWS Cloud Computing Tutorial | Migrating on Premise VM to AWS Cloud | AWS Tra...
 
Cross Platform Mobile Development
Cross Platform Mobile DevelopmentCross Platform Mobile Development
Cross Platform Mobile Development
 
Amazon Redshift의 이해와 활용 (김용우) - AWS DB Day
Amazon Redshift의 이해와 활용 (김용우) - AWS DB DayAmazon Redshift의 이해와 활용 (김용우) - AWS DB Day
Amazon Redshift의 이해와 활용 (김용우) - AWS DB Day
 
Introduction to Firebase
Introduction to FirebaseIntroduction to Firebase
Introduction to Firebase
 
Introduction to AWS Database Services
Introduction to AWS Database ServicesIntroduction to AWS Database Services
Introduction to AWS Database Services
 
Android intents
Android intentsAndroid intents
Android intents
 
Rapid application developmet
Rapid application developmetRapid application developmet
Rapid application developmet
 
CHAPTER 6 REQUIREMENTS MODELING: SCENARIO based Model , Class based moddel
CHAPTER 6 REQUIREMENTS MODELING: SCENARIO based Model , Class based moddelCHAPTER 6 REQUIREMENTS MODELING: SCENARIO based Model , Class based moddel
CHAPTER 6 REQUIREMENTS MODELING: SCENARIO based Model , Class based moddel
 
Google App Engine ppt
Google App Engine  pptGoogle App Engine  ppt
Google App Engine ppt
 
Introducing AWS Elastic Beanstalk
Introducing AWS Elastic BeanstalkIntroducing AWS Elastic Beanstalk
Introducing AWS Elastic Beanstalk
 
Customer case - Dynatrace Monitoring Redefined
Customer case - Dynatrace Monitoring RedefinedCustomer case - Dynatrace Monitoring Redefined
Customer case - Dynatrace Monitoring Redefined
 
Lessons Learned from Using Next.js in Production
Lessons Learned from Using Next.js in ProductionLessons Learned from Using Next.js in Production
Lessons Learned from Using Next.js in Production
 
(DVO401) Deep Dive into Blue/Green Deployments on AWS
(DVO401) Deep Dive into Blue/Green Deployments on AWS(DVO401) Deep Dive into Blue/Green Deployments on AWS
(DVO401) Deep Dive into Blue/Green Deployments on AWS
 
How To be a Backend developer
How To be a Backend developer    How To be a Backend developer
How To be a Backend developer
 
pyspark.pdf
pyspark.pdfpyspark.pdf
pyspark.pdf
 
Cloud computing notes
Cloud computing notesCloud computing notes
Cloud computing notes
 
SE2023 0101 Software Development Process.pptx
SE2023 0101 Software Development Process.pptxSE2023 0101 Software Development Process.pptx
SE2023 0101 Software Development Process.pptx
 
[DevSecOps Live] DevSecOps: Challenges and Opportunities
[DevSecOps Live] DevSecOps: Challenges and Opportunities[DevSecOps Live] DevSecOps: Challenges and Opportunities
[DevSecOps Live] DevSecOps: Challenges and Opportunities
 

Similar to Rails request & middlewares

Rails Request & Middlewares
Rails Request & MiddlewaresRails Request & Middlewares
Rails Request & Middlewares
Santosh Wadghule
 
Rails in the bowels
Rails in the bowelsRails in the bowels
Rails in the bowels
Creditas
 
12 Introduction to Rails
12 Introduction to Rails12 Introduction to Rails
12 Introduction to Rails
Deepak Hagadur Bheemaraju
 
An Overview of the React Ecosystem
An Overview of the React EcosystemAn Overview of the React Ecosystem
An Overview of the React Ecosystem
FITC
 
Rails
RailsRails
Rails
SHC
 
Introduction to Rails by Evgeniy Hinyuk
Introduction to Rails by Evgeniy HinyukIntroduction to Rails by Evgeniy Hinyuk
Introduction to Rails by Evgeniy Hinyuk
Pivorak MeetUp
 
Lecture #5 Introduction to rails
Lecture #5 Introduction to railsLecture #5 Introduction to rails
Lecture #5 Introduction to rails
Evgeniy Hinyuk
 
Rack
RackRack
Rack
shen liu
 
Ruby On Rails Siddhesh
Ruby On Rails SiddheshRuby On Rails Siddhesh
Ruby On Rails Siddhesh
Siddhesh Bhobe
 
Aspose pdf
Aspose pdfAspose pdf
Aspose pdf
Jim Jones
 
TorqueBox
TorqueBoxTorqueBox
TorqueBox
bobmcwhirter
 
React on rails v4
React on rails v4React on rails v4
React on rails v4
Justin Gordon
 
Ruby On Rails Seminar Basis Softexpo Feb2010
Ruby On Rails Seminar Basis Softexpo Feb2010Ruby On Rails Seminar Basis Softexpo Feb2010
Ruby On Rails Seminar Basis Softexpo Feb2010
arif44
 
RoR guide_p1
RoR guide_p1RoR guide_p1
RoR guide_p1
Brady Cheng
 
React on rails v6.1 at LA Ruby, November 2016
React on rails v6.1 at LA Ruby, November 2016React on rails v6.1 at LA Ruby, November 2016
React on rails v6.1 at LA Ruby, November 2016
Justin Gordon
 
Introduction to Ruby on Rails
Introduction to Ruby on RailsIntroduction to Ruby on Rails
Introduction to Ruby on Rails
Alessandro DS
 
Introduction to Ruby on Rails
Introduction to Ruby on RailsIntroduction to Ruby on Rails
Introduction to Ruby on Rails
hasan2000
 
Ruby On Rails
Ruby On RailsRuby On Rails
Ruby On Rails
anides
 
Web Clients for Ruby and What they should be in the future
Web Clients for Ruby and What they should be in the futureWeb Clients for Ruby and What they should be in the future
Web Clients for Ruby and What they should be in the future
Toru Kawamura
 
How to Build a Big Data Application: Serverless Edition
How to Build a Big Data Application: Serverless EditionHow to Build a Big Data Application: Serverless Edition
How to Build a Big Data Application: Serverless Edition
ecobold
 

Similar to Rails request & middlewares (20)

Rails Request & Middlewares
Rails Request & MiddlewaresRails Request & Middlewares
Rails Request & Middlewares
 
Rails in the bowels
Rails in the bowelsRails in the bowels
Rails in the bowels
 
12 Introduction to Rails
12 Introduction to Rails12 Introduction to Rails
12 Introduction to Rails
 
An Overview of the React Ecosystem
An Overview of the React EcosystemAn Overview of the React Ecosystem
An Overview of the React Ecosystem
 
Rails
RailsRails
Rails
 
Introduction to Rails by Evgeniy Hinyuk
Introduction to Rails by Evgeniy HinyukIntroduction to Rails by Evgeniy Hinyuk
Introduction to Rails by Evgeniy Hinyuk
 
Lecture #5 Introduction to rails
Lecture #5 Introduction to railsLecture #5 Introduction to rails
Lecture #5 Introduction to rails
 
Rack
RackRack
Rack
 
Ruby On Rails Siddhesh
Ruby On Rails SiddheshRuby On Rails Siddhesh
Ruby On Rails Siddhesh
 
Aspose pdf
Aspose pdfAspose pdf
Aspose pdf
 
TorqueBox
TorqueBoxTorqueBox
TorqueBox
 
React on rails v4
React on rails v4React on rails v4
React on rails v4
 
Ruby On Rails Seminar Basis Softexpo Feb2010
Ruby On Rails Seminar Basis Softexpo Feb2010Ruby On Rails Seminar Basis Softexpo Feb2010
Ruby On Rails Seminar Basis Softexpo Feb2010
 
RoR guide_p1
RoR guide_p1RoR guide_p1
RoR guide_p1
 
React on rails v6.1 at LA Ruby, November 2016
React on rails v6.1 at LA Ruby, November 2016React on rails v6.1 at LA Ruby, November 2016
React on rails v6.1 at LA Ruby, November 2016
 
Introduction to Ruby on Rails
Introduction to Ruby on RailsIntroduction to Ruby on Rails
Introduction to Ruby on Rails
 
Introduction to Ruby on Rails
Introduction to Ruby on RailsIntroduction to Ruby on Rails
Introduction to Ruby on Rails
 
Ruby On Rails
Ruby On RailsRuby On Rails
Ruby On Rails
 
Web Clients for Ruby and What they should be in the future
Web Clients for Ruby and What they should be in the futureWeb Clients for Ruby and What they should be in the future
Web Clients for Ruby and What they should be in the future
 
How to Build a Big Data Application: Serverless Edition
How to Build a Big Data Application: Serverless EditionHow to Build a Big Data Application: Serverless Edition
How to Build a Big Data Application: Serverless Edition
 

Recently uploaded

GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
Neo4j
 
Best 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERPBest 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERP
Pixlogix Infotech
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
panagenda
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
Quotidiano Piemontese
 
GenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizationsGenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizations
kumardaparthi1024
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Paige Cruz
 
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with SlackLet's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
shyamraj55
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
Kari Kakkonen
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
danishmna97
 
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Speck&Tech
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
panagenda
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
Aftab Hussain
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
名前 です男
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
Alpen-Adria-Universität
 
Programming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup SlidesProgramming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup Slides
Zilliz
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
Neo4j
 
Infrastructure Challenges in Scaling RAG with Custom AI models
Infrastructure Challenges in Scaling RAG with Custom AI modelsInfrastructure Challenges in Scaling RAG with Custom AI models
Infrastructure Challenges in Scaling RAG with Custom AI models
Zilliz
 
Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
Zilliz
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
Matthew Sinclair
 
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
Neo4j
 

Recently uploaded (20)

GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
 
Best 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERPBest 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERP
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
 
GenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizationsGenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizations
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
 
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with SlackLet's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
 
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
 
Programming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup SlidesProgramming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup Slides
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
 
Infrastructure Challenges in Scaling RAG with Custom AI models
Infrastructure Challenges in Scaling RAG with Custom AI modelsInfrastructure Challenges in Scaling RAG with Custom AI models
Infrastructure Challenges in Scaling RAG with Custom AI models
 
Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
 
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
 

Rails request & middlewares

  • 3.
  • 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

  1. I’m a software developer.
  2. I’m a great fan of this watch, Casio f-91W.
  3. 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.
  4. 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.
  5. 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.
  6. It recognises Ruby application request and then it forwards its request to app server.
  7. 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.
  8. This is all about basics of Rails request life cycle. Now lets see how rails handles the request under the hood.
  9. 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.
  10. Before Rails 3, handling of session, parsing for parameters and etc were too difficult.
  11. Rake is “Make for Ruby” and it is task runner for Ruby.
  12. 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.
  13. Using low coupling, you can replace any component with another one without having to reimplement code.
  14. Here is a simple example of rack application.
  15. 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.
  16. 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.
  17. 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.
  18. Many of Action Dispatcher’s internal components are implemented as Rack middleware.
  19. You can inspect middleware stack of your rails application by using
  20. 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?
  21. 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.