Skip to content

Instantly share code, notes, and snippets.

@ahoward
Created October 13, 2017 17:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahoward/6b3d87088e619d619f539630f3a87a0e to your computer and use it in GitHub Desktop.
Save ahoward/6b3d87088e619d619f539630f3a87a0e to your computer and use it in GitHub Desktop.
class WebpackController < ApplicationController
include ReverseProxy::Controller
# https://stackoverflow.com/questions/10883211/deadly-cors-when-http-localhost-is-the-origin
#
layout false
HEADERS =
{
#
}
def index
options = reverse_proxy_options.dup
path = params[:path]
if request.query_string.present?
path += "?#{ request.query_string }"
end
options[:path] = path
url = Webpack.server.uri.to_s
url += '/' unless url.ends_with?('/')
reverse_proxy(url, options) do |config|
config.on_complete do |code, response|
content_type = response['Content-Type']
proxied_url = URI.parse(File.join(url, options[:path])).to_s
if code.to_i >= 400
send_data error_script_for(proxied_url, response), content_type: 'text/javascript', disposition: 'inline' #, status: code
end
end
end
end
def error_script_for(url, response)
html = error_html_for(url, response)
script = <<-__
document.write(#{ html.to_json });
__
script.html_safe
end
def error_html_for(url, response)
err = response.body.to_s
fullpath = request.fullpath
headers = response.instance_variable_get('@header')
<<-__
<html>
<body style='padding:1em;'>
<h2>
WEBPACK @ <a href="#{ url }">#{ esc(url) }</a>
</h2>
<hr>
<br>
<dl>
<dt>
URL
</dt>
<dd style='padding:1em;'>
<a href="#{ url }">#{ esc(fullpath) }</a>
</dd>
<dt>
HEADERS
</dt>
<dd style='padding:1em;'>
<code style='white-space:pre;'>
#{ esc(headers.to_yaml) }
</code>
</dd>
<dt>
RESPONSE
</dt>
<dd style='padding:1em;'>
#{ esc(err) }
</dd>
</dl>
</body>
</html>
__
end
def esc(html)
CGI::escapeHTML(html.to_s)
end
protected
if Rails.stage.try(:production?)
def debugging(*args, &block)
block.call
end
else
def debugging(*args, &block)
url, options, trace = nil
url = args.shift
options = args.shift
trace = Net::HTTP.debugging{ block.call }
ensure
begin
message = []
#message << "WEBPACK: url=#{ url.inspect }, options=#{ options.inspect }, trace=#{ trace.first(128) }..."
message << "WEBPACK: url=#{ url.inspect }, options=#{ options.inspect }"
trace.to_s.each_line do |line|
#message << " NG: #{ line.strip }"
end
Rails.logger.info(message)
rescue
nil
end
end
end
def webpack_server_url
if Webpack.server
server.uri
end
end
def reverse_proxy_options
{}.tap do |options|
options[:verify_ssl] = false
options[:headers] = HEADERS
end
end
def render(options = {}, &block)
if options[:body]
options[:text] = options.delete(:body) # Rails' 3 HACK : ref: https://github.com/rails/rails/issues/12374
end
super(options, &block)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment