Skip to content

Commit

Permalink
Merge pull request #261 from alphagov/add-govuk-proxy
Browse files Browse the repository at this point in the history
Add GovukProxy::StaticProxy for development
  • Loading branch information
theseanything authored Sep 5, 2022
2 parents c669786 + a3495bc commit 08fd9cf
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 4.9.0

- Add GovukProxy::StaticProxy to forward Static asset requests by setting `GOVUK_PROXY_STATIC_ENABLED=true`.([#261](https://github.com/alphagov/govuk_app_config/pull/261))

# 4.8.0

- Enables Sentry environment names for EKS versions of integration, staging and production.([#260](https://github.com/alphagov/govuk_app_config/pull/260))
Expand Down
2 changes: 2 additions & 0 deletions govuk_app_config.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ Gem::Specification.new do |spec|
spec.require_paths = %w[lib]

spec.add_dependency "logstasher", "~> 2.1"
spec.add_dependency "plek", "~> 4"
spec.add_dependency "prometheus_exporter", "~> 2.0"
spec.add_dependency "puma", "~> 5.6"
spec.add_dependency "rack-proxy", "~> 0.7"
spec.add_dependency "sentry-rails", "~> 5.3"
spec.add_dependency "sentry-ruby", "~> 5.3"
spec.add_dependency "statsd-ruby", "~> 1.5"
Expand Down
1 change: 1 addition & 0 deletions lib/govuk_app_config.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require "govuk_app_config/version"
require "govuk_app_config/govuk_statsd"
require "govuk_app_config/govuk_error"
require "govuk_app_config/govuk_proxy/static_proxy"
require "govuk_app_config/govuk_healthcheck"
require "govuk_app_config/govuk_i18n"
# This require is deprecated and should be removed on next major version bump
Expand Down
19 changes: 19 additions & 0 deletions lib/govuk_app_config/govuk_proxy/static_proxy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require "rack-proxy"

module GovukProxy
class StaticProxy < Rack::Proxy
def perform_request(env)
request = Rack::Request.new(env)

# use rack proxy to forward any requests for /assets/static/*
# this regex needs to match the path set for `Rails.application.config.assets.prefix` in Static
# https://github.com/alphagov/static/blob/main/config/initializers/assets.rb
if request.path =~ %r{^/assets/static/}
env["HTTP_HOST"] = @backend.host
super(env)
else
@app.call(env)
end
end
end
end
9 changes: 9 additions & 0 deletions lib/govuk_app_config/railtie.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
require "plek"

module GovukAppConfig
class Railtie < Rails::Railtie
initializer "govuk_app_config.configure_govuk_proxy" do |app|
if ENV["GOVUK_PROXY_STATIC_ENABLED"] == "true"
static_url = Plek.new.find("static")
app.middleware.use GovukProxy::StaticProxy, backend: static_url
end
end

config.before_initialize do
GovukLogging.configure if Rails.env.production?
end
Expand Down
2 changes: 1 addition & 1 deletion lib/govuk_app_config/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module GovukAppConfig
VERSION = "4.8.0".freeze
VERSION = "4.9.0".freeze
end
40 changes: 40 additions & 0 deletions spec/lib/govuk_proxy/static_proxy_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
require "spec_helper"
require "govuk_app_config/govuk_proxy/static_proxy"
require "rack/mock"

RSpec.describe GovukProxy::StaticProxy do
def test_request(path, proxied)
dest_host = (proxied ? static_domain : app_domain)

stub_request(:get, "https://#{dest_host}#{path}")
.with(headers: { "Host" => dest_host })
.to_return(status: 200, body: "", headers: {})

env = Rack::MockRequest.env_for("http://#{app_domain}#{path}")
status, _headers, _response = proxy.call(env)
expect(status.to_i).to eq(200)
end

# dummy app to validate success
let(:app) { ->(_env) { [200, {}, "success"] } }
let(:app_domain) { "app.domain" }
let(:static_domain) { "static.domain" }

let(:proxy) { GovukProxy::StaticProxy.new(app, backend: "https://static.domain", streaming: false) }

it "redirects the request if path begins with /asset/static" do
test_request("/assets/static/a.css", true)
end

it "ignores requests not with path prefix /asset/static" do
test_request("/assets/app/a.css", false)
end

it "ignores requests where /asset/static isn't a prefix" do
test_request("/another/prefix/assets/static/a.css", false)
end

it "ignores requests with no path" do
test_request("/", false)
end
end

0 comments on commit 08fd9cf

Please sign in to comment.