Class: Cloudenvoy::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/cloudenvoy/config.rb

Overview

Holds cloudenvoy configuration. See Cloudenvoy#configure

Constant Summary collapse

EMULATOR_HOST =

Emulator host

ENV['PUBSUB_EMULATOR_HOST'] || 'localhost:8085'
DEFAULT_PROCESSOR_PATH =

Default application path used for processing messages

'/cloudenvoy/receive'
PROCESSOR_HOST_MISSING =
<<~DOC
  Missing host for processing.
  Please specify a processor hostname in form of `https://some-public-dns.example.com`'
DOC
SUB_PREFIX_MISSING_ERROR =
<<~DOC
  Missing GCP subscription prefix.
  Please specify a subscription prefix in the form of `my-app`.
DOC
PROJECT_ID_MISSING_ERROR =
<<~DOC
  Missing GCP project ID.
  Please specify a project ID in the cloudenvoy configurator.
DOC
SECRET_MISSING_ERROR =
<<~DOC
  Missing cloudenvoy secret.
  Please specify a secret in the cloudenvoy initializer or add Rails secret_key_base in your credentials
DOC

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#gcp_project_idString

Return the GCP project ID.

Returns:

  • (String)

    The ID of the project where pub/sub messages are hosted.



127
128
129
# File 'lib/cloudenvoy/config.rb', line 127

def gcp_project_id
  @gcp_project_id || raise(StandardError, PROJECT_ID_MISSING_ERROR)
end

#gcp_sub_prefixString

Return the prefix used for queues.

Returns:

  • (String)

    The prefix used when creating subscriptions.



118
119
120
# File 'lib/cloudenvoy/config.rb', line 118

def gcp_sub_prefix
  @gcp_sub_prefix || raise(StandardError, SUB_PREFIX_MISSING_ERROR)
end

#loggerLogger, any

Return the Cloudenvoy logger.

Returns:

  • (Logger, any)

    The cloudenvoy logger.



59
60
61
# File 'lib/cloudenvoy/config.rb', line 59

def logger
  @logger ||= defined?(Rails) ? Rails.logger : ::Logger.new($stdout)
end

#mode<Type>

The operating mode.

- :production => send messages to GCP Pub/Sub
- :development => send message to gcloud CLI Pub/Sub emulator

Returns:

  • (<Type>)

    <description>



41
42
43
# File 'lib/cloudenvoy/config.rb', line 41

def mode
  @mode ||= environment == 'development' ? :development : :production
end

#processor_pathString

The path on the host when message payloads will be sent. Default to ‘/cloudenvoy/receive`

Returns:

  • (String)

    The processor path



109
110
111
# File 'lib/cloudenvoy/config.rb', line 109

def processor_path
  @processor_path || DEFAULT_PROCESSOR_PATH
end

#secretString

Return the secret to use to sign the verification tokens attached to messages.

Returns:

  • (String)

    The cloudenvoy secret



137
138
139
140
141
# File 'lib/cloudenvoy/config.rb', line 137

def secret
  @secret || (
    defined?(Rails) && Rails.application.credentials&.dig(:secret_key_base)
  ) || raise(StandardError, SECRET_MISSING_ERROR)
end

Instance Method Details

#environmentString

Return the current environment.

Returns:

  • (String)

    The environment name.



50
51
52
# File 'lib/cloudenvoy/config.rb', line 50

def environment
  ENV['CLOUDENVOY_ENV'] || ENV['RAILS_ENV'] || ENV['RACK_ENV'] || 'development'
end

#processor_hostString

The hostname of the application processing the messages. The hostname must be reachable from Cloud Pub/Sub.

Returns:

  • (String)

    The processor host.



98
99
100
# File 'lib/cloudenvoy/config.rb', line 98

def processor_host
  @processor_host || raise(StandardError, PROCESSOR_HOST_MISSING)
end

#processor_host=(val) ⇒ Object

Set the processor host. In the context of Rails the host will also be added to the list of authorized Rails hosts.

Parameters:

  • val (String)

    The processor host to set.



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/cloudenvoy/config.rb', line 79

def processor_host=(val)
  @processor_host = val

  # Check if Rails supports host filtering
  return unless val &&
                defined?(Rails) &&
                Rails.application.config.respond_to?(:hosts) &&
                Rails.application.config.hosts&.any?

  # Add processor host to the list of authorized hosts
  Rails.application.config.hosts << val.gsub(%r{https?://}, '')
end

#processor_urlString

Return the full URL of the processor. Message payloads will be sent to this URL.

Returns:

  • (String)

    The processor URL.



69
70
71
# File 'lib/cloudenvoy/config.rb', line 69

def processor_url
  File.join(processor_host, processor_path)
end

#publisher_middleware {|@publisher_middleware| ... } ⇒ Cloudenvoy::Middleware::Chain

Return the chain of publisher middlewares.

Yields:

Returns:



148
149
150
151
152
# File 'lib/cloudenvoy/config.rb', line 148

def publisher_middleware
  @publisher_middleware ||= Middleware::Chain.new
  yield @publisher_middleware if block_given?
  @publisher_middleware
end

#subscriber_middleware {|@subscriber_middleware| ... } ⇒ Cloudenvoy::Middleware::Chain

Return the chain of subscriber middlewares.

Yields:

Returns:



159
160
161
162
163
# File 'lib/cloudenvoy/config.rb', line 159

def subscriber_middleware
  @subscriber_middleware ||= Middleware::Chain.new
  yield @subscriber_middleware if block_given?
  @subscriber_middleware
end