dodo  0.0.1
A C++ library to create containerized Linux services
dodo::common::Config Class Reference

Singleton interface to a (read-only) deployment configuration, combining data from the deployment configuration file, environment variables and some operating system attributes such as the CPU count, RAM and virtual memory size. More...

#include <config.hpp>

Collaboration diagram for dodo::common::Config:

Public Types

typedef std::list< std::string > KeyPath
 Used by getValue, enable use of list initializers as path specification, eg. More...
 

Public Member Functions

 Config (const Config &)=delete
 Disallow the copy constructor as this is a singleton. More...
 
bool exists (const KeyPath &keypath) const
 Return true if the KeyPath exists. More...
 
std::string getAppName () const
 Return the application name. More...
 
std::string getConfigPath () const
 Return the path of the configuration file. More...
 
void getDecryptedValue (const KeyPath &keypath, Bytes &decrypted)
 Get the decrypted value from KeyPath - decrypted using the secret from getSecret(). More...
 
std::string getSecret () const
 Get the application secret. More...
 
template<typename T >
getValue (const KeyPath &keypath) const
 Get the value at keypath. More...
 
Configoperator= (const Config &)=delete
 Disallow assignment from another Config, as this is a singleton. More...
 
void readConfig ()
 Read the configuration file. More...
 

Static Public Member Functions

static std::string flattenKeyPath (const KeyPath &keypath)
 Transform a keypath to a string with ":" as seperator between levels. More...
 
static ConfiggetConfig ()
 return the singleton. More...
 
static Configinitialize (const std::string path)
 Initialize the singleton. More...
 

Static Public Attributes

static const Config::KeyPath config_dodo = {"dodo"}
 The dodo root node. More...
 
static const Config::KeyPath config_dodo_common = {"dodo","common"}
 The dodo.common node. More...
 
static const Config::KeyPath config_dodo_common_application = {"dodo","common","application"}
 The dodo.common.application node. More...
 
static const Config::KeyPath config_dodo_common_application_name = {"dodo","common","application","name"}
 The dodo.common.application.name node. More...
 
static const Config::KeyPath config_dodo_common_application_secret = {"dodo","common","application","secret"}
 The dodo.common.application.secret node. More...
 
static const Config::KeyPath config_dodo_common_application_secret_env = {"dodo","common","application","secret","env"}
 The dodo.common.application.secret.env node. More...
 
static const Config::KeyPath config_dodo_common_application_secret_file = {"dodo","common","application","secret","file"}
 The dodo.common.application.secret.file node. More...
 
static const Config::KeyPath config_dodo_common_logger = {"dodo","common","logger"}
 The dodo.common.logger node. More...
 
static const Config::KeyPath config_dodo_common_logger_console = {"dodo","common","logger","console"}
 The dodo.common.logger.console node. More...
 
static const Config::KeyPath config_dodo_common_logger_console_level = {"dodo","common","logger","console","level"}
 The dodo.common.logger.console.level node. More...
 
static const Config::KeyPath config_dodo_common_logger_file = {"dodo","common","logger","file"}
 The dodo.common.logger.file node. More...
 
static const Config::KeyPath config_dodo_common_logger_file_directory = {"dodo","common","logger","file","directory"}
 The dodo.common.logger.file.directory node. More...
 
static const Config::KeyPath config_dodo_common_logger_file_level = {"dodo","common","logger","file","level"}
 The dodo.common.logger.file.level node. More...
 
static const Config::KeyPath config_dodo_common_logger_file_max_file_trail = {"dodo","common","logger","file","max-file-trail"}
 The dodo.common.logger.file.max-file-trail node. More...
 
static const Config::KeyPath config_dodo_common_logger_file_max_size_mib = {"dodo","common","logger","file","max-size-mib"}
 The dodo.common.logger.file.max-size-mib node. More...
 
static const Config::KeyPath config_dodo_common_logger_syslog = {"dodo","common","logger","syslog"}
 The dodo.common.logger.syslog node. More...
 
static const Config::KeyPath config_dodo_common_logger_syslog_facility = {"dodo","common","logger","syslog","facility"}
 The dodo.common.logger.syslog.facility node. More...
 
static const Config::KeyPath config_dodo_common_logger_syslog_level = {"dodo","common","logger","syslog","level"}
 The dodo.common.logger.syslog.level node. More...
 

Protected Member Functions

 Config (const std::string path)
 Constructor called by initialize(). More...
 
virtual ~Config ()
 Destructor. More...
 
void checkConfig ()
 Check for required elements in the config file. More...
 

Protected Attributes

std::string secret_
 The encryption secret / key. More...
 
YAML::Node yaml_
 The root YAML node. More...
 

Static Protected Attributes

static Configconfig_ = nullptr
 The singleton pointer. More...
 
static std::string path_ = ""
 The path to the configuration file. More...
 

Friends

class Application
 Application::~Application() will destruct this singleton. More...
 

Detailed Description

Singleton interface to a (read-only) deployment configuration, combining data from the deployment configuration file, environment variables and some operating system attributes such as the CPU count, RAM and virtual memory size.

As multiple threads may be reading and modifying the configuration data, access must be serialized. However, as the data is exposed as a reference to a YAML::Node, this class cannot control serialization transparently. Developers know when they are done reading or writing the configuration data. For example

{
threads::Mutexer( getConfig()->getMutex() );
const std::string username = config["username"].as<std::string>();
}
// Mutexer is out of scope, destructed and the getConfig()->getMutex() lock is released.

Definition at line 52 of file config.hpp.

Member Typedef Documentation

◆ KeyPath

typedef std::list<std::string> dodo::common::Config::KeyPath

Used by getValue, enable use of list initializers as path specification, eg.

std::string s = getvalue<std::string>( { "level1","level2","level2"} )

on this YAML

level1:
level2:
level3: foo

would set s to "foo". A KeyPath cannot index into arrays.

Definition at line 68 of file config.hpp.

Constructor & Destructor Documentation

◆ Config() [1/2]

dodo::common::Config::Config ( const Config )
delete

Disallow the copy constructor as this is a singleton.

Referenced by initialize().

Here is the caller graph for this function:

◆ Config() [2/2]

dodo::common::Config::Config ( const std::string  path)
protected

Constructor called by initialize().

Parameters
pathThe path to the configuration file.

Definition at line 57 of file config.cpp.

References path_, and readConfig().

Here is the call graph for this function:

◆ ~Config()

virtual dodo::common::Config::~Config ( )
inlineprotectedvirtual

Destructor.

Definition at line 228 of file config.hpp.

Member Function Documentation

◆ checkConfig()

◆ exists()

bool dodo::common::Config::exists ( const KeyPath keypath) const
inline

Return true if the KeyPath exists.

Parameters
keypathThe KeyPath to check.
Returns
True if the keypath exists.

Definition at line 201 of file config.hpp.

References yaml_.

Referenced by checkConfig(), dodo::common::Logger::getFileParams(), and dodo::common::Logger::Logger().

Here is the caller graph for this function:

◆ flattenKeyPath()

std::string dodo::common::Config::flattenKeyPath ( const KeyPath keypath)
static

Transform a keypath to a string with ":" as seperator between levels.

Parameters
keypathThe KeyPath to flatten.
Returns
The flattened keypath.

Definition at line 127 of file config.cpp.

Referenced by checkConfig(), getDecryptedValue(), and getValue().

Here is the caller graph for this function:

◆ getAppName()

std::string dodo::common::Config::getAppName ( ) const
inline

Return the application name.

Returns
the application name.

Definition at line 174 of file config.hpp.

Referenced by dodo::common::Application::Application(), dodo::common::Logger::formatMessage(), dodo::common::Logger::getFileParams(), and dodo::common::Logger::log().

Here is the caller graph for this function:

◆ getConfig()

Config * dodo::common::Config::getConfig ( )
static

return the singleton.

Returns
a pointer to the Config instance.
Exceptions
Exceptionwhen

Definition at line 68 of file config.cpp.

References config_, and throw_Exception.

Referenced by dodo::common::Logger::formatMessage(), dodo::common::Logger::getFileParams(), dodo::common::Logger::log(), and dodo::common::Logger::Logger().

Here is the caller graph for this function:

◆ getConfigPath()

std::string dodo::common::Config::getConfigPath ( ) const
inline

Return the path of the configuration file.

Returns
The path of the configuration file.

Definition at line 162 of file config.hpp.

References path_.

◆ getDecryptedValue()

void dodo::common::Config::getDecryptedValue ( const KeyPath keypath,
Bytes decrypted 
)

Get the decrypted value from KeyPath - decrypted using the secret from getSecret().

Parameters
keypathThe KeyPath to the the value for.
decryptedReceives the decrypted Bytes.

Definition at line 136 of file config.cpp.

References dodo::common::DataCrypt::decrypt(), flattenKeyPath(), getSecret(), path_, throw_Exception, and yaml_.

Here is the call graph for this function:

◆ getSecret()

std::string dodo::common::Config::getSecret ( ) const
inline

Get the application secret.

Returns
The secret.

Definition at line 180 of file config.hpp.

References secret_.

Referenced by getDecryptedValue().

Here is the caller graph for this function:

◆ getValue()

template<typename T >
T dodo::common::Config::getValue ( const KeyPath keypath) const
inline

Get the value at keypath.

Parameters
keypathThe KeyPath to get the value for.
Returns
The value.

Definition at line 187 of file config.hpp.

References flattenKeyPath(), path_, throw_Exception, and yaml_.

Referenced by dodo::common::Logger::getFileParams(), and dodo::common::Logger::Logger().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ initialize()

Config * dodo::common::Config::initialize ( const std::string  path)
static

Initialize the singleton.

Once initialize is called, subsequent calls to getConfig() will return the same pointer as returned by this function.

Parameters
pathThe path to the configuration file.
Returns
a pointer to the Config singleton.

Definition at line 62 of file config.cpp.

References Config(), config_, and throw_Exception.

Referenced by dodo::common::Application::Application().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ operator=()

Config& dodo::common::Config::operator= ( const Config )
delete

Disallow assignment from another Config, as this is a singleton.

◆ readConfig()

void dodo::common::Config::readConfig ( )

Read the configuration file.

Definition at line 117 of file config.cpp.

References checkConfig(), path_, throw_Exception, and yaml_.

Referenced by Config().

Here is the call graph for this function:
Here is the caller graph for this function:

Friends And Related Function Documentation

◆ Application

friend class Application
friend

Application::~Application() will destruct this singleton.

Definition at line 258 of file config.hpp.

Field Documentation

◆ config_

Config * dodo::common::Config::config_ = nullptr
staticprotected

The singleton pointer.

Definition at line 238 of file config.hpp.

Referenced by getConfig(), initialize(), and dodo::common::Application::~Application().

◆ config_dodo

const Config::KeyPath dodo::common::Config::config_dodo = {"dodo"}
static

The dodo root node.

Definition at line 71 of file config.hpp.

Referenced by checkConfig().

◆ config_dodo_common

const Config::KeyPath dodo::common::Config::config_dodo_common = {"dodo","common"}
static

The dodo.common node.

Definition at line 74 of file config.hpp.

Referenced by checkConfig().

◆ config_dodo_common_application

const Config::KeyPath dodo::common::Config::config_dodo_common_application = {"dodo","common","application"}
static

The dodo.common.application node.

Definition at line 77 of file config.hpp.

Referenced by checkConfig().

◆ config_dodo_common_application_name

const Config::KeyPath dodo::common::Config::config_dodo_common_application_name = {"dodo","common","application","name"}
static

The dodo.common.application.name node.

Definition at line 80 of file config.hpp.

Referenced by checkConfig().

◆ config_dodo_common_application_secret

const Config::KeyPath dodo::common::Config::config_dodo_common_application_secret = {"dodo","common","application","secret"}
static

The dodo.common.application.secret node.

Definition at line 83 of file config.hpp.

Referenced by checkConfig().

◆ config_dodo_common_application_secret_env

const Config::KeyPath dodo::common::Config::config_dodo_common_application_secret_env = {"dodo","common","application","secret","env"}
static

The dodo.common.application.secret.env node.

Definition at line 89 of file config.hpp.

Referenced by checkConfig().

◆ config_dodo_common_application_secret_file

const Config::KeyPath dodo::common::Config::config_dodo_common_application_secret_file = {"dodo","common","application","secret","file"}
static

The dodo.common.application.secret.file node.

Definition at line 86 of file config.hpp.

Referenced by checkConfig().

◆ config_dodo_common_logger

const Config::KeyPath dodo::common::Config::config_dodo_common_logger = {"dodo","common","logger"}
static

The dodo.common.logger node.

Definition at line 92 of file config.hpp.

Referenced by checkConfig().

◆ config_dodo_common_logger_console

const Config::KeyPath dodo::common::Config::config_dodo_common_logger_console = {"dodo","common","logger","console"}
static

The dodo.common.logger.console node.

Definition at line 95 of file config.hpp.

Referenced by checkConfig().

◆ config_dodo_common_logger_console_level

const Config::KeyPath dodo::common::Config::config_dodo_common_logger_console_level = {"dodo","common","logger","console","level"}
static

The dodo.common.logger.console.level node.

Definition at line 98 of file config.hpp.

Referenced by checkConfig(), and dodo::common::Logger::Logger().

◆ config_dodo_common_logger_file

const Config::KeyPath dodo::common::Config::config_dodo_common_logger_file = {"dodo","common","logger","file"}
static

The dodo.common.logger.file node.

Definition at line 101 of file config.hpp.

Referenced by checkConfig(), and dodo::common::Logger::Logger().

◆ config_dodo_common_logger_file_directory

const Config::KeyPath dodo::common::Config::config_dodo_common_logger_file_directory = {"dodo","common","logger","file","directory"}
static

The dodo.common.logger.file.directory node.

Definition at line 107 of file config.hpp.

Referenced by checkConfig(), and dodo::common::Logger::getFileParams().

◆ config_dodo_common_logger_file_level

const Config::KeyPath dodo::common::Config::config_dodo_common_logger_file_level = {"dodo","common","logger","file","level"}
static

The dodo.common.logger.file.level node.

Definition at line 104 of file config.hpp.

Referenced by checkConfig(), and dodo::common::Logger::getFileParams().

◆ config_dodo_common_logger_file_max_file_trail

const Config::KeyPath dodo::common::Config::config_dodo_common_logger_file_max_file_trail = {"dodo","common","logger","file","max-file-trail"}
static

The dodo.common.logger.file.max-file-trail node.

Definition at line 113 of file config.hpp.

Referenced by dodo::common::Logger::getFileParams().

◆ config_dodo_common_logger_file_max_size_mib

const Config::KeyPath dodo::common::Config::config_dodo_common_logger_file_max_size_mib = {"dodo","common","logger","file","max-size-mib"}
static

The dodo.common.logger.file.max-size-mib node.

Definition at line 110 of file config.hpp.

Referenced by dodo::common::Logger::getFileParams().

◆ config_dodo_common_logger_syslog

const Config::KeyPath dodo::common::Config::config_dodo_common_logger_syslog = {"dodo","common","logger","syslog"}
static

The dodo.common.logger.syslog node.

Definition at line 116 of file config.hpp.

Referenced by checkConfig(), and dodo::common::Logger::Logger().

◆ config_dodo_common_logger_syslog_facility

const Config::KeyPath dodo::common::Config::config_dodo_common_logger_syslog_facility = {"dodo","common","logger","syslog","facility"}
static

The dodo.common.logger.syslog.facility node.

Definition at line 122 of file config.hpp.

Referenced by dodo::common::Logger::Logger().

◆ config_dodo_common_logger_syslog_level

const Config::KeyPath dodo::common::Config::config_dodo_common_logger_syslog_level = {"dodo","common","logger","syslog","level"}
static

The dodo.common.logger.syslog.level node.

Definition at line 119 of file config.hpp.

Referenced by checkConfig(), and dodo::common::Logger::Logger().

◆ path_

std::string dodo::common::Config::path_ = ""
staticprotected

The path to the configuration file.

Definition at line 243 of file config.hpp.

Referenced by checkConfig(), Config(), getConfigPath(), getDecryptedValue(), getValue(), and readConfig().

◆ secret_

std::string dodo::common::Config::secret_
protected

The encryption secret / key.

Definition at line 253 of file config.hpp.

Referenced by checkConfig(), and getSecret().

◆ yaml_

YAML::Node dodo::common::Config::yaml_
protected

The root YAML node.

Definition at line 248 of file config.hpp.

Referenced by exists(), getDecryptedValue(), getValue(), and readConfig().


The documentation for this class was generated from the following files:
dodo::common::Config::getConfig
static Config * getConfig()
return the singleton.
Definition: config.cpp:68