Skip to content

Instantly share code, notes, and snippets.

@rvalyi
Last active January 1, 2016 23:09
Show Gist options
  • Save rvalyi/8214311 to your computer and use it in GitHub Desktop.
Save rvalyi/8214311 to your computer and use it in GitHub Desktop.
rack proxy for OpenERP integration in Rails app
require 'rack-proxy'
class AppProxy < Rack::Proxy
def initialize(app)
@app = app
end
def call(env)
if env['PATH_INFO'] == "/shop"
[301, {"Location" => "/"}, self]
elsif env['PATH_INFO'] =~ %r{^/web} || env['PATH_INFO'] =~ %r{^/shop}
rewrite_response(perform_request(rewrite_env(env)))
else
@app.call(env)
end
end
def rewrite_env(env)
env["HTTP_HOST"] = "localhost:8069"
env
end
def extract_http_request_headers_with_hack(env)
headers = extract_http_request_headers_without_hack(env)
headers.delete("IF-NONE-MATCH") # don't ask me why but this is required
headers.merge!("HOST" => "localhost:9292") # preserve host, required
end
alias_method :extract_http_request_headers_without_hack, :extract_http_request_headers
alias_method :extract_http_request_headers, :extract_http_request_headers_with_hack
end
config.middleware.insert 0, AppProxy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment