waterlink/rack-reverse-proxy

Proxy all requests that not found

swordray opened this issue · 4 comments

I tried by hacking matcher#match. Is there a better way to do that?

Rails.application.config.middleware.use Rack::ReverseProxy do
  matcher = Class.new
  matcher.define_singleton_method :match do |url|
    !File.exist?(File.join(Rails.root, 'public', url)) && url !~ /^\/assets\// && !(Rails.application.routes.recognize_path(url) rescue nil)
  end
  reverse_proxy matcher, 'http://api.secipin.com'
end

This is not a hack, this is a public interface, you implement your own matcher, that have to respond to #match(url) method. It doesn't necessary need to be a singleton method though:

class NotFoundUrlMatcher
  def match(url)
    !File.exist?(File.join(Rails.root, 'public', url)) && url !~ /^\/assets\// && !(Rails.application.routes.recognize_path(url) rescue nil)
  end
end

# ...

Rails.application.config.middleware.use Rack::ReverseProxy do
  reverse_proxy NotFoundUrlMatcher.new, 'http://api.secipin.com'
end

Updated code example to instantiate NotFoundUrlMatcher

@alex-fedorov will you consider supporting lambda or block directly. A non-reusable class is not quite clear and take more lines.

reverse_proxy, -> (url) { f(url) }, 'http://localhost'

Sounds good to me
On Sep 23, 2015 11:09 AM, "Jianqiu Xiao" notifications@github.com wrote:

@alex-fedorov https://github.com/alex-fedorov will you consider
supporting lambda or block directly. A non-reusable class is not quite
clear and take more lines.

reverse_proxy, -> (url) { f(url) }, 'http://localhost'


Reply to this email directly or view it on GitHub
#15 (comment)
.