Class: HWSSettings

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

Overview

A Rack middleware application that creates a single configuration for Helix Web Services.

Most web services are modular Sinatra applications, which does come with it's own settings mechanism. We should try to avoid those settings in most cases. Perhaps only if those settings are only relevant to the logic directly within the Sinatra app.

Many other settings, like the port setting of the associated Helix Versioning Engine, should be exposed and overridable by the client application. These settings should be defined here.

This class provides middleware that will inject an hws_settings object into each request. This hws_settings object is seeded by values declared on this class. When the system starts, our system config file is read in, and default system values are overridden. On any request, these settings can be overridden by the user.

Additionally, there are “system” settings that are only overridable from the system config file. Client classes should reference this class directly: HWSSettings.system.

Naming Conventions

Use uppercase letters, numbers, or underscores only.

HTTP Header Override Syntax

We allow per-request overrides of settings via HTTP headers.

The key format of the custom setting is:

X-PERFORCE-HELIX_WEB_SERVICES-{key}

For example:

X-PERFORCE-HELIX_WEB_SERVICES-P4HOST: perforce.mycompany.com
X-PERFORCE-HELIX_WEB_SERVICES-P4CHARSET: auto

Please note that headers will be converted by Rack to all uppercase, hence our naming conventions.

System Config File

The system configuration is stored in the /etc/perforce/helix_web_services.conf file. This is a YAML file, and we override any locally defined variables with values found in this file. ()If you specify a value in this file we do not locally define, we ignore it.)

Example values in /etc/perforce/helix_web_services.conf:

P4HOST: 'perforce.mycompany.com'
P4PORT: '9991'

Constant Summary

SYSTEM_CONFIG_PATH =
'/etc/perforce/helix_web_services.conf'

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (HWSSettings) initialize(app)

Returns a new instance of HWSSettings



172
173
174
# File 'lib/hws_settings.rb', line 172

def initialize(app)
  @app = app
end

Class Method Details

+ (Object) overrides

Return the system overrides in our system configuration file.



158
159
160
# File 'lib/hws_settings.rb', line 158

def overrides
  return @overrides ||= init_overrides
end

+ (Object) settings

Returns baseline settings with system overrides applied.

This is a copy of state. If you want to alter the default settings in code instead of via config files, use the settings_handle



120
121
122
123
124
125
126
127
128
# File 'lib/hws_settings.rb', line 120

def settings
  s = OpenStruct.new(@settings)
  s.each_pair do |key, _|
    if overrides.respond_to?(key)
      s[key] = overrides[key]
    end
  end
  s
end

+ (Object) settings_handle

You can tweak the default settings directly here in code.



131
132
133
# File 'lib/hws_settings.rb', line 131

def settings_handle
  @settings
end

+ (Object) system

Returns our system settings overridden by local configuration in overrides.

This is a copy of the class system settings, suitable for editing and passing on.

See the official guide for declared options.



141
142
143
144
145
146
147
148
149
# File 'lib/hws_settings.rb', line 141

def system
  s = OpenStruct.new(@system)
  s.each_pair do |key, _|
    if overrides.respond_to?(key)
      s[key] = overrides[key]
    end
  end
  s
end

+ (Object) system_handle

In case your code wants to edit the system classes directly. Typically used for test initialization.



153
154
155
# File 'lib/hws_settings.rb', line 153

def system_handle
  @system
end

Instance Method Details

- (Object) call(env)



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/hws_settings.rb', line 176

def call(env)
  hws_settings = self.class.settings

  # TODO: need to whitelist P4PORT for cloud, arbitrary perforce servers not supported
  env.each do |key, value|
    match = /^HTTP_X_PERFORCE_HELIX_WEB_SERVICES_(.*)$/.match(key)
    if match
      hws_settings[match[1]] = value
    end
  end

  env['hws_settings'] = hws_settings

  @app.call(env)
end