Skip to content

Commit

Permalink
* bin/camping: idea from adamb, mounting several apps.
Browse files Browse the repository at this point in the history
  • Loading branch information
_why committed May 16, 2006
1 parent 659180d commit 31bdfc4
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 125 deletions.
133 changes: 113 additions & 20 deletions bin/camping
Original file line number Diff line number Diff line change
Expand Up @@ -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 <<USAGE; exit) if ARGV.length == 0
#{File.basename($0)}, the microframework ON-button for ruby #{RUBY_VERSION} (#{RUBY_RELEASE_DATE}) [#{RUBY_PLATFORM}]
Usage: #{File.basename($0)} your.camping.rb [your.camping.db] [host] [port]
(host defaults to 0.0.0.0; port defaults to 3301)
USAGE

script, db = ARGV[0..-1]
host = ARGV[1] || '0.0.0.0'
port = (ARGV[2] || 3301).to_i

unless db
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 )
host = '0.0.0.0'
port = 3301

db = nil
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 )
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)") do |h|
host = h
end

opts.on("-p", "--port NUM", "Port for web server (defaults to #{port})") do |p|
port = p
end

opts.on("-d", "--database FILE", "Database file (defaults to #{db})") do |d|
db = d
end

opts.separator ""
opts.separator "Common options:"

# No argument, shows at tail. This will print an options summary.
# Try it and see!
opts.on_tail("-h", "--help", "Show this message") do
puts opts
exit
end

# Another typical switch to print the version.
opts.on_tail("--version", "Show version") do
class << Gem; attr_accessor :loaded_specs; end
puts Gem.loaded_specs['camping'].version
exit
end
end

opts.parse! ARGV
if ARGV.length < 1
puts opts
exit
end

Camping::Models::Base.establish_connection :adapter => '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

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
105 changes: 0 additions & 105 deletions examples/serve

This file was deleted.

0 comments on commit 31bdfc4

Please sign in to comment.