Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: b2139ff18c
Fetching contributors…

Cannot retrieve contributors at this time

file 53 lines (44 sloc) 1.113 kb
1 2 3 4 5 6 7 8 9 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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
# Override Rack::Static to allow a fallthrough for missing files
module Rack
  class Static

    def initialize(app, options={})
      @app = app
      @urls = options[:urls] || ["/favicon.ico"]
      @root = options[:root] || Dir.pwd
      @file_server = Rack::File.new(@root)
    end

    def call(env)
      path = env["PATH_INFO"]
      can_serve = @urls.any? { |url| path.index(url) == 0 }

      if can_serve && ::File.exist?("#{@root}/#{path}")
        @file_server.call(env)
      else
        @app.call(env)
      end
    end

  end
end

use Rack::Static, :urls => ["/"], :root => "lib"
use Rack::Static, :urls => ["/"], :root => "public"

current = Hash.new(0)

map "/favicon.ico" do
  run proc {
    [200, {"Content-Type" => "image/png"}, [""]]
  }
end

map "/reset" do
  run proc {
    current = Hash.new(0)
    [200, {"Content-Type" => "text/plain"}, ["success"]]
  }
end

map "/ajax/app" do
  run proc { |env|
    string = env["QUERY_STRING"]
    now = current[string] += 1
    [200,
      {"Content-Type" => "application/json"},
      [%{{"count": #{now}, "qs": #{string.inspect}}}]
    ]
  }
end
Something went wrong with that request. Please try again.