class Bolt::Serve

Attributes

pages[RW]

Public Class Methods

new() click to toggle source
Calls superclass method Bolt::Base::new
# File lib/bolt/serve.rb, line 53
def initialize
  super
  @pages = DRbHash.new
  
  DRb.start_service(nil, self)
  $drb_uri = DRb.uri
  
  $serve = true
  
  @server = HTTPServer.new(:host => $config.serve_host, :port => $config.serve_port)
  
  @errors_base = File.expand_path(File.dirname(File.dirname(__FILE__))) + '/bolt/serve_errors/'
  trap("INT") { exit! }            
end

Public Instance Methods

run() click to toggle source
# File lib/bolt/serve.rb, line 68
def run
  # This has to be loaded after the DRb server is started above
  load 'bolt/serve_page.rb'
  
  begin
    while(request = @server.listen)
      # Bit of magic here (eek)
      # Basically the parent class Build loads each page in the pages directory
      # for whatever project we're in, however the bolt/serve_page require up above
      # overrides the standard page method used in these pages to instead send us
      # the block used to generate the page (via drb), so basically @pages is full of
      # url => block references.
      load_pages        
      parse_config
      
      page_name = request['GET'].gsub(/\.html/,'')[1..-1]
      page_name = "index" if page_name == ""
      page = @pages[page_name]
      
      # Handle subdirectories
      page = @pages[page_name + "index"] if page.nil?
      page = @pages[page_name + "/index"] if page.nil?
      
      if(!page.nil?)
        # A tad hacky, otherwise @current_page isn't set properly and all hell breaks loose
        body = PageBinding.new(page_name).instance_eval(&page)
        @server.reply(body)
      elsif(File.exists?(d($config.resources) + request['GET']))
        f = File.new(d($config.resources) + request['GET'])            
        @server.reply(f.to_s, 200, 'Content-Type' => f.content_type)
      else
        raise NotFound404            
      end
    end
  rescue NotFound404        
    @server.reply(File.new(@errors_base + '404.html').to_s, 404)
  rescue Exception => e
    puts "Error: #{e}"
    puts e.backtrace
    @server.reply(File.new(@errors_base + '500.html').to_s, 500)
    retry
  end
  
  DRb.thread.join
end