Sha256: 6b9eecd77de11aea0b848252626579a05dcad6e9df8eea11560bc98bfeebc634

Contents?: true

Size: 1.9 KB

Versions: 1

Compression:

Stored size: 1.9 KB

Contents

module StaticMatic
  class Server < Mongrel::HttpHandler
    @@file_only_methods = ["GET","HEAD"]
 
    def initialize(base_dir)
      @files = Mongrel::DirHandler.new("#{base_dir}/site",false)
      @staticmatic = StaticMatic::Base.new(base_dir)
    end
  
    def process(request, response)
      path_info = request.params[Mongrel::Const::PATH_INFO]
      get_or_head = @@file_only_methods.include? request.params[Mongrel::Const::REQUEST_METHOD]
      
      file_to_serve = path_info
      
      file_name, file_ext = path_info.split('.')
      
      if file_name == "/"
        file_name = "index"
        file_ext = "html"
      end
      
      # remove stylesheets/ directory if applicable
      file_name.gsub!("/stylesheets/", "")
      
      if file_ext && file_ext.match(/html|css/)
        response.start(200) do |head, out|
          output = ""
          
          if @staticmatic.template_exists?(file_name)
            if file_ext == "html"
              output = @staticmatic.generate_html_with_layout("#{file_name}")
            elsif file_ext == "css"
              output = @staticmatic.generate_css("#{file_name}")
            end
          else
            output = "File not Found"
          end
          out.write output
        end
      else
        # try to serve static file from site dir
        if @files.can_serve(path_info)
          @files.process(request,response)
        end
      end
    end
    
    class << self
      # Starts the StaticMatic preview server
      #
      # StaticMatic.start('/path/to/site/')
      #
      def start(base_dir, port = 3000)
        config = Mongrel::Configurator.new :host => "127.0.0.1" do
          puts "Running Preview of #{base_dir} on 127.0.0.1:#{port}"
          listener :port => port do
            uri "/", :handler => Server.new(base_dir)
          end
          trap("INT") { stop }
          run
        end
        config.join
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
staticmatic-0.2.0 lib/staticmatic/server.rb