about summary refs log tree commit homepage
diff options
context:
space:
mode:
-rw-r--r--bin/mongrel_rails78
-rw-r--r--examples/simpletest.rb13
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 <host> <port>"
-  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 << "<html><body>Your request:<br />"
-        out << "<pre>#{request.params.to_yaml}</pre>"
-        out << "<a href=\"/files\">View the files.</a></body></html>"
+        results = "<html><body>Your request:<br /><pre>#{request.params.to_yaml}</pre><a href=\"/files\">View the files.</a></body></html>"
+        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