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:
:app
a rack application to run (overrides :config)
:config
a rackup configuration file path to load (.ru)
:environment
this selects the middleware that will be wrapped around
your application. Default options available are:
:server
choose a specific Rack::Handler, e.g. cgi, fcgi, webrick
:daemonize
if true, the server will daemonize itself (fork, detach, etc)
:pid
path to write a pid file after daemonize
:Host
the host address to bind to (used by supporting Rack::Handler)
:Port
the port to bind to (used by supporting Rack::Handler)
:AccessLog
webrick acess log options (or supporting Rack::Handler)
:debug
turn on debug output ($DEBUG = true)
:warn
turn on warnings ($-w = true
)
:include
add given paths to $LOAD_PATH
:require
require the given libraries
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
[~/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
[~]$ 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
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