Sha256: ac877e78e215a335233f1b61af1d25fb4bcf89f9069ba8cc190932d4905e68a2

Contents?: true

Size: 1.59 KB

Versions: 7

Compression:

Stored size: 1.59 KB

Contents

require 'sinatra/base'
require 'rack'

module Lurker
  class Server
    # fixed rack_contrib implementation
    class TryStatic

      def initialize(app, options)
        @app = app
        @try = ['', *options.delete(:try)]
        @static = ::Rack::Static.new(
          Proc.new { [404, {}, []] }, # HERE proc, not lambda
          options)
      end

      def call(env)
        orig_path = env['PATH_INFO']
        found = nil
        @try.each do |path|
          resp = @static.call(env.merge!({'PATH_INFO' => orig_path + path}))
          break if 404 != resp[0] && found = resp
        end
        found or @app.call(env.merge!('PATH_INFO' => orig_path))
      end
    end

    def self.to_rack(options = {})
      default_path = options[:path] || Lurker::DEFAULT_SERVICE_PATH

      cls = Class.new(Sinatra::Base) do

        if !Rails.env.development? && (username, password = options.values_at(:username, :password)).all?(&:present?)
          use ::Rack::Auth::Basic, "Protected Area" do |u, p|
            username == u && password == p
          end
        end

        use ::Rack::Deflater

        use TryStatic,
         :root => "#{::Rails.root}/#{default_path}",  # static files root dir
         :urls => %w[/],     # match all requests
         :header_rules => [
           [%w(css js), {'Cache-Control' => 'public, max-age=31536000'}],
           [:fonts, {'Access-Control-Allow-Origin' => '*'}]
         ],
         :try => ['.html', 'index.html', '/index.html'] # try these postfixes sequentially

      end
      Lurker.const_set("Rack_#{rand 10}_#{Time.now.to_i}", cls)
      cls
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
lurker-0.6.1 lib/lurker/server.rb
lurker-0.5.7 lib/lurker/server.rb
lurker-0.6.0 lib/lurker/server.rb
lurker-0.5.6 lib/lurker/server.rb
lurker-0.5.5 lib/lurker/server.rb
lurker-0.5.4 lib/lurker/server.rb
lurker-0.5.3 lib/lurker/server.rb