Class: Gitlab::Webpack::DevServerMiddleware

Inherits:
Rack::Proxy
  • Object
show all
Defined in:
lib/gitlab/webpack/dev_server_middleware.rb

Instance Method Summary collapse

Constructor Details

#initialize(app = nil, opts = {}) ⇒ DevServerMiddleware

Returns a new instance of DevServerMiddleware.



10
11
12
13
14
15
16
17
# File 'lib/gitlab/webpack/dev_server_middleware.rb', line 10

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]
  @proxy_scheme = opts[:proxy_https] ? 'https' : 'http'

  super(app, backend: "#{@proxy_scheme}://#{@proxy_host}:#{@proxy_port}", **opts)
end

Instance Method Details

#perform_request(env) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/gitlab/webpack/dev_server_middleware.rb', line 19

def perform_request(env)
  if @proxy_path && env['PATH_INFO'].start_with?("/#{@proxy_path}")
    # 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}"

    if relative_url_root = Rails.application.config.relative_url_root
      env['SCRIPT_NAME'] = ""
      env['REQUEST_PATH'].sub!(/\A#{Regexp.escape(relative_url_root)}/, '')
    end

    super(env)
  else
    @app.call(env)
  end
end