Skip to content

Commit

Permalink
* Rakefile: auto-revision the packages.
Browse files Browse the repository at this point in the history
 * lib/camping/fastcgi.rb: send all unhandled requests to the main Camping module.
 * bin/camping: support the ~/.campingrc if people want to use other databases.
  • Loading branch information
_why committed Jul 8, 2006
1 parent b125431 commit e56c05c
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 28 deletions.
5 changes: 3 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ require 'fileutils'
include FileUtils

NAME = "camping"
VERS = "1.4.120"
REV = File.read(".svn/entries")[/committed-rev="(\d+)"/, 1] rescue nil
VERS = "1.4" + (REV ? ".#{REV}" : "")
CLEAN.include ['**/.*.sw?', '*.gem', '.config']
RDOC_OPTS = ['--quiet', '--title', "Camping, the Documentation",
"--template", "extras/flipbook_rdoc.rb",
Expand Down Expand Up @@ -50,7 +51,7 @@ spec =
s.platform = Gem::Platform::RUBY
s.has_rdoc = true
s.extra_rdoc_files = ["README", "CHANGELOG", "COPYING"]
s.rdoc_options += RDOC_OPTS + ['--exclude', '^(examples|extras)\/', '--exclude', 'lib/camping(-orig)?.rb']
s.rdoc_options += RDOC_OPTS + ['--exclude', '^(examples|extras)\/', '--exclude', 'lib/camping.rb']
s.summary = "minature rails for stay-at-home moms"
s.description = s.summary
s.author = "why the lucky stiff"
Expand Down
69 changes: 44 additions & 25 deletions bin/camping
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,46 @@ RAILS_CONNECTION_ADAPTERS.replace %w[sqlite]

require 'delegate'
require 'optparse'
require 'ostruct'
require 'stringio'
require 'rubygems'
require 'camping'
require 'camping/session'
require 'camping/reloader'
require 'yaml'

server, host, port, db, log = :mongrel, '0.0.0.0', 3301
conf = OpenStruct.new(:host => '0.0.0.0', :port => 3301)

homes = []
homes << File.join( ENV['HOME'], '.camping.db' ) if ENV['HOME']
homes << File.join( ENV['APPDATA'], 'Camping.db' ) if ENV['APPDATA']
homes.each do |db|
break if File.exists?( db )
homes.each do |conf.db|
break if File.exists?( conf.db )
rc = File.expand_path( '../.campingrc', conf.db )
if File.exists? rc
YAML.load_file(rc).each do |k,v|
v = v.to_sym if k == 'server'
conf.send("#{k}=", v)
end
end
end

unless conf.db
puts "!! No home directory found. Please specify a database file, see --help."; exit
end

opts = OptionParser.new do |opts|
opts.banner = "Usage: camping app1.rb, app2.rb..."
opts.define_head "#{File.basename($0)}, the microframework ON-button for ruby #{RUBY_VERSION} (#{RUBY_RELEASE_DATE}) [#{RUBY_PLATFORM}]"
opts.separator ""
opts.separator "Specific options:"

opts.on("-h", "--host HOSTNAME", "Host for web server to bind to (default is all IPs)") { |host| }
opts.on("-p", "--port NUM", "Port for web server (defaults to #{port})") { |port| }
opts.on("-d", "--database FILE", "Database file (defaults to #{db})") { |db| }
opts.on("-l", "--log FILE", "Start a database log ('-' for STDOUT)") { |log| }
opts.on("-C", "--console", "Run in console mode with IRB") { server = :console }
opts.on("-h", "--host HOSTNAME", "Host for web server to bind to (default is all IPs)") { |conf.host| }
opts.on("-p", "--port NUM", "Port for web server (defaults to #{conf.port})") { |conf.port| }
opts.on("-d", "--database FILE", "Database file (defaults to #{conf.db})") { |conf.db| }
opts.on("-l", "--log FILE", "Start a database log ('-' for STDOUT)") { |conf.log| }
opts.on("-C", "--console", "Run in console mode with IRB") { conf.server = :console }
opts.on("-s", "--server", "Server to force (mongrel, webrick, console)") { |s| conf.server = s.to_sym }

opts.separator ""
opts.separator "Common options:"
Expand All @@ -61,9 +74,13 @@ if ARGV.length < 1
exit
end

Camping::Models::Base.establish_connection :adapter => 'sqlite3', :database => db
if log
Camping::Models::Base.logger = Logger.new(log == "-" ? STDOUT : log)
unless conf.database
conf.database = {:adapter => 'sqlite3', :database => conf.db}
end
Camping::Models::Base.establish_connection conf.database

if conf.log
Camping::Models::Base.logger = Logger.new(conf.log == "-" ? STDOUT : conf.log)
end
begin
Camping::Models::Session.create_schema
Expand All @@ -73,14 +90,15 @@ rescue MissingSourceFile
exit
end

begin
require 'sqlite3_api'
rescue LoadError
puts "!! Your SQLite3 adapter isn't a compiled extension."
abort "!! Please check out http://code.whytheluckystiff.net/camping/wiki/BeAlertWhenOnSqlite3 for tips."
if conf.database[:adapter] == 'sqlite3'
begin
require 'sqlite3_api'
rescue LoadError
puts "!! Your SQLite3 adapter isn't a compiled extension."
abort "!! Please check out http://code.whytheluckystiff.net/camping/wiki/BeAlertWhenOnSqlite3 for tips."
end
end


apps = ARGV.inject([]) do |apps, script|
if File.directory? script
apps.push(*Dir[File.join(script, '*.rb')])
Expand Down Expand Up @@ -130,16 +148,17 @@ def apps.index_page
end

# Check that mongrel exists
if server == :mongrel
unless conf.server
begin
require 'mongrel'
require 'mongrel/camping'
conf.server = :mongrel
rescue LoadError
server = :webrick
conf.server = :webrick
end
end

case server
case conf.server
when :mongrel
class IndexHandler < Mongrel::HttpHandler
def initialize(apps)
Expand All @@ -163,8 +182,8 @@ when :mongrel
end
end
begin
config = Mongrel::Configurator.new :host => host do
listener :port => port do
config = Mongrel::Configurator.new :host => conf.host do
listener :port => conf.port do
if apps.length > 1
apps.each do |app|
uri "/#{app.mount}", :handler => Mongrel::Camping::CampingHandler.new(app)
Expand All @@ -180,17 +199,17 @@ when :mongrel
end
end

puts "** Camping running on #{host}:#{port}."
puts "** Camping running on #{conf.host}:#{conf.port}."
config.join
rescue Errno::EADDRINUSE
puts "** ERROR : address #{host}:#{port} already in use."
puts "** ERROR : address #{conf.host}:#{conf.port} already in use."
end
when :webrick
require 'webrick/httpserver'
require 'camping/webrick'

# Mount the root
s = WEBrick::HTTPServer.new(:BindAddress => host, :Port => port)
s = WEBrick::HTTPServer.new(:BindAddress => conf.host, :Port => conf.port)
if apps.length > 1
apps.each do |app|
s.mount "/#{app.mount}", WEBrick::CampingHandler, app
Expand Down
4 changes: 3 additions & 1 deletion lib/camping/fastcgi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,11 @@ def mount(dir, app)
# Starts the FastCGI main loop.
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]) }
unless dir and app
dir, app = '/', Camping
end
req.env['SCRIPT_NAME'] = dir
req.env['PATH_INFO'] = path.gsub(/^#{dir}/, '')
req.out << app.run(req.in, req.env)
Expand Down

0 comments on commit e56c05c

Please sign in to comment.