lib/heel/server.rb in heel-0.2.0 vs lib/heel/server.rb in heel-0.3.0

- old
+ new

@@ -23,14 +23,10 @@ (ENV["HOMEPATH"] && "#{ENV["HOMEDRIVE"]}#{ENV["HOMEPATH"]}") || "/" end end - DEFAULT_DIRECTORY = File.join(home_directory,".heel") - DEFAULT_PID_FILE = File.join(DEFAULT_DIRECTORY,"heel.pid") - DEFAULT_LOG_FILE = File.join(DEFAULT_DIRECTORY,"heel.log") - def initialize(argv = []) argv ||= [] set_io @@ -51,18 +47,31 @@ def default_options if @default_options.nil? then @default_options = ::OpenStruct.new @default_options.show_version = false @default_options.show_help = false - @default_options.address = "0.0.0.0" + @default_options.address = "127.0.0.1" @default_options.port = 4331 @default_options.document_root = Dir.pwd @default_options.daemonize = false @default_options.kill = false + @default_options.launch_browser = true end return @default_options end + + def default_directory + ENV["HEEL_DEFAULT_DIRECTORY"] || File.join(::Heel::Server.home_directory,".heel") + end + + def pid_file + File.join(default_directory,"heel.pid") + end + + def log_file + File.join(default_directory,"heel.log") + end def option_parser OptionParser.new do |op| op.separator "" @@ -80,10 +89,14 @@ end op.on("-k", "--kill", "Kill an existing daemonized heel process") do @parsed_options.kill = true end + + op.on("-l", "--[no-]launch-browser", "Control automatically launching a browser") do |l| + @parsed_options.launch_browser = l + end op.on("-p", "--port PORT", Integer, "Port to bind to", "(default: #{default_options.port})") do |port| @parsed_options.port = port end @@ -134,18 +147,18 @@ end end # kill an already running background heel process def kill_existing_proc - if File.exists?(DEFAULT_PID_FILE) then + if File.exists?(pid_file) then begin - pid = open(DEFAULT_PID_FILE).read.to_i + pid = open(pid_file).read.to_i @stdout.puts "Sending TERM to process #{pid}" Process.kill("TERM", pid) rescue Errno::ESRCH @stdout.puts "Process does not exist. Removing stale pid file." - File.unlink(DEFAULT_PID_FILE) + File.unlink(pid_file) end else @stdout.puts "No pid file exists, no process to kill" end @stdout.puts "Done." @@ -153,15 +166,15 @@ end # setup the directory that heel will use as the location to run from, where its logs will # be stored and its PID file if backgrounded. def setup_heel_dir - if not File.exists?(DEFAULT_DIRECTORY) then - FileUtils.mkdir_p(DEFAULT_DIRECTORY) - @stdout.puts "Created #{DEFAULT_DIRECTORY}" - @stdout.puts "PID file #{DEFAULT_PID_FILE} is stored here" - @stdout.puts "along with the log #{DEFAULT_LOG_FILE}" + if not File.exists?(default_directory) then + FileUtils.mkdir_p(default_directory) + @stdout.puts "Created #{default_directory}" + @stdout.puts "PID file #{pid_file} is stored here" + @stdout.puts "along with the log #{log_file}" end end # run the heel server with the current options. def run @@ -171,19 +184,19 @@ setup_heel_dir document_root = options.document_root background_me = options.daemonize stats = ::Mongrel::StatisticsFilter.new(:sample_rate => 1) - config = ::Mongrel::Configurator.new :host => options.address, :port => options.port, :pid_file => DEFAULT_PID_FILE do + config = ::Mongrel::Configurator.new :host => options.address, :port => options.port, :pid_file => pid_file do if background_me then - if File.exists?(DEFAULT_PID_FILE) then - log "ERROR: PID File #{DEFAULT_PID_FILE} already exists. Heel may already be running." - log "ERROR: Check the Log file #{DEFAULT_LOG_FILE}" + if File.exists?(pid_file) then + log "ERROR: PID File #{pid_file} already exists. Heel may already be running." + log "ERROR: Check the Log file #{log_file}" log "ERROR: Heel will not start until the .pid file is cleared." exit 1 end - daemonize({:cwd => DEFAULT_DIRECTORY, :log_file => DEFAULT_LOG_FILE}) + daemonize({:cwd => default_directory, :log_file => log_file}) end listener do uri "/", :handler => stats uri "/", :handler => Heel::DirHandler.new({:document_root => document_root}) @@ -203,11 +216,13 @@ config.write_pid_file else config.log "Use Ctrl-C to stop." end - config.log "Launching your browser..." - ::Launchy.do_magic("http://#{options.address}:#{options.port}/") + if options.launch_browser then + config.log "Launching your browser..." + ::Launchy.open("http://#{options.address}:#{options.port}/") + end config.join end