Sha256: b712e774fc85097f78347b0adc520ffdd59c8ff5f28451e7610c769da8849fc8

Contents?: true

Size: 1 KB

Versions: 3

Compression:

Stored size: 1 KB

Contents

require 'rack'

module Miso
  #Rack application to handle static site.
  class StaticSite
    @@NOT_FOUND_PAGE = '404.html'
    
    #Receives static site base path.
    def initialize(static_site_path)
      @root = static_site_path
      @static_page_server = Rack::Directory.new(@root)
    end
    
    #Execute request.
    def call(env)
      path = Rack::Utils.unescape(env['PATH_INFO'])
      not_found = false
      
      #if / then route to index.html
      if path == "/"
        # Return the index
        env['PATH_INFO']="/index.html"
        path = path+'index.html'
      end
      
      #if page does not exists then route to 404.html
      if !::File.exists?(@root+path) and ::File.exists?(@root+'/'+@@NOT_FOUND_PAGE)
        env['PATH_INFO']=@@NOT_FOUND_PAGE
        not_found = true
      end
      
      #call Directory to resolve
      code, headers, body = @static_page_server.call(env)
      
      #override to 404 if page not found
      [not_found ? 404 : code, headers, body]
    end
  end  
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
misosoup-0.1.2 lib/miso/static.rb
misosoup-0.1.1 lib/miso/static.rb
misosoup-0.1.0 lib/miso/static.rb