mirror of
https://github.com/puma/puma.git
synced 2022-11-09 13:48:40 -05:00
Final touches to get mongrel_rails working. Fixed URI registration bug. Small change to config tool.
git-svn-id: svn+ssh://rubyforge.org/var/svn/mongrel/trunk@123 19e92222-5c0b-0410-8929-a290d50e31e9
This commit is contained in:
parent
f2b53a3a4b
commit
4d9966ee00
6 changed files with 73 additions and 30 deletions
|
@ -22,6 +22,7 @@ class Start < GemPlugin::Plugin "/commands"
|
||||||
['-c', '--chdir PATH', "Change to dir before starting (will be expanded)", :@cwd, Dir.pwd],
|
['-c', '--chdir PATH', "Change to dir before starting (will be expanded)", :@cwd, Dir.pwd],
|
||||||
['-r', '--root PATH', "Set the document root (default 'public')", :@docroot, "public"],
|
['-r', '--root PATH', "Set the document root (default 'public')", :@docroot, "public"],
|
||||||
['-B', '--debug', "Enable debugging mode", :@debug, false],
|
['-B', '--debug', "Enable debugging mode", :@debug, false],
|
||||||
|
['-C', '--config PATH', "Use a config file", :@config_file, nil]
|
||||||
]
|
]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -36,18 +37,24 @@ class Start < GemPlugin::Plugin "/commands"
|
||||||
valid_dir? File.dirname(@pid_file), "Path to pid file not valid: #@pid_file"
|
valid_dir? File.dirname(@pid_file), "Path to pid file not valid: #@pid_file"
|
||||||
valid_dir? @docroot, "Path to docroot not valid: #@docroot"
|
valid_dir? @docroot, "Path to docroot not valid: #@docroot"
|
||||||
valid_exists? @mime_map, "MIME mapping file does not exist: #@mime_map" if @mime_map
|
valid_exists? @mime_map, "MIME mapping file does not exist: #@mime_map" if @mime_map
|
||||||
|
valid_exists? @config_file, "Config file not there: #@config_file" if @config_file
|
||||||
return @valid
|
return @valid
|
||||||
end
|
end
|
||||||
|
|
||||||
def run
|
def run
|
||||||
|
|
||||||
|
# command line setting override config file settings
|
||||||
settings = { :host => @address, :port => @port, :cwd => @cwd,
|
settings = { :host => @address, :port => @port, :cwd => @cwd,
|
||||||
:log_file => @log_file, :pid_file => @pid_file, :environment => @environment,
|
:log_file => @log_file, :pid_file => @pid_file, :environment => @environment,
|
||||||
:docroot => @docroot, :mime_map => @mime_map, :daemon => @daemon,
|
:docroot => @docroot, :mime_map => @mime_map, :daemon => @daemon,
|
||||||
:debug => @debug, :includes => ["mongrel"]
|
:debug => @debug, :includes => ["mongrel"]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if @config_file
|
||||||
|
STDERR.puts "** Loading settings from #{@config_file} (command line options override)."
|
||||||
|
conf = YAML.load_file(@config_file)
|
||||||
|
settings = conf.merge(settings)
|
||||||
|
end
|
||||||
|
|
||||||
config = Mongrel::Rails::RailsConfigurator.new(settings) do
|
config = Mongrel::Rails::RailsConfigurator.new(settings) do
|
||||||
log "Starting Mongrel in #{settings[:environment]} mode at #{settings[:host]}:#{settings[:port]}"
|
log "Starting Mongrel in #{settings[:environment]} mode at #{settings[:host]}:#{settings[:port]}"
|
||||||
|
|
9
examples/mongrel.conf
Normal file
9
examples/mongrel.conf
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
---
|
||||||
|
:environment: production
|
||||||
|
:daemon: "true"
|
||||||
|
:host: 0.0.0.0
|
||||||
|
:log_file: log/mongrel.log
|
||||||
|
:docroot: public
|
||||||
|
:debug: "false"
|
||||||
|
:port: 3000
|
||||||
|
:pid_file: log/mongrel.pid
|
|
@ -5,7 +5,6 @@ require 'stringio'
|
||||||
require 'mongrel/cgi'
|
require 'mongrel/cgi'
|
||||||
require 'mongrel/handlers'
|
require 'mongrel/handlers'
|
||||||
require 'mongrel/command'
|
require 'mongrel/command'
|
||||||
require 'timeout'
|
|
||||||
require 'mongrel/tcphack'
|
require 'mongrel/tcphack'
|
||||||
require 'yaml'
|
require 'yaml'
|
||||||
|
|
||||||
|
@ -256,11 +255,15 @@ module Mongrel
|
||||||
# Receives a block passing it the header and body for you to work with.
|
# Receives a block passing it the header and body for you to work with.
|
||||||
# When the block is finished it writes everything you've done to
|
# When the block is finished it writes everything you've done to
|
||||||
# the socket in the proper order. This lets you intermix header and
|
# the socket in the proper order. This lets you intermix header and
|
||||||
# body content as needed.
|
# body content as needed. Handlers are able to modify pretty much
|
||||||
def start(status=200)
|
# any part of the request in the chain, and can stop further processing
|
||||||
|
# by simple passing "finalize=true" to the start method. By default
|
||||||
|
# all handlers run and then mongrel finalizes the request when they're
|
||||||
|
# all done.
|
||||||
|
def start(status=200, finalize=false)
|
||||||
@status = status.to_i
|
@status = status.to_i
|
||||||
yield @header, @body
|
yield @header, @body
|
||||||
finished
|
finished if finalize
|
||||||
end
|
end
|
||||||
|
|
||||||
# Primarily used in exception handling to reset the response output in order to write
|
# Primarily used in exception handling to reset the response output in order to write
|
||||||
|
@ -268,7 +271,7 @@ module Mongrel
|
||||||
# sent the header or the body. This is pretty catastrophic actually.
|
# sent the header or the body. This is pretty catastrophic actually.
|
||||||
def reset
|
def reset
|
||||||
if @body_sent
|
if @body_sent
|
||||||
raise "You ahve already sent the request body."
|
raise "You have already sent the request body."
|
||||||
elsif @header_sent
|
elsif @header_sent
|
||||||
raise "You have already sent the request headers."
|
raise "You have already sent the request headers."
|
||||||
else
|
else
|
||||||
|
@ -278,23 +281,29 @@ module Mongrel
|
||||||
end
|
end
|
||||||
|
|
||||||
def send_status
|
def send_status
|
||||||
status = "HTTP/1.1 #{@status} #{HTTP_STATUS_CODES[@status]}\r\nContent-Length: #{@body.length}\r\nConnection: close\r\n"
|
if not @status_sent
|
||||||
@socket.write(status)
|
status = "HTTP/1.1 #{@status} #{HTTP_STATUS_CODES[@status]}\r\nContent-Length: #{@body.length}\r\nConnection: close\r\n"
|
||||||
@status_sent = true
|
@socket.write(status)
|
||||||
|
@status_sent = true
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def send_header
|
def send_header
|
||||||
@header.out.rewind
|
if not @header_sent
|
||||||
@socket.write(@header.out.read)
|
@header.out.rewind
|
||||||
@socket.write("\r\n")
|
@socket.write(@header.out.read)
|
||||||
@header_sent = true
|
@socket.write("\r\n")
|
||||||
|
@header_sent = true
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def send_body
|
def send_body
|
||||||
@body.rewind
|
if not @body_sent
|
||||||
# connection: close is also added to ensure that the client does not pipeline.
|
@body.rewind
|
||||||
@socket.write(@body.read)
|
# connection: close is also added to ensure that the client does not pipeline.
|
||||||
@body_sent = true
|
@socket.write(@body.read)
|
||||||
|
@body_sent = true
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# This takes whatever has been done to header and body and then writes it in the
|
# This takes whatever has been done to header and body and then writes it in the
|
||||||
|
@ -306,7 +315,7 @@ module Mongrel
|
||||||
end
|
end
|
||||||
|
|
||||||
def done
|
def done
|
||||||
@status_sent && @header_sent && @body_sent
|
(@status_sent and @header_sent and @body_sent)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -332,6 +341,7 @@ module Mongrel
|
||||||
class HttpServer
|
class HttpServer
|
||||||
attr_reader :acceptor
|
attr_reader :acceptor
|
||||||
attr_reader :workers
|
attr_reader :workers
|
||||||
|
attr_reader :classifier
|
||||||
|
|
||||||
# Creates a working server on host:port (strange things happen if port isn't a Number).
|
# Creates a working server on host:port (strange things happen if port isn't a Number).
|
||||||
# Use HttpServer::run to start the server and HttpServer.acceptor.join to
|
# Use HttpServer::run to start the server and HttpServer.acceptor.join to
|
||||||
|
@ -387,6 +397,10 @@ module Mongrel
|
||||||
break if response.done
|
break if response.done
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if not response.done
|
||||||
|
response.finished
|
||||||
|
end
|
||||||
|
|
||||||
else
|
else
|
||||||
client.write(Const::ERROR_404_RESPONSE)
|
client.write(Const::ERROR_404_RESPONSE)
|
||||||
end
|
end
|
||||||
|
@ -482,11 +496,14 @@ module Mongrel
|
||||||
def register(uri, handler)
|
def register(uri, handler)
|
||||||
script_name, path_info, handlers = @classifier.resolve(uri)
|
script_name, path_info, handlers = @classifier.resolve(uri)
|
||||||
|
|
||||||
if not handlers or (path_info and path_info != "/" and path_info.length == 0)
|
if not handlers
|
||||||
# new uri that is just longer
|
|
||||||
@classifier.register(uri, [handler])
|
@classifier.register(uri, [handler])
|
||||||
else
|
else
|
||||||
handlers << handler
|
if path_info.length == 0 or (script_name == "/" and path_info == "/")
|
||||||
|
handlers << handler
|
||||||
|
else
|
||||||
|
@classifier.register(uri, [handler])
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -745,9 +762,9 @@ module Mongrel
|
||||||
MongrelDbg.begin_trace :rails
|
MongrelDbg.begin_trace :rails
|
||||||
MongrelDbg.begin_trace :files
|
MongrelDbg.begin_trace :files
|
||||||
|
|
||||||
uri "/", :handler => plugin("/handlers/requestlog::files")
|
uri location, :handler => plugin("/handlers/requestlog::files")
|
||||||
uri "/", :handler => plugin("/handlers/requestlog::objects")
|
uri location, :handler => plugin("/handlers/requestlog::objects")
|
||||||
uri "/", :handler => plugin("/handlers/requestlog::params")
|
uri location, :handler => plugin("/handlers/requestlog::params")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -89,7 +89,6 @@ module Configure::Views
|
||||||
p { span { "Port:" }; input :name => "port", :value => "4000" }
|
p { span { "Port:" }; input :name => "port", :value => "4000" }
|
||||||
p { span { "Environment:" }; input :name => "env", :value => "development" }
|
p { span { "Environment:" }; input :name => "env", :value => "development" }
|
||||||
p { span { "Address:" }; input :name => "address", :value => "0.0.0.0" }
|
p { span { "Address:" }; input :name => "address", :value => "0.0.0.0" }
|
||||||
p { span { "Number Processors:" }; input :name => "num_procs", :value => "20" }
|
|
||||||
input :type => "submit", :value => "START"
|
input :type => "submit", :value => "START"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -21,7 +21,7 @@ class Sentinel < GemPlugin::Plugin "/handlers"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
class MongrelDbgTest < Test::Unit::TestCase
|
class ConfiguratorTest < Test::Unit::TestCase
|
||||||
|
|
||||||
def test_base_handler_config
|
def test_base_handler_config
|
||||||
config = Mongrel::Configurator.new :host => "localhost" do
|
config = Mongrel::Configurator.new :host => "localhost" do
|
||||||
|
@ -30,24 +30,32 @@ class MongrelDbgTest < Test::Unit::TestCase
|
||||||
uri "/", :handler => plugin("/handlers/testplugin")
|
uri "/", :handler => plugin("/handlers/testplugin")
|
||||||
uri "/", :handler => plugin("/handlers/testplugin")
|
uri "/", :handler => plugin("/handlers/testplugin")
|
||||||
uri "/", :handler => Mongrel::DirHandler.new(".", load_mime_map("examples/mime.yaml"))
|
uri "/", :handler => Mongrel::DirHandler.new(".", load_mime_map("examples/mime.yaml"))
|
||||||
uri "/", :handler => plugin("/handlers/sentinel")
|
uri "/", :handler => plugin("/handlers/testplugin")
|
||||||
|
|
||||||
uri "/test", :handler => plugin("/handlers/testplugin")
|
uri "/test", :handler => plugin("/handlers/testplugin")
|
||||||
uri "/test", :handler => plugin("/handlers/testplugin")
|
uri "/test", :handler => plugin("/handlers/testplugin")
|
||||||
uri "/test", :handler => Mongrel::DirHandler.new(".", load_mime_map("examples/mime.yaml"))
|
uri "/test", :handler => Mongrel::DirHandler.new(".", load_mime_map("examples/mime.yaml"))
|
||||||
uri "/test", :handler => plugin("/handlers/sentinel")
|
uri "/test", :handler => plugin("/handlers/testplugin")
|
||||||
run
|
run
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
config.listeners.each do |host,listener|
|
||||||
|
puts "Registered URIs: #{listener.classifier.uris.inspect}"
|
||||||
|
assert listener.classifier.uris.length == 2, "Wrong number of registered URIs"
|
||||||
|
assert listener.classifier.uris.include?("/"), "/ not registered"
|
||||||
|
assert listener.classifier.uris.include?("/test"), "/test not registered"
|
||||||
|
end
|
||||||
|
|
||||||
res = Net::HTTP.get(URI.parse('http://localhost:3111/test'))
|
res = Net::HTTP.get(URI.parse('http://localhost:3111/test'))
|
||||||
assert res != nil, "Didn't get a response"
|
assert res != nil, "Didn't get a response"
|
||||||
assert $test_plugin_fired == 2, "Test filter plugin didn't run twice."
|
assert $test_plugin_fired == 3, "Test filter plugin didn't run 3 times."
|
||||||
|
|
||||||
|
|
||||||
res = Net::HTTP.get(URI.parse('http://localhost:3111/'))
|
res = Net::HTTP.get(URI.parse('http://localhost:3111/'))
|
||||||
assert res != nil, "Didn't get a response"
|
assert res != nil, "Didn't get a response"
|
||||||
assert $test_plugin_fired == 4, "Test filter plugin didn't run 4 times."
|
assert $test_plugin_fired == 6, "Test filter plugin didn't run 6 times."
|
||||||
|
|
||||||
config.stop
|
config.stop
|
||||||
|
|
||||||
|
|
|
@ -26,6 +26,8 @@ class ResponseTest < Test::Unit::TestCase
|
||||||
out.write("tested")
|
out.write("tested")
|
||||||
out.write("hello!")
|
out.write("hello!")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
resp.finished
|
||||||
assert io.length > 0, "output didn't have data"
|
assert io.length > 0, "output didn't have data"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -38,6 +40,7 @@ class ResponseTest < Test::Unit::TestCase
|
||||||
out.write("NOT FOUND")
|
out.write("NOT FOUND")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
resp.finished
|
||||||
assert io.length > 0, "output didn't have data"
|
assert io.length > 0, "output didn't have data"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue