about summary refs log tree commit homepage
path: root/lib/mongrel/camping.rb
diff options
context:
space:
mode:
authorzedshaw <zedshaw@19e92222-5c0b-0410-8929-a290d50e31e9>2006-03-07 01:44:24 +0000
committerzedshaw <zedshaw@19e92222-5c0b-0410-8929-a290d50e31e9>2006-03-07 01:44:24 +0000
commit9a1895539db72bff6d75b16399e00fd34940f9ad (patch)
treef48130191229cbc71a16a48617f65813b67e8e0c /lib/mongrel/camping.rb
parent423b87625f8f549ac83b0182fc875c5cabc14fa1 (diff)
downloadunicorn-9a1895539db72bff6d75b16399e00fd34940f9ad.tar.gz
git-svn-id: svn+ssh://rubyforge.org/var/svn/mongrel/trunk@85 19e92222-5c0b-0410-8929-a290d50e31e9
Diffstat (limited to 'lib/mongrel/camping.rb')
-rw-r--r--lib/mongrel/camping.rb62
1 files changed, 62 insertions, 0 deletions
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