From a2d837a3719187a906b60b9212b0dbf02396cb59 Mon Sep 17 00:00:00 2001 From: Mike Greiling <mike@pixelcog.com> Date: Wed, 1 Feb 2017 13:05:33 -0600 Subject: [PATCH] add rack middleware to proxy webpack dev server --- Gemfile | 2 ++ Gemfile.lock | 3 +++ config/application.rb | 2 ++ config/gitlab.yml.example | 10 ++++++++++ config/initializers/1_settings.rb | 9 +++++++++ config/initializers/static_files.rb | 21 +++++++++++++++++++++ lib/gitlab/middleware/webpack_proxy.rb | 24 ++++++++++++++++++++++++ 7 files changed, 71 insertions(+) create mode 100644 lib/gitlab/middleware/webpack_proxy.rb diff --git a/Gemfile b/Gemfile index f9aecca70ff..6af69e8a37a 100644 --- a/Gemfile +++ b/Gemfile @@ -312,6 +312,8 @@ group :development, :test do gem 'activerecord_sane_schema_dumper', '0.2' gem 'stackprof', '~> 0.2.10' + + gem 'rack-proxy', '~> 0.6.0' end group :test do diff --git a/Gemfile.lock b/Gemfile.lock index 0434fdefcd5..bcf500b16f9 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -544,6 +544,8 @@ GEM rack (>= 1.1) rack-protection (1.5.3) rack + rack-proxy (0.6.0) + rack rack-test (0.6.3) rack (>= 1.0) rails (4.2.7.1) @@ -943,6 +945,7 @@ DEPENDENCIES rack-attack (~> 4.4.1) rack-cors (~> 0.4.0) rack-oauth2 (~> 1.2.1) + rack-proxy (~> 0.6.0) rails (= 4.2.7.1) rails-deprecated_sanitizer (~> 1.0.3) rainbow (~> 2.1.0) diff --git a/config/application.rb b/config/application.rb index 4efe73c7798..9088d3c432b 100644 --- a/config/application.rb +++ b/config/application.rb @@ -84,6 +84,8 @@ module Gitlab config.webpack.config_file = "config/webpack.config.js" config.webpack.output_dir = "public/assets/webpack" config.webpack.public_path = "assets/webpack" + + # Webpack dev server configuration is handled in initializers/static_files.rb config.webpack.dev_server.enabled = false # Enable the asset pipeline diff --git a/config/gitlab.yml.example b/config/gitlab.yml.example index 42e5f105d46..2906633fcbc 100644 --- a/config/gitlab.yml.example +++ b/config/gitlab.yml.example @@ -505,6 +505,16 @@ production: &base # Git timeout to read a commit, in seconds timeout: 10 + ## Webpack settings + # If enabled, this will tell rails to serve frontend assets from the webpack-dev-server running + # on a given port instead of serving directly from /assets/webpack. This is only indended for use + # in development. + webpack: + # dev_server: + # enabled: true + # host: localhost + # port: 3808 + # # 5. Extra customization # ========================== diff --git a/config/initializers/1_settings.rb b/config/initializers/1_settings.rb index 4f33aad8693..ea61aa9e047 100644 --- a/config/initializers/1_settings.rb +++ b/config/initializers/1_settings.rb @@ -409,6 +409,15 @@ Settings.rack_attack.git_basic_auth['bantime'] ||= 1.hour Settings['gitaly'] ||= Settingslogic.new({}) Settings.gitaly['socket_path'] ||= ENV['GITALY_SOCKET_PATH'] +# +# Webpack settings +# +Settings['webpack'] ||= Settingslogic.new({}) +Settings.webpack['dev_server'] ||= Settingslogic.new({}) +Settings.webpack.dev_server['enabled'] ||= false +Settings.webpack.dev_server['host'] ||= 'localhost' +Settings.webpack.dev_server['port'] ||= 3808 + # # Testing settings # diff --git a/config/initializers/static_files.rb b/config/initializers/static_files.rb index d6dbf8b9fbf..718cdd51782 100644 --- a/config/initializers/static_files.rb +++ b/config/initializers/static_files.rb @@ -12,4 +12,25 @@ if app.config.serve_static_files app.paths["public"].first, app.config.static_cache_control ) + + # If webpack-dev-server is configured, proxy webpack's public directory + # instead of looking for static assets + if Gitlab.config.webpack.dev_server.enabled + app.config.webpack.dev_server.merge!( + enabled: true, + host: Gitlab.config.gitlab.host, + port: Gitlab.config.gitlab.port, + https: Gitlab.config.gitlab.https, + manifest_host: Gitlab.config.webpack.dev_server.host, + manifest_port: Gitlab.config.webpack.dev_server.port, + ) + + app.config.middleware.insert_before( + Gitlab::Middleware::Static, + Gitlab::Middleware::WebpackProxy, + proxy_path: app.config.webpack.public_path, + proxy_host: Gitlab.config.webpack.dev_server.host, + proxy_port: Gitlab.config.webpack.dev_server.port, + ) + end end diff --git a/lib/gitlab/middleware/webpack_proxy.rb b/lib/gitlab/middleware/webpack_proxy.rb new file mode 100644 index 00000000000..3fe32adeade --- /dev/null +++ b/lib/gitlab/middleware/webpack_proxy.rb @@ -0,0 +1,24 @@ +# This Rack middleware is intended to proxy the webpack assets directory to the +# webpack-dev-server. It is only intended for use in development. + +module Gitlab + module Middleware + class WebpackProxy < Rack::Proxy + 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] + super(app, opts) + end + + def perform_request(env) + unless @proxy_path && env['PATH_INFO'].start_with?("/#{@proxy_path}") + return @app.call(env) + end + + env['HTTP_HOST'] = "#{@proxy_host}:#{@proxy_port}" + super(env) + end + end + end +end -- GitLab