Skip to content

Instantly share code, notes, and snippets.

@dblock
Created February 20, 2012 17:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dblock/1870297 to your computer and use it in GitHub Desktop.
Save dblock/1870297 to your computer and use it in GitHub Desktop.
Rackup static pages along with a Grape API, inspired by Rack::TryStatic
class App
def initialize(options)
@try = ['', *options.delete(:try)]
@static = ::Rack::Static.new(
lambda { [404, {}, []] },
options)
end
def call(env)
orig_path = env['PATH_INFO']
# there's probably a better way to do this
if orig_path.starts_with?('/api/')
Acme::API.call(env)
else
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 [404, {}, []]
end
end
end
run App.new({
:root => File.expand_path('../public', __FILE__),
:urls => %w[/],
:try => ['.html', 'index.html', '/index.html']
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment