Subsecciones

rackup

Introducción

  1. The Rack gem gives you a rackup command which lets you start your app on any supported application server.

  2. rackup is a useful tool for running Rack applications, which uses the Rack::Builder DSL to configure middleware and build up applications easily.

  3. rackup automatically figures out the environment it is run in, and runs your application as FastCGI, CGI, or standalone with Mongrel or WEBrick, all from the same configuration.

De hecho este es todo el código del ejecutable rackup

#!/usr/bin/env ruby

require "rack"
Rack::Server.start

El método start starts a new rack server (like running rackup). This will parse ARGV and provide standard ARGV rackup options, defaulting to load config.ru.

Providing an option hash will prevent ARGV parsing and will not include any default options.

This method can be used to very easily launch a CGI application, for example:

  Rack::Server.start(
    :app => lambda do |e|
      [200, {'Content-Type' => 'text/html'}, ['hello world']]
    end,
    :server => 'cgi'
  )

Further options available here are documented on Rack::Server#initialize (véase el código en Rack::Server ):

    def self.start(options = nil)
      new(options).start
    end

como se ve, el código de Rack::Server está en Github.

The Options of start and new may include:

  1. :app a rack application to run (overrides :config)
  2. :config a rackup configuration file path to load (.ru)
  3. :environment this selects the middleware that will be wrapped around your application. Default options available are:
    1. development: CommonLogger, ShowExceptions, and Lint
    2. deployment: CommonLogger
    3. none: no extra middleware
    note: when the server is a cgi server, CommonLogger is not included.
  4. :server choose a specific Rack::Handler, e.g. cgi, fcgi, webrick
  5. :daemonize if true, the server will daemonize itself (fork, detach, etc)
  6. :pid path to write a pid file after daemonize
  7. :Host the host address to bind to (used by supporting Rack::Handler)
  8. :Port the port to bind to (used by supporting Rack::Handler)
  9. :AccessLog webrick acess log options (or supporting Rack::Handler)
  10. :debug turn on debug output ($DEBUG = true)
  11. :warn turn on warnings ($-w = true)
  12. :include add given paths to $LOAD_PATH
  13. :require require the given libraries

Ejemplo de uso

Si no se especifica, rackup busca un fichero con nombre config.ru.

[~/local/src/ruby/sinatra/rack/rackup/simple(master)]$ cat config.ru 
require './myapp'
run MyApp.new

Esta es la aplicación:

[~/local/src/ruby/sinatra/rack/rackup/simple(master)]$ cat myapp.rb 
class MyApp
  def call env
    [200, {"Content-Type" => "text/html"}, ["Hello Rack Participants"]] 
  end
end

[~/local/src/ruby/sinatra/rack/rackup/simple(master)]$ cat Rakefile 
task :default => :server

desc "run server"
task :server do
  sh "rackup"
end

desc "run client via curl"
task :client do
  sh "curl -v localhost:9292"
end

Ejecución

[~/local/src/ruby/sinatra/rack/rackup/simple(master)]$ rackup
>> Thin web server (v1.5.1 codename Straight Razor)
>> Maximum connections set to 1024
>> Listening on 0.0.0.0:9292, CTRL+C to stop

[~/local/src/ruby/sinatra/rack/rackup/simple(master)]$ curl -v localhost:9292
* About to connect() to localhost port 9292 (#0)
*   Trying ::1... Connection refused
*   Trying 127.0.0.1... connected
* Connected to localhost (127.0.0.1) port 9292 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.21.4 (universal-apple-darwin11.0) libcurl/7.21.4 OpenSSL/0.9.8y zlib/1.2.5
> Host: localhost:9292
> Accept: */*
> 
< HTTP/1.1 200 OK
< Content-Type: text/html
< Transfer-Encoding: chunked
< Connection: close
< Server: thin 1.5.1 codename Straight Razor
< 
* Closing connection #0
Hello Rack Participants

Opciones del ejecutable rackup

  1. Véase rackup.

[~]$ rackup --help
Usage: rackup [ruby options] [rack options] [rackup config]

Ruby options:
  -e, --eval LINE          evaluate a LINE of code
  -d, --debug              set debugging flags (set $DEBUG to true)
  -w, --warn               turn warnings on for your script
  -I, --include PATH       specify $LOAD_PATH (may be used more than once)
  -r, --require LIBRARY    require the library, before executing your script

Rack options:
  -s, --server SERVER      serve using SERVER (webrick/mongrel)
  -o, --host HOST          listen on HOST (default: 0.0.0.0)
  -p, --port PORT          use PORT (default: 9292)
  -O NAME[=VALUE],         pass VALUE to the server as option NAME. 
                           If no VALUE, sets it to true. 
                           Run 
                           'rackup -s SERVER -h' 
                           to get a list of options for SERVER
      --option
  -E, --env ENVIRONMENT    use ENVIRONMENT for defaults (default: development)
  -D, --daemonize          run daemonized in the background
  -P, --pid FILE           file to store PID (default: rack.pid)

Common options:
  -h, -?, --help           Show this message
      --version            Show version

Especificación de Opciones en la primera línea

Si la primera línea de un fichero config.ru empieza por \# es tratada como una línea de opciones permitiendo así que los argumentos de rackup se especifiquen en el fichero de configuración:

#\-w -p 8765

use Rack::Reloader, 0
use Rack::ContentLength

app = proc do |env|
  [200, {'content-Type' => 'text/plain' }, ['a']]
end

run app

Casiano Rodríguez León
2015-01-25