Class: Nonnative::Configuration

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

Overview

The gem configuration object.

You can populate configuration either programmatically via the DSL (#process, #server, #service), or by loading a YAML file via #load_file.

The configuration is consumed when start is called.

Programmatic configuration

Nonnative.configure do |config|
  config.name = 'example'
  config.url = 'http://127.0.0.1:8080'
  config.log = 'test.log'

  config.process do |p|
    p.name = 'api'
    p.command = -> { './bin/api' }
    p.host = '127.0.0.1'
    p.ports = [8080, 9090]
    p.timeout = 10
    p.log = 'api.log'
  end
end

File-based configuration

Nonnative.configure do |config|
  config.load_file('features/configs/processes.yml')
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializevoid

Creates an empty configuration.



38
39
40
41
42
# File 'lib/nonnative/configuration.rb', line 38

def initialize
  @processes = []
  @servers = []
  @services = []
end

Instance Attribute Details

#logString?

Returns path to the Nonnative log file.

Returns:

  • (String, nil)

    path to the Nonnative log file



54
55
56
# File 'lib/nonnative/configuration.rb', line 54

def log
  @log
end

#nameString?

Returns logical system name (used for observability endpoints).

Returns:

  • (String, nil)

    logical system name (used for observability endpoints)



45
46
47
# File 'lib/nonnative/configuration.rb', line 45

def name
  @name
end

#processesArray<Nonnative::ConfigurationProcess>

Returns configured processes.

Returns:



57
58
59
# File 'lib/nonnative/configuration.rb', line 57

def processes
  @processes
end

#serversArray<Nonnative::ConfigurationServer>

Returns configured in-process servers.

Returns:



60
61
62
# File 'lib/nonnative/configuration.rb', line 60

def servers
  @servers
end

#servicesArray<Nonnative::ConfigurationService>

Returns configured services (proxy-only).

Returns:



63
64
65
# File 'lib/nonnative/configuration.rb', line 63

def services
  @services
end

#urlString?

Returns base URL for observability queries (for example ‘“127.0.0.1:8080”`).

Returns:

  • (String, nil)

    base URL for observability queries (for example ‘“127.0.0.1:8080”`)



51
52
53
# File 'lib/nonnative/configuration.rb', line 51

def url
  @url
end

#versionString?

Returns configuration version.

Returns:

  • (String, nil)

    configuration version



48
49
50
# File 'lib/nonnative/configuration.rb', line 48

def version
  @version
end

Instance Method Details

#load_file(path) ⇒ void

This method returns an undefined value.

Loads a configuration file and appends its runners to this instance.

The file is loaded using safe YAML parsing. ERB is not evaluated, arbitrary object deserialization is not allowed, top-level attributes are copied onto this object, and runner sections are transformed into configuration runner objects.

Parameters:

  • path (String)

    path to a configuration file (typically YAML)



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/nonnative/configuration.rb', line 73

def load_file(path)
  cfg = Nonnative::ConfigurationFile.load(path)

  self.version = cfg.version
  self.name = cfg.name
  self.url = cfg.url
  self.log = cfg.log

  add_processes(cfg)
  add_servers(cfg)
  add_services(cfg)
end

#process {|process| ... } ⇒ void

This method returns an undefined value.

Adds a process configuration entry.

Yield Parameters:



90
91
92
93
94
95
# File 'lib/nonnative/configuration.rb', line 90

def process
  process = Nonnative::ConfigurationProcess.new
  yield process

  processes << process
end

#process_by_name(name) ⇒ Nonnative::ConfigurationProcess

Finds a configured process by name.

Parameters:

  • name (String)

Returns:

Raises:



127
128
129
130
131
132
# File 'lib/nonnative/configuration.rb', line 127

def process_by_name(name)
  process = processes.find { |s| s.name == name }
  raise NotFoundError, "Could not find process with name '#{name}'" if process.nil?

  process
end

#server {|server| ... } ⇒ void

This method returns an undefined value.

Adds a server configuration entry.

Yield Parameters:



101
102
103
104
105
106
# File 'lib/nonnative/configuration.rb', line 101

def server
  server = Nonnative::ConfigurationServer.new
  yield server

  servers << server
end

#service {|service| ... } ⇒ void

This method returns an undefined value.

Adds a service configuration entry.

A “service” does not manage a Ruby thread or OS process; it exists so that a proxy can be started and controlled for an external dependency.

Yield Parameters:



115
116
117
118
119
120
# File 'lib/nonnative/configuration.rb', line 115

def service
  service = Nonnative::ConfigurationService.new
  yield service

  services << service
end