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.
pull/6346/head
Jens Ulferts 6 years ago
parent f18ecfcb27
commit 7da9bde5c8
No known key found for this signature in database
GPG Key ID: 3CAA4B1182CF5308
  1. 50
      app/helpers/frontend_asset_helper.rb
  2. 2
      config/initializers/secure_headers.rb
  3. 9
      config/routes.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

@ -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

@ -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'

Loading…
Cancel
Save