Class: Plek

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/plek.rb,
lib/plek/version.rb

Overview

Plek resolves service names to a corresponding base URL.

It does this by combining the requested service name with information from environment variables. It will raise a NoConfigurationError if a required environment variable isn’t set.

Development mode fallback defaults

When running development mode (identified by either RAILS_ENV or RACK_ENV environment variables being set to “development”), Plek provides some default values when the necessary environment variables aren’t set detailed below.

Defined Under Namespace

Classes: NoConfigurationError

Constant Summary collapse

DEV_DOMAIN =

The fallback parent domain to use in development mode.

"dev.gov.uk".freeze
VERSION =
"5.2.0".freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(domain_to_use = nil, external_domain = nil) ⇒ Plek

Construct a new Plek instance.

Parameters:

  • domain_to_use (String, nil) (defaults to: nil)

    Optionally override the parent domain to use. If unspecified, this uses the GOVUK_APP_DOMAIN environment variable.

    In development mode, this falls back to DEV_DOMAIN if the environment variable is unset.

  • external_domain (String, nil) (defaults to: nil)

    Optionally override the external domain to use. If unspecified it will fall back to using GOVUK_APP_DOMAIN_EXTERNAL and if that is unavailable the parent domain will be used



37
38
39
40
41
42
43
44
# File 'lib/plek.rb', line 37

def initialize(domain_to_use = nil, external_domain = nil)
  truth_re = /^[1ty]/i
  @parent_domain = domain_to_use || env_var_or_dev_fallback("GOVUK_APP_DOMAIN", DEV_DOMAIN)
  @external_domain = external_domain || ENV.fetch("GOVUK_APP_DOMAIN_EXTERNAL", @parent_domain)
  @host_prefix = ENV.fetch("PLEK_HOSTNAME_PREFIX", "")
  @unprefixable_hosts = ENV.fetch("PLEK_UNPREFIXABLE_HOSTS", "").split(",").map(&:strip)
  @use_http_for_single_label_domains = truth_re.match?(ENV.fetch("PLEK_USE_HTTP_FOR_SINGLE_LABEL_DOMAINS", ""))
end

Instance Attribute Details

#external_domainObject (readonly)

Returns the value of attribute external_domain.



23
24
25
# File 'lib/plek.rb', line 23

def external_domain
  @external_domain
end

#parent_domainObject (readonly)

Returns the value of attribute parent_domain.



23
24
25
# File 'lib/plek.rb', line 23

def parent_domain
  @parent_domain
end

Class Method Details

.asset_rootString

Convenience wrapper. The same as calling Plek.new.asset_root.

Returns:

  • (String)

See Also:



138
# File 'lib/plek.rb', line 138

def_delegators :new, :find, :external_url_for, :asset_root, :website_root

.external_url_forString

Convenience wrapper. The same as calling Plek.new.external_url_for.

Returns:

  • (String)

See Also:



138
# File 'lib/plek.rb', line 138

def_delegators :new, :find, :external_url_for, :asset_root, :website_root

.findString

Convenience wrapper. The same as calling Plek.new.find.

Returns:

  • (String)

See Also:



138
# File 'lib/plek.rb', line 138

def_delegators :new, :find, :external_url_for, :asset_root, :website_root

.website_rootString

Convenience wrapper. The same as calling Plek.new.website_root.

Returns:

  • (String)

See Also:



138
# File 'lib/plek.rb', line 138

def_delegators :new, :find, :external_url_for, :asset_root, :website_root

Instance Method Details

#asset_rootString

Find the base URL for assets.

Returns:

  • (String)

    The assets base URL.



108
109
110
# File 'lib/plek.rb', line 108

def asset_root
  env_var_or_dev_fallback("GOVUK_ASSET_ROOT") { find("static") }
end

#external_url_for(service, options = {}) ⇒ Object

Find the external URL for a service/application.

Parameters:

  • service (String)

    the name of the service to lookup. This should be the hostname of the service.

  • options (Hash) (defaults to: {})

    see the documentation for find.



101
102
103
# File 'lib/plek.rb', line 101

def external_url_for(service, options = {})
  find(service, options.merge(external: true))
end

#find(service, options = {}) ⇒ String

Find the base URL for a service/application. This constructs the URL from the given hostname and the #parent_domain. If the #parent_domain 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 unless the hostname appears in the comma-separated list PLEK_UNPREFIXABLE_HOSTS.

If PLEK_USE_HTTP_FOR_SINGLE_LABEL_DOMAINS=1 in the environment, Plek will use “http” as the URL scheme instead of “https” for single-label domains. Single-label domains are domains with just a single name component, for example “frontend” or “content-store”, as opposed to “frontend.example.com” or “content-store.test.govuk.digital”.

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 variable. This overrides both the “internal” and “external” URL for the service. It is not possible to override them separately.

Parameters:

  • service (String)

    the name of the service to lookup. This should be the hostname of the service.

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :force_http (Boolean)

    If true, force the returned URL to be http.

  • :scheme_relative (Boolean)

    If true, return a URL without a scheme (eg ‘//foo.example.com`)

Returns:

  • (String)

    The base URL for the service.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/plek.rb', line 74

def find(service, options = {})
  name = valid_service_name(service)
  if (service_uri = defined_service_uri_for(name))
    return service_uri
  end

  name = "#{host_prefix}#{name}" unless unprefixable_hosts.include?(name)

  domain = options[:external] ? external_domain : parent_domain
  domain_suffix = domain.empty? ? "" : ".#{domain}"

  scheme = if options[:scheme_relative]
             ""
           elsif options[:force_http] || http_domain?(domain)
             "http:"
           else
             "https:"
           end

  "#{scheme}//#{name}#{domain_suffix}".freeze
end

#website_rootString

Find the base URL for the public website frontend.

Returns:

  • (String)

    The website base URL.



115
116
117
# File 'lib/plek.rb', line 115

def website_root
  env_var_or_dev_fallback("GOVUK_WEBSITE_ROOT") { find("www") }
end