| Rack, a Ruby Webserver Interface | |
|---|---|
| Original author | Leah Neukirchen |
| Developers | James Tucker, Josh Peek, José Valim, Michael Fellinger, Aaron Patterson, Santiago Pastorino, Konstantin Haase |
| Stable release | |
| Operating system | Cross-platform |
| Type | Middleware |
| License | MIT License |
| Website | rack |
| Repository | |
Rack is a modular interface betweenweb servers andweb applications developed in theRuby programming language. With Rack,application programming interfaces (APIs) forweb frameworks andmiddleware arewrapped into a singlemethod call handlingHTTP requests andresponses.
Rack is used by many Ruby web frameworks andlibraries, such asRuby on Rails andSinatra. It is available as a RubyGem. Many Ruby applications are called "rack-compliant".[2]
Rack has inspired similar frameworks inJavaScript[3] (jack.js),Clojure,[4]Perl (Plack),Common Lisp (Clack),[5] and.NET (OWIN).[6]
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.
Source:[7]
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.
Source:[7]
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 classRack::Response is defined in lib/rack/response.rb. To use theResponse class, instantiate it from the middleware layer down the stack. It can be used to modify the cookies.
Source:[7]
Rack makes it easy to add a chain ofmiddleware 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,[8] validating the request and responses according to the Rack spec[9] etc.
A Rack-compatible "Hello World" application inRuby syntax:
# helloWorld.ru# The application that has the call method defined.classHelloWorld# Call method that would return the HTTP status code, the content type and the content.defcall(env)[200,{"content-type"=>"text/html; charset=utf-8"},["Hello World"]]endendrunHelloWorld.new
The server for the above code can be initiated using "rackup helloWorld.ru" and can be accessed athttp://localhost:9292/ The default port used by the Rack application is 9292.