Rack

From DreamHost
Jump to: navigation, search

Overview

Rack is an interface implemented by Passenger which operates between the Apache HTTP Server and web applications developed in the Ruby programming language. Rack boils down the API for web servers, web frameworks, and software in between (the so-called middleware) into a single method call. Rack is particularly popular as an interface to the Ruby on Rails web framework.

You do not have to install any software to use Rack's interface at Dreamhost. It is implemented automatically as part of Passenger.

Under normal circumstances (e.g. when used to invoke a Ruby on Rails application), Rack provides a light-weight and straightforward pass-through interface. However, Rack has the ability to provided very effective interface features for a variety of web software. (See the links at the end of this page for further information.)

Basic Flow of Control

When a request is made to a domain (or subdomain) with Passenger enabled, the Apache HTTP Server invokes Passenger which in turn invokes the Rack interface module. Rack in turn invokes a file with the name "config.ru" that is located in the domain's root directory (i.e. the parent of the domain's "public" directory.) The Ruby code contained in config.ru in turn invokes the desired web framework or application.

The Ruby code responds with a three element Ruby array containing the return code, header information, and response body respectively.

Hello World! Example

If a domain or subdomain at Dreamhost is configured to use Passenger, then the following single line of Ruby code can be placed in a file named "config.ru" in the domain's root directory to provide a minimally functional example website -

run Proc.new {|env| [200, {"Content-Type" => "text/html"}, "Hello World!<br />"+env.inspect]}

NOTE: "config.ru" is NOT placed in the domain's "public" directory but rather in the directory above (i.e. the parent of) the "public" directory.

Once config.ru is created and Passenger/Rack is reinitialized (see next section), any request to the designated domain/subdomain (except those that map to files that exist in the "public" subdirectory) will be dispatched to the Ruby code contained in config.ru.

See link below to the Passenger documentation for a more complete "Hello World!" example.

Getting Passenger/Rack to Recognize Changes in Application Code/Configuration Files

Whenever the code or configuration files for your application has changed, creating or updating the modification date of the file "tmp/restart.txt" in the domain's root directory tree will tell Passenger to reinitialize the application. Passenger caches many resources and so changes will generally not be recognized unless the modification date of "tmp/restart.txt" is changed. The most common method to make this change is to invoke "touch tmp/restart.txt" from the shell. (Ruby on Rails automatically creates a directory named "tmp". If you are creating non-RoR application, you may need to create the "tmp" directory manually.)


Using Rack with Other Web Frameworks

Passenger is most often used in conjunction with Ruby on Rails. However, it has been known to work with a variety of other Rack-compliant web frameworks including (but not limited to) Camping, Padrino, Sinatra, and Ramaze. However, little or no support is provided by Dreamhost for these other frameworks. It is also possible to write ones own custom Rack-compliant applications and frameworks.

See Also

External Links