Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between use and run in Rack

Tags:

ruby

rack

What's the difference between the use and run methods in rackup files? It seems run is always at the end of config.ru, but it seems as if you should just be able to use use. Enlightening resources would also be very much appreciated.

like image 936
Cenoc Avatar asked Dec 22 '13 04:12

Cenoc


People also ask

How does a rack work?

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.

What are rack applications?

Rack is a modular interface between web servers and web applications developed in the Ruby programming language. With Rack, application programming interfaces (APIs) for web frameworks and middleware are wrapped into a single method call handling HTTP requests and responses.

What are Middlewares in rails?

You can refer rack middleware as a small component that assists with the execution of a task. For example, You can think of different middleware doing different processes like logging, caching. It can modify or halt requests before they reach the final middleware(The object which we assigned to run method).

What is a rack handler?

The Rack Handler is an attachable handling system ergonomically designed for manually moving racks over long distances and in tight areas. It allows operators to quickly attach handles onto any rack within seconds.


1 Answers

use is for middlewares

class MyCustomMiddleware
  def initialize(app)
    @app = app
  end

  def call(env)
    if condition
     env['set-header'] = 'Middleware Can modify the response & pass it into next middleware'
    end
    @app.call(env)
end

run takes an argument that responds to call and returns a final Rack response with a HTTP Response code like 200.

class MyApp
  def self.call(env)
   [200, { "Content-Type" => "text/html" }, ["OK"]]
 end
end

To understand the difference between use & run. lets see structure of typically rack app.

Typical Rack App Rack app includes multiple middleware(s) which respond to call but do not return final rack response & an Object that responds to call which returns final rack response which includes HTTP response code(200,404, 500 etc). so typically there will be multiple objects which act as middlewares & then a object which returns final rack response with response code.

Difference between use & run

Now with this, it seems can we can invoke use multiple times, once for each middleware & run only once in a single Rack App. so use will only invoke a middleware, while run will run the rack object which will return final rack response with HTTP status code.

example config.ru

use MyCustomMiddleware
use MyCustomMiddleware2
use MyCustomMiddleware3
run MyApp

In case if anything above is wrong, Let me know. So I can correct it.

like image 162
CuriousMind Avatar answered Oct 08 '22 10:10

CuriousMind