2006-02-28 02:04:41 -05:00
|
|
|
require 'mongrel'
|
|
|
|
|
|
|
|
|
|
|
|
# Implements a handler that can run Rails and serve files out of the
|
|
|
|
# Rails application's public directory. This lets you run your Rails
|
|
|
|
# application with Mongrel during development and testing, then use it
|
|
|
|
# also in production behind a server that's better at serving the
|
|
|
|
# static files.
|
|
|
|
#
|
|
|
|
# The RailsHandler takes a mime_map parameter which is a simple suffix=mimetype
|
|
|
|
# mapping that it should add to the list of valid mime types.
|
|
|
|
#
|
|
|
|
# It also supports page caching directly and will try to resolve a request
|
|
|
|
# in the following order:
|
|
|
|
#
|
|
|
|
# * If the requested exact PATH_INFO exists as a file then serve it.
|
|
|
|
# * If it exists at PATH_INFO+".html" exists then serve that.
|
|
|
|
# * Finally, construct a Mongrel::CGIWrapper and run Dispatcher.dispath to have Rails go.
|
|
|
|
#
|
|
|
|
# This means that if you are using page caching it will actually work with Mongrel
|
|
|
|
# and you should see a decent speed boost (but not as fast as if you use lighttpd).
|
2006-03-04 12:55:39 -05:00
|
|
|
#
|
|
|
|
# An additional feature you can use is
|
2006-02-28 02:04:41 -05:00
|
|
|
class RailsHandler < Mongrel::HttpHandler
|
2006-03-04 12:55:39 -05:00
|
|
|
attr_reader :files
|
|
|
|
|
2006-02-28 02:04:41 -05:00
|
|
|
def initialize(dir, mime_map = {})
|
|
|
|
@files = Mongrel::DirHandler.new(dir,false)
|
|
|
|
@guard = Mutex.new
|
|
|
|
|
|
|
|
# register the requested mime types
|
|
|
|
mime_map.each {|k,v| Mongrel::DirHandler::add_mime_type(k,v) }
|
|
|
|
end
|
|
|
|
|
|
|
|
# Attempts to resolve the request as follows:
|
|
|
|
#
|
|
|
|
#
|
|
|
|
# * If the requested exact PATH_INFO exists as a file then serve it.
|
|
|
|
# * If it exists at PATH_INFO+".html" exists then serve that.
|
|
|
|
# * Finally, construct a Mongrel::CGIWrapper and run Dispatcher.dispath to have Rails go.
|
|
|
|
def process(request, response)
|
|
|
|
return if response.socket.closed?
|
|
|
|
|
2006-03-04 12:55:39 -05:00
|
|
|
path_info = request.params[Mongrel::Const::PATH_INFO]
|
|
|
|
page_cached = request.params[Mongrel::Const::PATH_INFO] + ".html"
|
2006-02-28 02:04:41 -05:00
|
|
|
|
|
|
|
if @files.can_serve(path_info)
|
|
|
|
# File exists as-is so serve it up
|
|
|
|
@files.process(request,response)
|
|
|
|
elsif @files.can_serve(page_cached)
|
|
|
|
# possible cached page, serve it up
|
2006-03-04 12:55:39 -05:00
|
|
|
request.params[Mongrel::Const::PATH_INFO] = page_cached
|
2006-02-28 02:04:41 -05:00
|
|
|
@files.process(request,response)
|
|
|
|
else
|
|
|
|
cgi = Mongrel::CGIWrapper.new(request, response)
|
2006-03-04 12:55:39 -05:00
|
|
|
cgi.handler = self
|
2006-03-04 13:10:57 -05:00
|
|
|
|
2006-02-28 02:04:41 -05:00
|
|
|
begin
|
|
|
|
@guard.synchronize do
|
|
|
|
# Rails is not thread safe so must be run entirely within synchronize
|
|
|
|
Dispatcher.dispatch(cgi, ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS, response.body)
|
|
|
|
end
|
|
|
|
|
|
|
|
# This finalizes the output using the proper HttpResponse way
|
|
|
|
cgi.out {""}
|
2006-03-01 23:26:08 -05:00
|
|
|
rescue Errno::EPIPE
|
|
|
|
# ignored
|
2006-02-28 02:04:41 -05:00
|
|
|
rescue Object => rails_error
|
2006-03-01 23:26:08 -05:00
|
|
|
STDERR.puts "Error calling Dispatcher.dispatch #{rails_error.inspect}"
|
2006-02-28 02:04:41 -05:00
|
|
|
STDERR.puts rails_error.backtrace.join("\n")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|