Class: Miso::StaticSite

Inherits:
Object
  • Object
show all
Defined in:
lib/miso/static.rb

Overview

Rack application to handle static site.

Constant Summary collapse

@@NOT_FOUND_PAGE =
'404.html'

Instance Method Summary collapse

Constructor Details

#initialize(static_site_path) ⇒ StaticSite

Receives static site base path.



9
10
11
12
# File 'lib/miso/static.rb', line 9

def initialize(static_site_path)
  @root = static_site_path
  @static_page_server = Rack::Directory.new(@root)
end

Instance Method Details

#call(env) ⇒ Object

Execute request.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/miso/static.rb', line 15

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