2018-11-19 21:01:13 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-02-01 14:05:33 -05:00
|
|
|
# This Rack middleware is intended to proxy the webpack assets directory to the
|
|
|
|
# webpack-dev-server. It is only intended for use in development.
|
|
|
|
|
2017-08-10 19:09:14 -04:00
|
|
|
# :nocov:
|
2017-02-01 14:05:33 -05:00
|
|
|
module Gitlab
|
2018-04-26 14:47:21 -04:00
|
|
|
module Webpack
|
|
|
|
class DevServerMiddleware < Rack::Proxy
|
2017-02-01 14:05:33 -05:00
|
|
|
def initialize(app = nil, opts = {})
|
|
|
|
@proxy_host = opts.fetch(:proxy_host, 'localhost')
|
|
|
|
@proxy_port = opts.fetch(:proxy_port, 3808)
|
|
|
|
@proxy_path = opts[:proxy_path] if opts[:proxy_path]
|
2020-10-07 02:09:03 -04:00
|
|
|
@proxy_scheme = opts[:proxy_https] ? 'https' : 'http'
|
2017-02-27 01:32:50 -05:00
|
|
|
|
2020-10-07 02:09:03 -04:00
|
|
|
super(app, backend: "#{@proxy_scheme}://#{@proxy_host}:#{@proxy_port}", **opts)
|
|
|
|
end
|
|
|
|
|
2017-02-01 14:05:33 -05:00
|
|
|
def perform_request(env)
|
2017-02-27 01:32:50 -05:00
|
|
|
if @proxy_path && env['PATH_INFO'].start_with?("/#{@proxy_path}")
|
2020-11-03 19:09:12 -05:00
|
|
|
# disable SSL check since any cert used here will likely be self-signed
|
|
|
|
env['rack.ssl_verify_none'] = true
|
|
|
|
|
|
|
|
# ensure we pass the expected Host header so webpack-dev-server doesn't complain
|
|
|
|
env['HTTP_HOST'] = "#{@proxy_host}:#{@proxy_port}"
|
|
|
|
|
2018-05-29 09:03:14 -04:00
|
|
|
if relative_url_root = Rails.application.config.relative_url_root
|
|
|
|
env['SCRIPT_NAME'] = ""
|
|
|
|
env['REQUEST_PATH'].sub!(/\A#{Regexp.escape(relative_url_root)}/, '')
|
|
|
|
end
|
|
|
|
|
2017-02-27 01:32:50 -05:00
|
|
|
super(env)
|
|
|
|
else
|
|
|
|
@app.call(env)
|
2017-02-01 14:05:33 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-08-10 19:09:14 -04:00
|
|
|
# :nocov:
|