From 7da9bde5c8dc46c1b2537f42a201911da9473a30 Mon Sep 17 00:00:00 2001 From: Jens Ulferts Date: Mon, 11 Jun 2018 15:41:15 +0200 Subject: [PATCH] fix angular cli handling in test Proxy href can be configured via OPENPROJECT_CLI_PROXY='[PROXY HREF]'. 'http://localhost:4200' is configured to be the default. Also fixes accessing the server via port 3000 in dev mode. --- app/helpers/frontend_asset_helper.rb | 50 ++++++++++++++++----------- config/initializers/secure_headers.rb | 2 +- config/routes.rb | 9 +++++ 3 files changed, 39 insertions(+), 22 deletions(-) diff --git a/app/helpers/frontend_asset_helper.rb b/app/helpers/frontend_asset_helper.rb index 57122df4b3..82d1c851df 100644 --- a/app/helpers/frontend_asset_helper.rb +++ b/app/helpers/frontend_asset_helper.rb @@ -28,24 +28,33 @@ #++ module FrontendAssetHelper + CLI_DEFAULT_PROXY = 'http://localhost:4200'.freeze + + def self.assets_proxied? + !Rails.env.production? && cli_proxy? + end + + def self.cli_proxy + ENV.fetch('OPENPROJECT_CLI_PROXY', CLI_DEFAULT_PROXY) + end + + def self.cli_proxy? + cli_proxy.present? + end ## # Include angular CLI frontend assets by either referencing a prod build, # or referencing the running CLI proxy that hosts the assets in memory. def include_frontend_assets capture do - if Rails.env.production? || !frontend_assets_proxied? - concat javascript_include_tag frontend_asset_path "vendor.js" - concat javascript_include_tag frontend_asset_path "polyfills.js" - concat javascript_include_tag frontend_asset_path "runtime.js" - concat javascript_include_tag frontend_asset_path "main.js" - concat stylesheet_link_tag frontend_asset_path "styles.css" + %w(vendor.js polyfills.js runtime.js main.js).each do |file| + concat javascript_include_tag variable_asset_path(file) + end + + if FrontendAssetHelper.assets_proxied? + concat javascript_include_tag variable_asset_path("styles.js") else - concat javascript_include_tag angular_cli_asset "vendor.js" - concat javascript_include_tag angular_cli_asset "polyfills.js" - concat javascript_include_tag angular_cli_asset "runtime.js" - concat javascript_include_tag angular_cli_asset "main.js" - concat javascript_include_tag angular_cli_asset "styles.js" + concat stylesheet_link_tag variable_asset_path("styles.css") end end end @@ -53,16 +62,7 @@ module FrontendAssetHelper private def angular_cli_asset(path) - base_url = ENV.fetch('OPENPROJECT_DEV_CLI_PROXY', 'http://localhost:4200') - - URI.join(base_url, path) - end - - def frontend_assets_proxied? - proxy_in_dev = Rails.env.development? && !ActiveRecord::Type::Boolean.new.cast(ENV['OPENPROJECT_NO_CLI_PROXY']) - proxy_in_test = Rails.env.test? && ActiveRecord::Type::Boolean.new.cast(ENV['OPENPROJECT_CLI_PROXY_IN_TEST']) - - proxy_in_dev || proxy_in_test + URI.join(FrontendAssetHelper.cli_proxy, path) end def frontend_asset_path(unhashed, options = {}) @@ -70,4 +70,12 @@ module FrontendAssetHelper asset_path "assets/frontend/#{file_name}", options.merge(skip_pipeline: true) end + + def variable_asset_path(path) + if FrontendAssetHelper.assets_proxied? + angular_cli_asset(path) + else + frontend_asset_path(path) + end + end end diff --git a/config/initializers/secure_headers.rb b/config/initializers/secure_headers.rb index 770df6beaf..fc9824cdaf 100644 --- a/config/initializers/secure_headers.rb +++ b/config/initializers/secure_headers.rb @@ -25,7 +25,7 @@ SecureHeaders::Configuration.default do |config| # Allow requests to CLI in dev mode connect_src = default_src - if Rails.env.development? + if FrontendAssetHelper.assets_proxied? connect_src += %w[ws://localhost:4200 http://localhost:4200] assets_src += %w[ws://localhost:4200 http://localhost:4200] end diff --git a/config/routes.rb b/config/routes.rb index 4fd012e67a..03a812340c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -51,6 +51,15 @@ OpenProject::Application.routes.draw do get '/auth/:provider', to: proc { [404, {}, ['']] }, as: 'omniauth_start' match '/auth/:provider/callback', to: 'account#omniauth_login', as: 'omniauth_login', via: %i[get post] + # In case assets are actually delivered by a node server (e.g. in test env) + # we redirect all requests to socksjs necessary to support HMR to that server. + # Status code 307 is important so that POST requests are repeated as POST. + if FrontendAssetHelper.assets_proxied? + match '/sockjs-node/*appendix', + to: redirect("http://localhost:4200/sockjs-node/%{appendix}", status: 307), + via: :all + end + scope controller: 'account' do get '/account/force_password_change', action: 'force_password_change' post '/account/change_password', action: 'change_password'