Class: ExperellaProxy::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/experella-proxy/configuration.rb

Overview

The Configuration loader

The config specifies following DSL options

backend

Takes an options hash defining a backend_server, see {BackendServer}

set_logger

specifies the Logger used by the program. The Logger must support debug/info/warn/error/fatal functions

set_proxy

Add proxy as Hash with :host => "string-ip/domain" and :proxy => Fixnum
The proxy will listen on every host:port hash added with this function.

set_timeout

Time as float when an idle persistent connection gets closed (no receive/send events occured)

set_error_pages

Add html error-pages to the proxy, requires 2 arguments
1st arg: the error code as Fixnum
2nd arg: path to an error page html file relative to the config file directory
Currently 404 and 503 error codes are supported

Defined Under Namespace

Classes: NoConfigError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Configuration

The Configuration

Parameters:

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

Options Hash (options):

  • :configfile (String)

    the config filepath



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/experella-proxy/configuration.rb', line 54

def initialize(options={})
  @backends = []
  @proxy = []
  @error_pages = { 404 => "", 503 => "" }
  @on_event = lambda{ |name, data| }
  default_options = { :timeout => 15.0, :logger => Logger.new($stdout) }
  options = options.reduce({}){ |memo, (k, v)| memo[k.to_sym] = v; memo }
  options = default_options.merge(options)
  options.each do |k, v|
    instance_variable_set("@#{k}", v)
  end
  read_config_file(@configfile) if @configfile
  ExperellaProxy.config = self
end

Instance Attribute Details

#backendsObject (readonly)

Returns the value of attribute backends.



46
47
48
# File 'lib/experella-proxy/configuration.rb', line 46

def backends
  @backends
end

#error_pagesObject (readonly)

Returns the value of attribute error_pages.



46
47
48
# File 'lib/experella-proxy/configuration.rb', line 46

def error_pages
  @error_pages
end

#loggerObject (readonly)

Returns the value of attribute logger.



46
47
48
# File 'lib/experella-proxy/configuration.rb', line 46

def logger
  @logger
end

#on_eventObject (readonly)

Returns the value of attribute on_event.



46
47
48
# File 'lib/experella-proxy/configuration.rb', line 46

def on_event
  @on_event
end

#proxyObject (readonly)

Returns the value of attribute proxy.



46
47
48
# File 'lib/experella-proxy/configuration.rb', line 46

def proxy
  @proxy
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



46
47
48
# File 'lib/experella-proxy/configuration.rb', line 46

def timeout
  @timeout
end

Instance Method Details

#backend(backend_options) ⇒ Object

Adds a BackendServer specified in the config file to #backends It allows some syntactic sugar to pass :host_port as an abbrev of host and port keys separated by ‘:’.

Parameters:

  • backend_options (Hash)

    backends option hash

See Also:

  • ExperellaProxy::Configuration.{BackendServer{BackendServer#initialize}


99
100
101
102
103
104
105
106
107
# File 'lib/experella-proxy/configuration.rb', line 99

def backend(backend_options)
  host_port = backend_options.delete(:host_port)
  if host_port
    host, port = host_port.split(":")
    backend_options[:host] = host
    backend_options[:port] = port
  end
  @backends << backend_options
end

#join_config_path(filename) ⇒ String

Return filenames fullpath relative to configfile directory

Parameters:

  • filename (String)

    the filename

Returns:

  • (String)

    full filepath



73
74
75
# File 'lib/experella-proxy/configuration.rb', line 73

def join_config_path(filename)
  File.expand_path(File.join(File.dirname(@configfile), filename))
end

#read_config_file(configfile) ⇒ Boolean

Opens the given config file and evaluates it’s contents DSL

Parameters:

  • configfile (String)

    the config filepath

Returns:

  • (Boolean)

    true on success, false if file can’t be found



81
82
83
84
85
86
87
88
89
# File 'lib/experella-proxy/configuration.rb', line 81

def read_config_file(configfile)
  unless File.exist?(configfile)
    puts "error reading #{configfile}"
    raise NoConfigError.new("unable to read config file #{configfile}")
  end
  content = File.read(configfile)
  instance_eval(content)
  true
end

#set_error_pages(key, page_path) ⇒ Object

Loads the Errorpages specified in the config file

currently 404 and 503 errors are supported

Parameters:

  • key (Fixnum)

    HTTP Error code

  • page_path (String)

    page_path relative to config file directory



150
151
152
# File 'lib/experella-proxy/configuration.rb', line 150

def set_error_pages(key, page_path)
  @error_pages[key] = File.read(join_config_path(page_path))
end

#set_logger(logger) ⇒ Object

Sets the global Logger object specified in the config file

Logger can be any object that responds to Ruby Loggers debug, info, warn, error, fatal functions

Parameters:

  • logger (Logger)

    the logger object



124
125
126
# File 'lib/experella-proxy/configuration.rb', line 124

def set_logger(logger)
  @logger = logger
end

#set_on_event(lambda) ⇒ Object



109
110
111
# File 'lib/experella-proxy/configuration.rb', line 109

def set_on_event(lambda)
  @on_event = lambda
end

#set_proxy(proxy) ⇒ Object

Adds a Proxy specified in the config file to #proxy

Multiple proxies can be added with multiple calls. Needs a Hash containing :host => “domain/ip”, :port => fixnum

Parameters:

  • proxy (Hash)

    proxy Hash with :host => “domain/ip”, :port => fixnum, :options => options

  • options (Hash)

    a customizable set of options



136
137
138
139
140
141
142
# File 'lib/experella-proxy/configuration.rb', line 136

def set_proxy(proxy)
  if proxy[:options] && proxy[:options][:tls]
    proxy[:options][:private_key_file] = join_config_path(proxy[:options][:private_key_file])
    proxy[:options][:cert_chain_file] = join_config_path(proxy[:options][:cert_chain_file])
  end
  @proxy << proxy
end

#set_timeout(to) ⇒ Object

Sets the ExperellaProxy::Connection timeout specified in the config file

Parameters:

  • to (Float)

    timeout as float



115
116
117
# File 'lib/experella-proxy/configuration.rb', line 115

def set_timeout(to)
  @timeout = to
end