Rails request & middlewares

45
Rails Request & Middlewares

Transcript of Rails request & middlewares

Rails Request&

Middlewares

Santosh Wadghule @mechanicles

http://www.mechanicles.com/

I do love to take photos

https://www.flickr.com/photos/santoshwadghule/

CASIO F-91WCoolest 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

ControllerModel

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 middlewaresEach 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 lineconfig.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?