diff options
-rwxr-xr-x | examples/camping/blog.rb | 28 | ||||
-rw-r--r-- | examples/camping/tepee.rb | 26 | ||||
-rw-r--r-- | lib/mongrel/camping.rb | 62 |
3 files changed, 69 insertions, 47 deletions
diff --git a/examples/camping/blog.rb b/examples/camping/blog.rb index ba0e041..14e4c4f 100755 --- a/examples/camping/blog.rb +++ b/examples/camping/blog.rb @@ -267,34 +267,14 @@ def Blog.create end if __FILE__ == $0 - require 'thread' - - class CampingHandler < Mongrel::HttpHandler - def initialize(klass) - @klass = klass - end - def process(request, response) - req = StringIO.new(request.body) - controller = @klass.run(req, request.params) - response.start(controller.status) do |head,out| - controller.headers.each do |k, v| - [*v].each do |vi| - head[k] = vi - end - end - out << controller.body - end - end - end + require 'mongrel/camping' Blog::Models::Base.establish_connection :adapter => 'sqlite3', :database => 'blog.db' Blog::Models::Base.logger = Logger.new('camping.log') Blog::Models::Base.threaded_connections=false Blog.create - h = Mongrel::HttpServer.new("0.0.0.0", "3000") - puts "** Blog example is running at http://localhost:3000/blog" - h.register("/blog", CampingHandler.new(Blog)) - h.register("/favicon.ico", Mongrel::Error404Handler.new("")) - h.run.join + server = Mongrel::Camping::start("0.0.0.0",3002,"/blog",Blog) + puts "** Blog example is running at http://localhost:3002/blog" + server.join end diff --git a/examples/camping/tepee.rb b/examples/camping/tepee.rb index b49ca7b..6f3ef21 100644 --- a/examples/camping/tepee.rb +++ b/examples/camping/tepee.rb @@ -135,34 +135,14 @@ def Tepee.create end if __FILE__ == $0 - require 'thread' - - class CampingHandler < Mongrel::HttpHandler - def initialize(klass) - @klass = klass - end - def process(request, response) - req = StringIO.new(request.body) - controller = @klass.run(req, request.params) - response.start(controller.status) do |head,out| - controller.headers.each do |k, v| - [*v].each do |vi| - head[k] = vi - end - end - out << controller.body - end - end - end + require 'mongrel/camping' Tepee::Models::Base.establish_connection :adapter => 'sqlite3', :database => 'tepee.db' Tepee::Models::Base.logger = Logger.new('camping.log') Tepee::Models::Base.threaded_connections=false Tepee.create - h = Mongrel::HttpServer.new("0.0.0.0", "3000") + server = Mongrel::Camping::start("0.0.0.0",3001,"/tepee",Tepee) puts "** Tepee example is running at http://localhost:3000/tepee" - h.register("/tepee", CampingHandler.new(Tepee)) - h.register("/favicon.ico", Mongrel::Error404Handler.new("")) - h.run.join + server.join end diff --git a/lib/mongrel/camping.rb b/lib/mongrel/camping.rb new file mode 100644 index 0000000..3a19960 --- /dev/null +++ b/lib/mongrel/camping.rb @@ -0,0 +1,62 @@ +require 'mongrel' + + +module Mongrel + # Support for the Camping micro framework at http://camping.rubyforge.org + # This implements the unusually long Postamble that Camping usually + # needs and shrinks it down to just a single line or two. + # + # Your Postamble would now be: + # + # Mongrel::Camping::start("0.0.0.0",3001,"/tepee",Tepee).join + # + # If you wish to get fancier than this then you can use the + # Camping::CampingHandler directly instead and do your own + # wiring: + # + # h = Mongrel::HttpServer.new(server, port) + # h.register(uri, CampingHandler.new(Tepee)) + # h.register("/favicon.ico", Mongrel::Error404Handler.new("")) + # + # I add the /favicon.ico since camping apps typically don't + # have them and it's just annoying anyway. + module Camping + + # This is a specialized handler for Camping applications + # that has them process the request and then translates + # the results into something the Mongrel::HttpResponse + # needs. + class CampingHandler < Mongrel::HttpHandler + def initialize(klass) + @klass = klass + end + + def process(request, response) + req = StringIO.new(request.body) + controller = @klass.run(req, request.params) + response.start(controller.status) do |head,out| + controller.headers.each do |k, v| + [*v].each do |vi| + head[k] = vi + end + end + out << controller.body + end + end + end + + # This is a convenience method that wires up a CampingHandler + # for your application on a given port and uri. It's pretty + # much all you need for a camping application to work right. + # + # It returns the server thread which you should either + # join or somehow manage. The thread is running when + # returned. + def Camping.start(server, port, uri, klass) + h = Mongrel::HttpServer.new(server, port) + h.register(uri, CampingHandler.new(klass)) + h.register("/favicon.ico", Mongrel::Error404Handler.new("")) + return h.run + end + end +end |