Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[30] Proxy Static Pages #317

Merged
merged 9 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .codeclimate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ exclude_patterns:
- vendor/
- "**/vendor/**/*"
- app/assets/images/
- spec/**/*
- spec/**/*
1 change: 1 addition & 0 deletions .simplecov
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ SimpleCov.start 'rails' do
add_filter '/app/jobs/application_job.rb'
add_filter '/app/mailers/application_mailer.rb'
add_filter '/app/models/application_record.rb'
add_filter '/app/controllers/pages_controller.rb'

add_filter '/app/controllers/sandbox_controller.rb'

Expand Down
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,5 @@ end
gem "factory_bot", "~> 6.5"

gem "faker", "~> 3.4"

gem "rails-reverse-proxy"
4 changes: 4 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,9 @@ GEM
rails-html-sanitizer (1.6.0)
loofah (~> 2.21)
nokogiri (~> 1.14)
rails-reverse-proxy (0.13.0)
actionpack
addressable
railties (7.2.2)
actionpack (= 7.2.2)
activesupport (= 7.2.2)
Expand Down Expand Up @@ -398,6 +401,7 @@ DEPENDENCIES
puma (>= 6.4.3)
rails (~> 7.2.1)
rails-controller-testing
rails-reverse-proxy
rspec-rails
rspec_junit_formatter
rubocop (>= 1.66.0)
Expand Down
67 changes: 67 additions & 0 deletions app/controllers/pages_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# 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
Fixed Show fixed Hide fixed
Dismissed Show dismissed Hide dismissed

# TODO: When launched, the cloud.gov pages need to move off the www.challenge.gov domain
# and these constants will need to be updated. The will be similar to the commented out versions
# and likely based on content.challenge.gov
DOMAIN = "federalist-2c628203-05c2-48ab-8f87-3eda79380559.sites.pages.cloud.gov"
HOST = "https://federalist-2c628203-05c2-48ab-8f87-3eda79380559.sites.pages.cloud.gov"
BASE_URL = "/preview/gsa/challenges-and-prizes/staging/"
# DOMAIN = "www.challenge.gov".freeze
# HOST = "https://www.challenge.gov".freeze
# BASE_URL = "/".freeze

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("https://challenge.gov/", "/")
danivovich marked this conversation as resolved.
Show resolved Hide resolved
parsed_html = parsed_html.gsub(HOST, "/")
danivovich marked this conversation as resolved.
Show resolved Hide resolved
if BASE_URL.length > 1
parsed_html = parsed_html.gsub(BASE_URL, "/")
end
# rubocop:disable Rails/OutputSafety
parsed_html.html_safe
# rubocop:enable Rails/OutputSafety
end
end
3 changes: 2 additions & 1 deletion config/initializers/assets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@

Rails.application.config.assets.enabled = true


# Move the rails assets so they don't conflict with the static pages
Rails.application.config.assets.prefix = "/platform-assets"

# Rails.application.config.assets.paths << Rails.root.join("app", "assets", "plugins", "uswds","js")

Expand Down
11 changes: 8 additions & 3 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
delete 'timeout'
end

get '/', to: "dashboard#index"
get '/dashboard', to: "dashboard#index"

resources :evaluations, only: [:index]
Expand All @@ -35,13 +34,19 @@
# Can be used by load balancers and uptime monitors to verify that the app is live.
get "up" => "rails/health#show", as: :rails_health_check

# Defines the root path route ("/")
# root "posts#index"
if Rails.env.development? || Rails.env.dev? || Rails.env.test?
namespace :dev do
get "/sandbox", to: "sandbox#index"
get "/accounts", to: "accounts#index"
post "/login", to: "accounts#login"
end
end

if Rails.env.test?
get '/', to: "dashboard#index"
danivovich marked this conversation as resolved.
Show resolved Hide resolved
else
match '/assets/*path.:ext' => 'pages#assets', via: [:get]
match '/*path' => 'pages#index', via: [:get]
match '/' => 'pages#root', via: [:get]
end
end
Loading