Skip to content

Commit

Permalink
Initial command/plugin setup for Mongrel
Browse files Browse the repository at this point in the history
git-svn-id: svn+ssh://rubyforge.org/var/svn/mongrel/trunk@30 19e92222-5c0b-0410-8929-a290d50e31e9
  • Loading branch information
zedshaw committed Feb 12, 2006
1 parent 4595749 commit 3a5dfe3
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 30 deletions.
78 changes: 51 additions & 27 deletions 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
Expand Down Expand Up @@ -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
13 changes: 10 additions & 3 deletions 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
Expand Down

0 comments on commit 3a5dfe3

Please sign in to comment.