From 31bdfc49dffafc46b1f83460303731edeeef378e Mon Sep 17 00:00:00 2001 From: _why Date: Tue, 16 May 2006 20:49:13 +0000 Subject: [PATCH] * bin/camping: idea from adamb, mounting several apps. --- bin/camping | 133 +++++++++++++++++++++++++++++++++++++++++-------- examples/serve | 105 -------------------------------------- 2 files changed, 113 insertions(+), 125 deletions(-) delete mode 100755 examples/serve diff --git a/bin/camping b/bin/camping index 1f45473..2d85910 100755 --- a/bin/camping +++ b/bin/camping @@ -2,39 +2,79 @@ # this line prevents other db adapters from being loaded (oci8 was # causing some pain.) -(RAILS_CONNECTION_ADAPTERS ||= []).replace %w[sqlite] +unless Object.const_defined? :RAILS_CONNECTION_ADAPTERS + RAILS_CONNECTION_ADAPTERS = [] +end +RAILS_CONNECTION_ADAPTERS.replace %w[sqlite] require 'delegate' +require 'optparse' require 'stringio' require 'rubygems' require 'camping' -(puts < 'sqlite3', :database => db class CampingReloader - attr_accessor :klass, :mtime + attr_accessor :klass, :mtime, :mount def initialize(script) @script = script + @mount = File.basename(script, '.rb') load_app end @@ -72,12 +112,62 @@ class CampingReloader end end +apps = ARGV.map { |script| CampingReloader.new(script) } +def apps.index_page + welcome = "You are Camping" + apps = self + b = Markaby::Builder.new({}, {}) + b = b.instance_eval do + html do + head do + title welcome + style <<-END, :type => 'text/css' + body { + font-family: verdana, arial, sans-serif; + padding: 10px 40px; + margin: 0; + } + h1, h2, h3, h4, h5, h6 { + font-family: utopia, georgia, serif; + } + END + end + body do + h1 welcome + p %{Good day. These are the Camping apps you've mounted.} + ul do + apps.each do |app| + li do + h3(:style => "display: inline") { a app.klass.name, :href => "/#{app.mount}" } + small { text " / " ; a "View Source", :href => "/code/#{app.mount}" } + end + end + end + end + end + end + b.to_s +end + begin require 'mongrel' require 'mongrel/camping' + class IndexHandler < Mongrel::HttpHandler + def initialize(apps) + @apps = apps + end + def process(req, res) + res.start(200) do |head, out| + out << @apps.index_page + end + end + end config = Mongrel::Configurator.new :host => host do listener :port => port do - uri "/", :handler => Mongrel::Camping::CampingHandler.new(CampingReloader.new(script)) + apps.each do |app| + uri "/#{app.mount}", :handler => Mongrel::Camping::CampingHandler.new(app) + end + uri "/", :handler => IndexHandler.new(apps) uri "/favicon.ico", :handler => Mongrel::Error404Handler.new("") trap("INT") { stop } run @@ -90,7 +180,10 @@ rescue LoadError # Mount the root s = WEBrick::HTTPServer.new(:BindAddress => host, :Port => port) - s.mount "/", WEBrick::CampingHandler, CampingReloader.new(script) + apps.each do |app| + s.mount "/#{app.mount}", WEBrick::CampingHandler, app + end + s.mount_proc("/") { |req, resp| resp.body = apps.index_page } # Server up trap(:INT) do diff --git a/examples/serve b/examples/serve deleted file mode 100755 index cce7af5..0000000 --- a/examples/serve +++ /dev/null @@ -1,105 +0,0 @@ -#!/usr/bin/env ruby -RAILS_CONNECTION_ADAPTERS = %w[sqlite] - -# -# Serves all examples, mounted into Webrick. -# -$:.unshift File.expand_path(File.dirname(__FILE__) + "/../lib") -require 'stringio' -require 'webrick/httpserver' -require 'camping/webrick' - -# All applications share a single database -Camping::Models::Base.establish_connection :adapter => 'sqlite3', :database => 'serve.db' -Camping::Models::Base.logger = Logger.new('camping.log') - -# Find the working applications -def find_apps - apps = - Dir['*'].select do |d| - if File.directory? "#{d}" - begin - load "#{d}/#{d}.rb" - true - rescue Exception => e - puts "Camping app `#{d}' will not load: #{e.class} #{e.message}" - end - end - end - apps.map! do |app| - begin - klass = Object.const_get(Object.constants.grep(/^#{app}$/i)[0]) - klass.create if klass.respond_to? :create - [app, klass] - rescue Exception => e - puts "Camping app `#{app}' will not load: #{e.class} #{e.message}" - end - end - apps.compact -end - -def index_page req, resp, apps - welcome = "Welcome to the Camping Example Server" - b = Markaby::Builder.new({}, {}) - b = b.instance_eval do - html do - head do - title welcome - style <<-END, :type => 'text/css' - body { - font-family: verdana, arial, sans-serif; - padding: 10px 40px; - margin: 0; - } - h1, h2, h3, h4, h5, h6 { - font-family: utopia, georgia, serif; - } - END - end - body do - h1 welcome - p %{ - Good day. These are the Camping sample applications. The code - for each of these applications is available in its own folder - under examples. A link to the application's source code is - also given next to each one. - } - p %{Well, click on the application's name to give a try.} - ul do - apps.each do |app, klass| - li do - h3(:style => "display: inline") { a app, :href => "/#{app}" } - small { text " / " ; a "View Source", :href => "/code/#{app}" } - end - end - end - end - end - end - resp.body = b.to_s -end - -apps = find_apps -s = WEBrick::HTTPServer.new(:BindAddress => '0.0.0.0', :Port => 3301) - -# Root mount displays applications mounted -s.mount_proc("/") do |req, resp| - index_page req, resp, apps -end - -# Mount which handles each application -apps.each do |app, klass| - # Mount for view source - s.mount_proc("/code/#{app}") do |req, resp| - resp.header['Content-Type'] = 'text/plain' - resp.body = File.read("#{app}/#{app}.rb") - end - - s.mount("/#{app}", WEBrick::CampingHandler, klass) -end - -# Server up -trap(:INT) do - s.shutdown -end -s.start