Sha256: cfa751a98fa58188acdf2a8b7a67729c5a8ae84de492f5b57c6539e7ab294e3c

Contents?: true

Size: 1.63 KB

Versions: 2

Compression:

Stored size: 1.63 KB

Contents

require 'cuba'
require 'rack'
require 'hyde'

class Hyde
  Server = Cuba.dup

  module Server; end

  module Server::PageHelpers
    def not_found
      show_status nil
      res.status = 404
      res.write "<h1>File Not Found</h1><p>The path <code>#{env['PATH_INFO']}</code> was not found." + " "*1024
    end

    def options
      @options ||= Hash.new
    end

    def show_status(page)
      return if options[:quiet]
      path = env['PATH_INFO']
      return  if path == '/favicon.ico'

      status = page ? "\033[0;32m[ OK ]" : "\033[0;31m[404 ]"
      verb = get ? 'GET ' : (post ? 'POST' : '')
      puts "%s\033[0;m %s %s" % [ status, verb, env['PATH_INFO'] ]
      puts "       src: #{page.filepath} (#{page.tilt_engine_name})"  if page && page.tilt?
    end
  end

  module Hyde::Server
    Ron.send :include, PageHelpers

    define do
      on default do
        begin
          page = Hyde::Page[env['PATH_INFO']]  or break not_found
          type = page.mime_type
          res['Content-Type'] = type  if type
          res.write page.to_html
          show_status page
        rescue => e
          res['Content-Type'] = 'text/html'
          res.write "<h1>#{e.class}: #{e.message}</h1><ul>#{e.backtrace.map{|l|"<li>#{l}</li>"}.join('')}</ul>"
        end
      end
    end
  end
end

module Hyde::Server
  # :Host, :Port
  def self.run!(options={})
    @options = options
    handler = rack_handler  or return false
    handler.run self, options
  end

  def self.rack_handler
    %w(thin mongrel webrick).each do |svr|
      begin
        return Rack::Handler.get(svr)
      rescue LoadError
      rescue NameError
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
hydeweb-0.1.9 lib/hyde/server.rb
hydeweb-0.1.8 lib/hyde/server.rb