Rack (web server interface)

Rack, a Ruby Webserver Interface
Original author(s) Christian Neukirchen
Developer(s) James Tucker, Josh Peek, José Valim, Michael Fellinger, Aaron Patterson, Santiago Pastorino, Konstantin Haase
Stable release
1.5.1 / January 28, 2013 (2013-01-28)
Operating system Cross-platform
Type Middleware
License MIT License
Website rack.github.io

Rack provides a modular and adaptable interface for developing web applications in Ruby. By wrapping HTTP requests and responses it unifies the API for web servers, web frameworks, and software in between (called middleware) into a single method call.

Rack is used by many Ruby web frameworks and libraries, such as Ruby On Rails and Sinatra. It is available as a Ruby Gem.

Rack has already inspired a JavaScript framework[1] (jackjs) and a Perl one (Plack), a Common Lisp one[2] (Clack), and has resulted in the Ruby developer quasi-standard of "rack-compliant".[3]

It was also cited as an inspiration for OWIN.[4]

Overview

The characteristics of a Rack application is that the application object responds to the call method. The call method takes in the Environment object as argument and returns the Rack response object.

Environment[5]

The environment that is taken as argument by the call method refers to an object that has:
a) Information on the HTTP Request This includes the information like:

b) Rack specific information This includes the information like

In case the application is being used as a middleware, the environment can have objects that would provide session information, logging capabilities, information on the size of the data that can be used for read and writes etc. In addition to these, the server can store their own data in the environment.

Rack response[5]

The rack server object returns a response which contains three parts: the status, headers and the body.

Rack::Response provides a convenient interface to create a Rack response. The class Rake::Response is defined in lib/rack/response.rb. To use the Response class, instantiate it from the middleware layer down the stack. It can be used to modify the cookies.

Middleware in racks[5]

Rack makes it easy to add a chain of middleware components between the application and the web server. Multiple middleware components can be used in the rack which modifies the request/response before handing it over to the next component. This is called middleware stack.

The Rack server adds multiple middle middleware by default for the functionalities like showing exception with all the details,[6] validating the request and responses according to the Rack spec[7] etc.

Example application

A Rack-compatible "Hello World" application in Ruby syntax:

# helloWorld.ru
# The application that has the call method defined.
class HelloWorld
   # Call method that would return the HTTP status code, the content type and the content.
   def call (env)
      [200, {"Content-Type" => "text/html; charset=utf-8"}, ["Hello World"]]
   end
end

The server for the above code can be initiated using "rackup helloWorld.ru" and can be accessed at http://localhost:9292/ The default port used by the Rack application is 9292.

See also

References

  1. jack - introduction. Jackjs.org. Retrieved on 2013-09-20.
  2. clacklisp.org. Retrieved on 2014-10-17.
  3. Pancake: How To Stack and Loosely Couple Rack-Based Webapps Together. Rubyinside.com (2009-12-04). Retrieved on 2013-09-20.
  4. http://www.asp.net/aspnet/overview/owin-and-katana/an-overview-of-project-katana. Asp.net. Retrieved on 2014-10-01.
  5. 1 2 3 "Documentation for rack". www.rubydoc.info. Retrieved 2016-09-14.
  6. "Rack::ShowExceptions". www.rubydoc.info. Retrieved 2016-09-14.
  7. "Rack::Lint". www.rubydoc.info. Retrieved 2016-09-14.
This article is issued from Wikipedia - version of the 9/28/2016. The text is available under the Creative Commons Attribution/Share Alike but additional terms may apply for the media files.