Web Development in Ruby : Using a Rackup File to Run Rack App

Objective


To learn how to use rackup command to run Rack app.

Discussion


Rack is also an implementation. The Rack gem provides helper classes for : Request / Response wrappers, logging, authentication (basic and digest), cookies, sessions, mock requests and responses.

Rack gem gives you rackup command which lets you start your app on any supported application server. The rackup command is a useful tool for running Rack applications, which uses the Rack::Builder DSL to configure middleware and build up applications easily. It automatically figures out the environment it is running in, and runs your application as FastCGI, CGI, or standalone with Mongrel or WEBrick--all from the same configuration.

Steps


Step 1

Create a basic_rack.ru file with:

app = lambda {|env| [200, {'Content-Type': 'text/html'}, 'Hello World']}
run app

The .ru extension stands for Rack's rackup executable. These files are regular Ruby files.

Step 2

Start the app using:

$rackup basic_rack.ru

Browse to http://localhost:9292.

Step 3

You now see the text : 'Hello World' in the browser.

Step 4

Now let's look at an example where we can use variables.

class HelloWorld
  def initialize(name)
    @name = name
  end

  def call(env)
    [200, {'Content-Type' => 'text/plain'}, ["Hello #{@name}"]]
  end
end

app = HelloWorld.new("Bugs Bunny")
run app

Step 5

This will print 'Hello Bugs Bunny' in the browser.

Discussion


The method call takes an environment Hash representing a request. It returns a three part array with each of the parts of a HTTP response. Here is the code for Rack Handler for Thin Server.

require "thin"
require "rack/content_length"
require "rack/chunked"

module Rack
  module Handler
    class Thin
      def self.run(app, options={})
        environment  = ENV['RACK_ENV'] || 'development'
        default_host = environment == 'development' ? 'localhost' : '0.0.0.0'

        host = options.delete(:Host) || default_host
        port = options.delete(:Port) || 8080
        args = [host, port, app, options]
        # Thin versions below 0.8.0 do not support additional options
        args.pop if ::Thin::VERSION::MAJOR < 1 && ::Thin::VERSION::MINOR < 8
        server = ::Thin::Server.new(*args)
        yield server if block_given?
        server.start
      end

      def self.valid_options
        environment  = ENV['RACK_ENV'] || 'development'
        default_host = environment == 'development' ? 'localhost' : '0.0.0.0'

        {
          "Host=HOST" => "Hostname to listen on (default: #{default_host})",
          "Port=PORT" => "Port to listen on (default: 8080)",
        }
      end
    end
  end
end

You can see how it abstracts running a specific server behind a well defined interface.

Exercises


  1. Use rackup file to run the lobster example.
  2. Use rackup file to run the protected lobster example that uses HTTP Basic authentication.

Summary


In this article, you learned how to use rackup file to run Rack apps.


Related Articles


Create your own user feedback survey