From 3a5dfe36b81d50db2c41ebe0b7bb99bb5ca8b738 Mon Sep 17 00:00:00 2001 From: zedshaw Date: Sun, 12 Feb 2006 01:30:33 +0000 Subject: Initial command/plugin setup for Mongrel git-svn-id: svn+ssh://rubyforge.org/var/svn/mongrel/trunk@30 19e92222-5c0b-0410-8929-a290d50e31e9 --- bin/mongrel_rails | 78 +++++++++++++++++++++++++++++++++----------------- examples/simpletest.rb | 13 +++++++-- 2 files changed, 61 insertions(+), 30 deletions(-) diff --git a/bin/mongrel_rails b/bin/mongrel_rails index 0107c32..a184659 100644 --- a/bin/mongrel_rails +++ b/bin/mongrel_rails @@ -1,12 +1,8 @@ require 'rubygems' require 'mongrel' require 'cgi' -begin - require 'daemons/daemonize' - HAVE_DAEMONS=true -rescue - HAVE_DAEMONS=false -end +require 'daemons/daemonize' +require 'mongrel/command' class CGIFixed < ::CGI @@ -87,28 +83,56 @@ class RailsHandler < Mongrel::HttpHandler end -if ARGV.length != 2 - STDERR.puts "usage: mongrel_rails " - exit(1) -end - -# store this for later since Daemonize insists on going to the / root -cwd = Dir.pwd +class StartCommand < Mongrel::Command::Command -if HAVE_DAEMONS - STDERR.puts "Running Mongrel in the background. See log/mongrel.log for errors." - Daemonize.daemonize(log_file=File.join(cwd,"log","mongrel.log")) -else - STDERR.puts "Unable to daemonize. Running in foreground. Use CTRL-C to stop." -end + def configure + options [ + ["-e", "--environment ENV", "Rails environment to run as", :@environment, "production"], + ["-d", "--daemonize", "Whether to run in the background or not", :@daemon, false], + ['-p', '--port PORT', "Which port to bind to", :@port, 3000], + ['-a', '--address ADDR', "Address to bind to", :@address, "0.0.0.0"], + ['-l', '--log FILE', "Where to write log messages", :@log_file, "log/mongrel.log"], + ['-P', '--pid FILE', "Where to write the PID", :@pid_file, "log/mongrel.pid"] + ] + end + + def validate + valid? ["production","development"].include?(@environment), "Only valid environments are 'production' or 'development'" + valid_dir? File.dirname(@log_file), "Path to log file not valid: #@log_file" + valid_dir? File.dirname(@pid_file), "Path to pid file not valid: #@pid_file" + + return @valid + end -# and go back -Dir.chdir(cwd) do - require 'config/environment' - open(File.join(cwd,"log/mongrel-#{ARGV[1]}.pid"),"w") {|f| f.write(Process.pid) } - h = Mongrel::HttpServer.new(ARGV[0], ARGV[1]) - h.register("/", RailsHandler.new(File.join(cwd,"public"))) - h.run - h.acceptor.join + def run + cwd = Dir.pwd + + if @daemon + STDERR.puts "Running as Daemon at #@address:#@port" + Daemonize.daemonize(log_file=@log_file) + open(File.join(cwd,@pid_file),"w") {|f| f.write(Process.pid) } + # change back to the original starting directory + Dir.chdir(cwd) + else + STDERR.puts "Running at #@address:#@port" + end + + + require 'config/environment' + h = Mongrel::HttpServer.new(@address, @port) + h.register("/", RailsHandler.new(File.join(cwd,"public"))) + h.run + + begin + h.acceptor.join + rescue Interrupt + STDERR.puts "Interrupted." + end + end end + + + + +Mongrel::Command::Registry.instance.run ARGV diff --git a/examples/simpletest.rb b/examples/simpletest.rb index 893df96..8f78461 100644 --- a/examples/simpletest.rb +++ b/examples/simpletest.rb @@ -1,13 +1,20 @@ require 'mongrel' require 'yaml' +require 'zlib' class SimpleHandler < Mongrel::HttpHandler def process(request, response) response.start do |head,out| head["Content-Type"] = "text/html" - out << "Your request:
" - out << "
#{request.params.to_yaml}
" - out << "View the files." + results = "Your request:
#{request.params.to_yaml}
View the files." + if not request.params["HTTP_ACCEPT_ENCODING"] == "gzip,deflate" + head["Content-Encoding"] = "deflate" + # send it back deflated + out << Zlib::Deflate.deflate(results) + else + # no gzip supported, send it back normal + out << results + end end end end -- cgit v1.2.3-24-ge0c7