#!/usr/bin/env ruby # * George Moschovitis # (c) 2004-2005 Navel, all rights reserved. # $Id$ require 'optparse' require 'nitro/conf' module N # The Runner is a helper class that encapsulates a web # application and is responsible for launching the # application in differnt modes. # # The runner provides default parsing of command line # and environment parameters. # # The default execution modes are: # # :debug, :stage, :live # # You can implement your own, custom version of the Runner # to run your custom web applications. class Runner # Execution mode = (:debug, :stage, :live) # # [:debug] # useful when debugging, extra debug information # is emmited, actions, templates and shaders are # reloaded, etc. The execution speed of the application # is impaired. # # [:stage] # test the application with live parameters # (typically on a staging server). # # [:live] # use the parameters for the live (production) # server. Optimized for speed. # # The default mode is :debug attr_accessor :mode # :start, :stop, :restart attr_accessor :action # The server used to run this web application. # :webrick, :nitro, :lhttp, :apache, :mod_apache # # At the moment only :webrick and :lhttp are available. attr_accessor :server # Parse the command line arguments and the environment # parameters to setup the application. def setup @mode = :debug @action = :start @server = :webrick @daemon = false parser = OptionParser.new do |opts| opts.banner = 'Usage: run.rb [options]' opts.separator '' opts.separator 'Specific options:' opts.on('-s', '--start', 'Start application.') do @action = :start end opts.on('-S', '--stop', 'Stop application.') do @action = :stop end opts.on('-r', '--restart', 'Restart application.') do @action = :restart end opts.on('-d', '--daemon', 'Run application as a daemon.') do @daemon = true end opts.on('-D', '--debug', 'Run application in debug mode.') do @mode = :debug end opts.on('-T', '--stage', 'Run application in stage mode.') do @mode = :stage end opts.on('-L', '--live', 'Run application in live mode.') do @mode = :live end opts.on('-w', '--webrick', 'Use a webrick server [default].') do @server = :webrick end opts.on('-l', '--lhttpd', 'Use a lighttpd server.') do @server = :lhttpd Logger.set(Logger.new('log/app.log')) end opts.on('-C', '--console', 'Start a console attached to an instance of the application.') do if RUBY_PLATFORM =~ /mswin32/ irb_name = 'irb.bat' else irb_name = 'irb' end ENV['NITRO_INVOKE'] = 'irb' conf_file = File.basename(caller.last.split(':').first) exec "#{irb_name} -r #{conf_file} -r irb/completion --noinspect" exit end opts.on('-l', '--lhttpd', 'Use a lighttpd server.') do @server = :lhttpd Logger.set(Logger.new('log/app.log')) end opts.on_tail('-v', '--version', 'Show version.') do puts "Nitro #{Nitro::Version}" exit end opts.on_tail('-h', '--help', 'Show this message.') do puts opts exit end end parser.parse!(ARGV) return self end # Run the application in the declared execution mode, # using the passed configuration parameters. def run(conf) if conf.is_a?(Hash) conf = Conf.new(conf) end case @mode when :debug setup_debug(conf) when :stage setup_stage(conf) when :live setup_live(conf) end if 'fcgi_proc' == ENV['NITRO_INVOKE'] invoke_fcgi_proc(conf) elsif 'irb' == ENV['NITRO_INVOKE'] invoke_irb(conf) else invoke_server(conf) end end def setup_debug(conf) $DBG = true Rendering.reload = :full end def setup_stage(conf) $DBG = false Rendering.reload = false Logger.set(Logger.new('log/app.log')) end def setup_live(conf) $DBG = false Rendering.reload = false Logger.set(Logger.new('log/app.log')) end alias_method :setup_production, :setup_live def invoke_fcgi_proc(conf) require 'nitro/adapters/fastcgi' FastCGI.start(conf) end def invoke_irb(conf) $conf = conf end def invoke_server(conf) case @action when :start case @server when :webrick require 'nitro/adapters/webrick' Webrick.start(conf) when :lhttpd require 'nitro/adapters/fastcgi' `lighttpd -f conf/lhttpd.conf` end when :stop end end # Helper method. def self.run(conf) runner = Runner.new.setup.run(conf) end end end