#!/usr/bin/env ruby # this line prevents other db adapters from being loaded (oci8 was # causing some pain.) RAILS_CONNECTION_ADAPTERS = %w[sqlite] require 'stringio' require 'webrick/httpserver' require 'camping/webrick' (puts < 'sqlite3', :database => db class WEBrick::CampingHandler attr_accessor :klass, :mtime end # Load the script, locate the module def camp_load(server, script, klass = nil) Object.instance_eval { remove_const klass.name } if klass mtime = File.mtime(script) load script klass = Object.const_get(Object.constants.grep(/^#{File.basename(script)[/^(\w+)/,1]}$/i)[0]) rescue nil klass ||= Camping klass.create if klass.respond_to? :create brick = WEBrick::CampingHandler.new(server, klass) brick.mtime = mtime brick end # Mount the root s = WEBrick::HTTPServer.new(:BindAddress => '0.0.0.0', :Port => 3301) brick = camp_load( s, script ) s.mount_proc("/") do |req, resp| newtime = File.mtime( script ) if newtime > brick.mtime brick = camp_load( s, script, brick.klass ) end brick.do_GET(req, resp) nil end # Server up trap(:INT) do s.shutdown end s.start