Skip to content

Commit

Permalink
Allow an ENV variable to be prepended to the host
Browse files Browse the repository at this point in the history
When `PLEK_HOSTNAME_PREFIX` is set, it will be prepended to the returned
host in the Ruby code.

This behaviour is overridden by the `PLEK_SERVICE_xxx_URI`
functionality, because prepending plus manual override would cause
unexpected behaviour.
  • Loading branch information
bradwright authored and mattbostock committed Jul 3, 2015
1 parent c0c56fb commit f15c0e7
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# CHANGELOG

# DEV

* Add `PLEK_HOSTNAME_PREFIX` environment variable, which prepends the contents
to the returned hostname

# 1.10.0

* Add `Plek.find_uri` for accessing `URI` objects for any service
Expand Down
9 changes: 9 additions & 0 deletions go/plek.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ var httpDomains = map[string]bool{
// domain as a string. The app domain is taken from the GOVUK_APP_DOMAIN
// environment variable. If this is unset, "dev.gov.uk" is used.
//
// If PLEK_HOSTNAME_PREFIX is present in the environment, it will be prepended
// to the hostname.
//
// The URLs for an individual service can be overridden by setting a corresponding
// PLEK_SERVICE_FOO_URI environment variable. For example, to override the "foo-api"
// service url, set PLEK_SERVICE_FOO_API_URI to the base URL of the service.
Expand Down Expand Up @@ -72,6 +75,12 @@ func New(parentDomain string) Plek {
// FindURL returns the base URL for the given service name as a *url.URL
func (p Plek) FindURL(serviceName string) *url.URL {
u := &url.URL{Scheme: "https", Host: serviceName + "." + p.parentDomain}

hostnamePrefix := os.Getenv("PLEK_HOSTNAME_PREFIX")
if hostnamePrefix != "" {
u.Host = hostnamePrefix + u.Host
}

if httpDomains[p.parentDomain] {
u.Scheme = "http"
}
Expand Down
19 changes: 19 additions & 0 deletions go/plek_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,15 @@ var packageFindExamples = []FindExample{
ServiceName: "foo",
ExpectedURL: "http://foo.dev.gov.uk",
},
// Setting a hostname prefix
{
GovukAppDomain: "",
ServiceName: "foo",
ExpectedURL: "http://draft-foo.dev.gov.uk",
Environ: map[string]string{
"PLEK_HOSTNAME_PREFIX": "draft-",
},
},
// Overriding a specific service URL with an ENV var.
{
GovukAppDomain: "foo.com",
Expand All @@ -124,6 +133,16 @@ var packageFindExamples = []FindExample{
ExpectedURL: "http://invalid%hostname.com",
Environ: map[string]string{"PLEK_SERVICE_FOO_URI": "http://invalid%hostname.com"},
},
// PLEK_SERVICE_FOO_BAR_URI overrides the hostname prefix
{
GovukAppDomain: "foo.com",
ServiceName: "foo-bar",
ExpectedURL: "http://anything.example.com",
Environ: map[string]string{
"PLEK_SERVICE_FOO_BAR_URI": "http://anything.example.com",
"PLEK_HOSTNAME_PREFIX": "draft-",
},
},
}

func TestPackageFind(t *testing.T) {
Expand Down
7 changes: 7 additions & 0 deletions lib/plek.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ def initialize(domain_to_use = nil)
# matches the {DEV_DOMAIN}, the returned URL will be a http URL, otherwise it
# will be https.
#
# If PLEK_HOSTNAME_PREFIX is present in the environment, it will be prepended
# to the hostname.
#
# The URL for a given service can be overridden by setting a corresponding
# environment variable. eg if +PLEK_SERVICE_EXAMPLE_CHEESE_THING_URI+ was
# set, +Plek.new.find('example-cheese-thing')+ would return the value of that
Expand All @@ -60,6 +63,10 @@ def find(service, options = {})

host = "#{name}.#{parent_domain}"

if host_prefix = ENV['PLEK_HOSTNAME_PREFIX']
host = "#{host_prefix}#{host}"
end

if options[:scheme_relative]
"//#{host}"
elsif options[:force_http] or HTTP_DOMAINS.include?(parent_domain)
Expand Down
7 changes: 7 additions & 0 deletions test/plek_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ def test_should_be_able_to_avoid_instantiation_with_uris
assert_equal Plek.new.find_uri("foo"), Plek.find_uri("foo")
end

def test_should_prepend_data_from_the_environment
ENV['PLEK_HOSTNAME_PREFIX'] = 'test-'
assert_equal "https://test-foo.preview.alphagov.co.uk", Plek.new("preview.alphagov.co.uk").find("foo")
ensure
ENV.delete("PLEK_HOSTNAME_PREFIX")
end

def test_scheme_relative_urls
url = Plek.new("dev.gov.uk").find("service", scheme_relative: true)
assert_equal "//service.dev.gov.uk", url
Expand Down

0 comments on commit f15c0e7

Please sign in to comment.