Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom response based on source request #38

Open
jeffcarbs opened this issue Jan 6, 2017 · 0 comments
Open

Custom response based on source request #38

jeffcarbs opened this issue Jan 6, 2017 · 0 comments

Comments

@jeffcarbs
Copy link

Use-case

My company's main web application (heroku) and marketing site (wordpress) are served at the same subdomain (www). The root/empty path (e.g. https://www.mysite.com) and some other whitelisted paths are reverse-proxied to wordpress, while the rest are served by a Rails app.

We wanted to make it such that if a request comes in for the root path AND they have a specific cookie (a session cookie for our app), redirect them into the application. Otherwise, reverse-proxy as usual to wordpress.

Solution

Currently I've monkey-patched this gem to support this use-case:

module RackReverseProxy
  class RoundTrip
    private

    alias_method :proxy_original, :proxy

    def proxy
      return app.call(env) if uri.nil?

      if options[:custom_response]
        response = options[:custom_response].call(source_request)
        return response if response
      end

      proxy_original
    end
  end
end

# In config.ru:
custom_response = proc do |request|
  [302, { 'Location' => "#{request.base_url}/app" }, []] if request.cookies['_my_app_session']
end

reverse_proxy(/^$/, passback_host, :custom_response => custom_response)

Just wondering if this is something other people have run into and/or this is something you'd want to bake into this gem. I'm happy turn this into a PR if so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant