Sha256: 0be2d62018dd4238273397763b47c3b65e54888802ed28a791a5f71486546de0

Contents?: true

Size: 1.35 KB

Versions: 1

Compression:

Stored size: 1.35 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 "404"
    end

    def show_status(page)
      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.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
          res['Content-Type'] = page.mime_type
          res.write page.to_html
          show_status page
        rescue => e
          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={})
    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

1 entries across 1 versions & 1 rubygems

Version Path
hydeweb-0.1.1 lib/hyde/server.rb