Skip to content

Commit

Permalink
* bin/camping: get view source working, ignore apps which throw load…
Browse files Browse the repository at this point in the history
…ing exceptions.
  • Loading branch information
_why committed May 16, 2006
1 parent 31bdfc4 commit 3302af5
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions bin/camping
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,16 @@ class CampingReloader
load @script
rescue Exception => e
puts "!! trouble loading #{title}: [#{e.class}] #{e.message}"
@klass = nil
return
end

@klass = Object.const_get(Object.constants.grep(/^#{title}$/i)[0]) rescue nil
unless @klass.const_defined? :C
puts "!! trouble loading #{title}: not a Camping app"
@klass = nil
return
end
@klass.create if @klass.respond_to? :create
@klass
end
Expand All @@ -110,6 +117,10 @@ class CampingReloader
Camping.run(*a)
end
end

def view_source
File.read(@script)
end
end

apps = ARGV.map { |script| CampingReloader.new(script) }
Expand Down Expand Up @@ -137,6 +148,7 @@ def apps.index_page
p %{Good day. These are the Camping apps you've mounted.}
ul do
apps.each do |app|
next unless app.klass
li do
h3(:style => "display: inline") { a app.klass.name, :href => "/#{app.mount}" }
small { text " / " ; a "View Source", :href => "/code/#{app.mount}" }
Expand All @@ -162,10 +174,22 @@ begin
end
end
end
class ViewSource < Mongrel::HttpHandler
def initialize(app)
@app = app
end
def process(req, res)
res.start(200) do |head, out|
head['Content-Type'] = 'text/plain'
out << @app.view_source
end
end
end
config = Mongrel::Configurator.new :host => host do
listener :port => port do
apps.each do |app|
uri "/#{app.mount}", :handler => Mongrel::Camping::CampingHandler.new(app)
uri "/code/#{app.mount}", :handler => ViewSource.new(app)
end
uri "/", :handler => IndexHandler.new(apps)
uri "/favicon.ico", :handler => Mongrel::Error404Handler.new("")
Expand All @@ -182,6 +206,10 @@ rescue LoadError
s = WEBrick::HTTPServer.new(:BindAddress => host, :Port => port)
apps.each do |app|
s.mount "/#{app.mount}", WEBrick::CampingHandler, app
s.mount_proc("/code/#{app.mount}") do |req, resp|
resp['Content-Type'] = 'text/plain'
resp.body = app.view_source
end
end
s.mount_proc("/") { |req, resp| resp.body = apps.index_page }

Expand Down

0 comments on commit 3302af5

Please sign in to comment.