Sha256: f96b35d4cf3d6b963aeb98f7a49938e48b916050204e422b476a6b6036970fd2

Contents?: true

Size: 1.83 KB

Versions: 3

Compression:

Stored size: 1.83 KB

Contents

module DomainSlice
  module Middleware
    class Static
      FILE_METHODS = %w(GET HEAD).freeze unless defined?(FILE_METHODS)
      
      def initialize(app)
        @app = app
        @file_servers = [
          ::Rack::File.new(domain_slice_public_path),
          ::Rack::File.new(rails_public_path)
        ]
      end
      
      def call(env)
        path        = env['PATH_INFO'].chomp('/')
        method      = env['REQUEST_METHOD']
        
        if FILE_METHODS.include?(method)
          if rack_file = file_exist?(path)
            return rack_file.call(env)
          else
            cached_path = directory_exist?(path) ? "#{path}/index" : path
            cached_path += ::ActionController::Base.page_cache_extension
            
            if rack_file = file_exist?(cached_path)
              env['PATH_INFO'] = cached_path
              return rack_file.call(env)
            end
          end
        end
        
        @app.call(env)
      end
      
      private
        def rails_public_path
          File.join(RAILS_ROOT, 'public')
        end
        
        def domain_slice_public_path
          File.join(RAILS_ROOT, 'domains', DomainSlice.domain, 'public')
        end
        
        # TODO [BG Mar 09] This might have to be optimized instead of a stupid check
        # one, check the other arrangement
        def file_exist?(path)
          @file_servers.find do |file_server|
            full_path = File.join(file_server.root, ::Rack::Utils.unescape(path))
            File.file?(full_path) && File.readable?(full_path)
          end
        end
        
        def directory_exist?(path)
          @file_servers.find do |file_server|
            full_path = File.join(file_server.root, ::Rack::Utils.unescape(path))
            File.directory?(full_path) && File.readable?(full_path)
          end
        end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
bradgessler-domain_slice-0.1.1 lib/domain_slice/middleware/static.rb
bradgessler-domain_slice-0.1.2 lib/domain_slice/middleware/static.rb
bradgessler-domain_slice-0.1.5 lib/domain_slice/middleware/static.rb