From a81c8a0ed7a45add1a8814a42e5d209ba674a119 Mon Sep 17 00:00:00 2001 From: _why Date: Thu, 18 May 2006 20:16:34 +0000 Subject: [PATCH] * lib/camping/fastcgi.rb: new FastCGI app mounter for running several apps in a single process. --- CHANGELOG | 3 ++- README | 2 +- lib/camping/fastcgi.rb | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 lib/camping/fastcgi.rb diff --git a/CHANGELOG b/CHANGELOG index 701d335..4ced7c6 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,8 +1,9 @@ = 1.4.2 -=== 10th May, 2006 +=== 18th May, 2006 * Efficient file uploads for multipart/form-data POSTs. * Camping tool now uses Mongrel, if available. If not, sticks with WEBrick. +* Multiple apps can be loaded with the camping tool, each mounted according to their file name. = 1.4.1 === 3rd May, 2006 diff --git a/README b/README index 5aedf58..c6e78f8 100644 --- a/README +++ b/README @@ -100,7 +100,7 @@ If you run them from the commandline, you'll probably just see a pile of HTML. Camping comes with an tool for launching apps from the commandline: * Run: camping blog.rb -* Visit http://localhost:3301/ to use the app. +* Visit http://localhost:3301/blog/ to use the app. == Debugging Camping Apps diff --git a/lib/camping/fastcgi.rb b/lib/camping/fastcgi.rb new file mode 100644 index 0000000..0d8e043 --- /dev/null +++ b/lib/camping/fastcgi.rb @@ -0,0 +1,36 @@ +require 'fcgi' + +module Camping +class FastCGI + def initialize + @mounts = {} + end + def mount(dir, app) + dir.gsub!(/\/{2,}/, '/') + dir.gsub!(/\/+$/, '') + @mounts[dir] = app + end + def match(path, mount) + m = path.match(/^#{Regexp::quote mount}(\/|$)/) + if m: m.end(0) + else -1 + end + end + def start + FCGI.each do |req| + # req.out << app.run(req.in, req.env) + path = req.env['SCRIPT_NAME'] + req.env['PATH_INFO'] + dir, app = @mounts.max { |a,b| match(path, a[0]) <=> match(path, b[0]) } + req.env['SCRIPT_NAME'] = dir + req.env['PATH_INFO'] = path.gsub(/^#{dir}/, '') + req.out << app.run(req.in, req.env) + req.finish + end + end + def self.start(app) + cf = Camping::FastCGI.new + cf.mount("/", app) + cf.start + end +end +end