-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Proxy Static Pages Map a proxy such that URLs attempt to load jekyll content from the static pages content. * make static pages configurable, proxy static pages through rails --------- Co-authored-by: Stephen Chudleigh <[email protected]>
- Loading branch information
1 parent
348d3af
commit e1ce5be
Showing
14 changed files
with
141 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,4 +19,4 @@ exclude_patterns: | |
- vendor/ | ||
- "**/vendor/**/*" | ||
- app/assets/images/ | ||
- spec/**/* | ||
- spec/**/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,4 +6,4 @@ mkdir -p .nix-bundler | |
export BUNDLE_PATH=./.nix-bundler | ||
|
||
# Login Env Vars | ||
source .env_login | ||
source .env_dev |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -102,3 +102,5 @@ end | |
gem "factory_bot", "~> 6.5" | ||
|
||
gem "faker", "~> 3.4" | ||
|
||
gem "rails-reverse-proxy" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# frozen_string_literal: true | ||
|
||
# Proxy Cloud.gov pages content at the root of the application | ||
class PagesController < ApplicationController | ||
include ReverseProxy::Controller | ||
# We must remove this for proxy of JS assets to be loaded by the browser | ||
protect_from_forgery except: :assets | ||
|
||
# TODO: When launched, the cloud.gov pages need to move off the www.challenge.gov domain | ||
DOMAIN = Rails.configuration.static_site_interop.fetch(:domain) | ||
HOST = Rails.configuration.static_site_interop.fetch(:host) | ||
BASE_URL = Rails.configuration.static_site_interop.fetch(:base_url) | ||
|
||
def index | ||
path = "#{BASE_URL}/#{params[:path]}/" | ||
reverse_proxy(HOST, path:, reset_accept_encoding: true, headers: { host: DOMAIN }) do |config| | ||
config.on_missing do |_code, _response| | ||
redirect_to "/dashboard" | ||
return true | ||
end | ||
|
||
config.on_response do |_code, response| | ||
response.body = rewrite_links(response.body) | ||
end | ||
end | ||
end | ||
|
||
def assets | ||
if params[:ext] == "min" | ||
path = "#{HOST}#{BASE_URL}/assets/#{params[:path]}.#{params[:ext]}.js" | ||
response = Faraday.get(path) | ||
send_data(response.body, type: 'application/javascript') | ||
else | ||
path = "#{BASE_URL}/assets/#{params[:path]}.#{params[:ext]}" | ||
reverse_proxy(HOST, path:, reset_accept_encoding: true, headers: { host: DOMAIN }) | ||
end | ||
end | ||
|
||
def root | ||
path = "#{BASE_URL}/" | ||
reverse_proxy(HOST, path:, reset_accept_encoding: true, headers: { host: DOMAIN }) do |config| | ||
config.on_response do |_code, response| | ||
if response.body.present? | ||
response.body = rewrite_links(response.body) | ||
end | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def rewrite_links(html) | ||
parsed_html = html.gsub(HOST, "/") | ||
if BASE_URL.length > 1 | ||
parsed_html = parsed_html.gsub(BASE_URL, "") | ||
end | ||
# delete the data-public-url attribute from the react app element to send requests through rails proxy | ||
parsed_html = parsed_html.sub(/(<div id="challenge-gov-react-app".+)(data-public-url=[^ ]+)/, '\1') | ||
|
||
# rubocop:disable Rails/OutputSafety | ||
parsed_html.html_safe | ||
# rubocop:enable Rails/OutputSafety | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
require "rails_helper" | ||
|
||
RSpec.describe "PagesController" do | ||
it "get root renders successfully" do | ||
stub_request(:get, PagesController::HOST + PagesController::BASE_URL + "/"). | ||
to_return(status: 200, body: "", headers: {}) | ||
|
||
get "/" | ||
expect(response).to be_ok | ||
end | ||
|
||
it "removes full paths to assets so they proxy too" do | ||
stub_request(:get, PagesController::HOST + PagesController::BASE_URL + "/"). | ||
to_return(status: 200, body: PagesController::HOST + PagesController::BASE_URL, headers: {}) | ||
|
||
get "/" | ||
expect(response.body).to eq("/") | ||
end | ||
|
||
it "works for minified js assets" do | ||
stub_request(:get, PagesController::HOST + PagesController::BASE_URL + "/assets/uswds.min.js"). | ||
to_return(status: 200, body: "", headers: {}) | ||
|
||
get "/assets/uswds.min.js" | ||
expect(response).to be_ok | ||
end | ||
|
||
it "works for image assets" do | ||
stub_request(:get, PagesController::HOST + PagesController::BASE_URL + "/assets/logo.svg"). | ||
to_return(status: 200, body: "", headers: {}) | ||
|
||
get "/assets/logo.svg" | ||
expect(response).to be_ok | ||
end | ||
|
||
it "404s to the dashboard" do | ||
stub_request(:get, PagesController::HOST + PagesController::BASE_URL + "/not_found/"). | ||
to_return(status: 404, body: "", headers: {}) | ||
|
||
get "/not_found" | ||
expect(response).to redirect_to("/dashboard") | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters