Class: Fir::Static

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

Constant Summary collapse

FORBIDDEN_DIRS =
['.svn', '.git']

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Static

Returns a new instance of Static.



5
6
7
8
# File 'lib/fir/static.rb', line 5

def initialize(app)
  @app = app
  @file_server = ::Rack::File.new(File.join(FIR_ROOT, 'public'))
end

Instance Method Details

#call(env) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/fir/static.rb', line 10

def call(env)
  path   = Fir.sanitize_page(::Rack::Utils.unescape(env['PATH_INFO']).chomp('/'))
  method = env['REQUEST_METHOD']
  if method == 'GET'
    if static_exists?(path, true)
      return @file_server.call(env)
    elsif Fir.config.perform_caching
      cached_path = File.join('cache', path)
      # Change PATH_INFO because @file_server uses that variable to find the file
      if static_exists?(cached_path, false) # Is it a directory?
        index_path = File.join(cached_path, 'index.html')
        if static_exists?(index_path, true)
          env['PATH_INFO'] = index_path              
          return @file_server.call(env)
        else # If the directory index isn't cached, pass on the request so PageRenderer can look for an index file.
          return @app.call(env)
        end
      elsif static_exists?(cached_path + '.html', true) # Is it a file?
        env['PATH_INFO'] = cached_path + '.html'
        return @file_server.call(env)
      end
    end
  end
  return @app.call(env)
end