mirror of
https://github.com/matrix-construct/construct
synced 2024-11-29 10:12:39 +01:00
ircd::conf: Simplify conf related; add \brief blerb.
This commit is contained in:
parent
d1944c6006
commit
c00d27d7ea
2 changed files with 13 additions and 93 deletions
|
@ -13,6 +13,16 @@
|
|||
|
||||
/// Configuration system.
|
||||
///
|
||||
/// This system disseminates mutable runtime values throughout IRCd. All users
|
||||
/// that integrate a configurable value will create a [static] conf::item<>
|
||||
/// instantiated with one of the explicit types available; also a name and
|
||||
/// default value.
|
||||
///
|
||||
/// All conf::items are collected by this system. Users that administrate
|
||||
/// configuration will push values to the conf::item's. The various items have
|
||||
/// O(1) access to the value contained in their item instance. Administrators
|
||||
/// have logarithmic access through this interface using the items map by name.
|
||||
///
|
||||
namespace ircd::conf
|
||||
{
|
||||
template<class T = void> struct item; // doesn't exist
|
||||
|
@ -33,15 +43,12 @@ namespace ircd::conf
|
|||
IRCD_EXCEPTION(error, not_found)
|
||||
IRCD_EXCEPTION(error, bad_value)
|
||||
|
||||
extern const std::string &config; //TODO: X
|
||||
extern std::map<string_view, item<> *> items;
|
||||
|
||||
bool exists(const string_view &key);
|
||||
string_view get(const string_view &key, const mutable_buffer &out);
|
||||
bool set(const string_view &key, const string_view &value);
|
||||
bool set(std::nothrow_t, const string_view &key, const string_view &value);
|
||||
|
||||
void init(const string_view &configfile);
|
||||
}
|
||||
|
||||
/// Conf item base class. You don't create this directly; use one of the
|
||||
|
|
93
ircd/conf.cc
93
ircd/conf.cc
|
@ -8,32 +8,9 @@
|
|||
// copyright notice and this permission notice is present in all copies. The
|
||||
// full license for this software is available in the LICENSE file.
|
||||
|
||||
namespace ircd::conf
|
||||
{
|
||||
extern const string_view default_filename;
|
||||
|
||||
std::string _config;
|
||||
static std::string read_json_file(std::string filename);
|
||||
}
|
||||
|
||||
//TODO: X
|
||||
decltype(ircd::conf::config)
|
||||
ircd::conf::config
|
||||
{
|
||||
_config
|
||||
};
|
||||
|
||||
decltype(ircd::conf::default_filename)
|
||||
ircd::conf::default_filename
|
||||
{
|
||||
"ircd.conf"
|
||||
};
|
||||
|
||||
void
|
||||
ircd::conf::init(const string_view &filename)
|
||||
{
|
||||
_config = read_json_file(std::string{filename});
|
||||
}
|
||||
decltype(ircd::conf::items)
|
||||
ircd::conf::items
|
||||
{};
|
||||
|
||||
bool
|
||||
ircd::conf::set(std::nothrow_t,
|
||||
|
@ -102,10 +79,6 @@ ircd::conf::exists(const string_view &key)
|
|||
// item
|
||||
//
|
||||
|
||||
decltype(ircd::conf::items)
|
||||
ircd::conf::items
|
||||
{};
|
||||
|
||||
/// Conf item abstract constructor.
|
||||
ircd::conf::item<void>::item(const json::members &opts)
|
||||
:feature_
|
||||
|
@ -152,66 +125,6 @@ const
|
|||
return {};
|
||||
}
|
||||
|
||||
//
|
||||
// misc
|
||||
//
|
||||
|
||||
std::string
|
||||
ircd::conf::read_json_file(std::string filename)
|
||||
try
|
||||
{
|
||||
if(!filename.empty())
|
||||
log::debug
|
||||
{
|
||||
"User supplied a configuration file path: `%s'", filename
|
||||
};
|
||||
|
||||
if(filename.empty())
|
||||
filename = fs::make_path(
|
||||
{
|
||||
fs::get(fs::CONF), conf::default_filename
|
||||
});
|
||||
|
||||
if(!fs::exists(filename))
|
||||
return {};
|
||||
|
||||
// To reduce a level of silly, we consider the conf file to be an
|
||||
// implicit object, this way it can just be a list of members.
|
||||
std::stringstream ss;
|
||||
ss << '{'
|
||||
<< fs::read(filename)
|
||||
<< '}';
|
||||
|
||||
std::string read
|
||||
{
|
||||
ss.str()
|
||||
};
|
||||
|
||||
// grammar check; throws on error
|
||||
json::valid(read);
|
||||
|
||||
const json::object object{read};
|
||||
const size_t key_count{object.count()};
|
||||
log::info
|
||||
{
|
||||
"Using configuration from: `%s': JSON object with %zu members in %zu bytes",
|
||||
filename,
|
||||
key_count,
|
||||
read.size()
|
||||
};
|
||||
|
||||
return read;
|
||||
}
|
||||
catch(const std::exception &e)
|
||||
{
|
||||
log::error
|
||||
{
|
||||
"Configuration @ `%s': %s", filename, e.what()
|
||||
};
|
||||
|
||||
throw;
|
||||
}
|
||||
|
||||
//
|
||||
// Non-inline template specialization definitions
|
||||
//
|
||||
|
|
Loading…
Reference in a new issue