Orbit Framework 2.0.0
A high-performance asynchronous C++20 HTTP server framework.
Loading...
Searching...
No Matches
orbit::config::Config Class Reference

An application's configuration: a JSON file, then ORBIT_* environment variables, then command-line flags (later wins). More...

#include <ConfigFile.hpp>

Collaboration diagram for orbit::config::Config:

Public Member Functions

const nlohmann::json & section (const std::string &name) const
 
const nlohmann::json & document () const
 The whole document after environment overrides.
 

Static Public Member Functions

static Config load (const std::string &path, int argc=0, char *argv[]=nullptr, ServerConfig::ParseMode mode=ServerConfig::ParseMode::Lenient)
 Reads path, applies the environment, then the command line (as ServerConfig::parse(); pass argc = 0 for none).
 
static Config from_json (nlohmann::json document, int argc=0, char *argv[]=nullptr, ServerConfig::ParseMode mode=ServerConfig::ParseMode::Lenient)
 As load(), from a document already in memory (tests, generated config).
 
static ServerConfig server_from_json (const nlohmann::json &server_section)
 The server section as a ServerConfig (strictly validated).
 

Public Attributes

ServerConfig server
 

Private Attributes

nlohmann::json document_ = nlohmann::json::object()
 

Detailed Description

An application's configuration: a JSON file, then ORBIT_* environment variables, then command-line flags (later wins).

The "server" section becomes a ServerConfig; its keys are the ServerConfig field names, with durations in seconds and the engine ("epoll", "iouring", "auto") and HTTP version ("1.1", "2", "3") as strings. Every other section is the application's, read with section().

// orbit.json
{
"server": { "port": 8080, "worker_threads": "auto", "header_timeout": 5 },
"database": { "url": "postgres://localhost/app", "pool_size": 8 }
}
auto cfg = orbit::config::Config::load("orbit.json", argc, argv);
orbit::server::App app(cfg.server);
auto db_url = cfg.section("database").value("url", "");
static Config load(const std::string &path, int argc=0, char *argv[]=nullptr, ServerConfig::ParseMode mode=ServerConfig::ParseMode::Lenient)
Reads path, applies the environment, then the command line (as ServerConfig::parse(); pass argc = 0 f...
The main application class for the Orbit Framework.
Definition App.hpp:36

Environment variables:

  • ORBIT_<KEY> sets server.<key> for a key the server section knows (ORBIT_PORT=9000, ORBIT_LOG_LEVEL=DEBUG); others are ignored.
  • ORBIT_<SECTION>__<KEY> (double underscore between levels) sets any path (ORBIT_DATABASE__URL=..., ORBIT_SERVER__PORT=9000). Names are matched case-insensitively. A value that parses as JSON (a number, true/false, an array) is used as such, anything else as a string.

Validation is strict: an unknown key in the server section, or a value of the wrong type or out of range, throws ConfigError naming the key.

Member Function Documentation

◆ document()

const nlohmann::json & orbit::config::Config::document ( ) const
inline

The whole document after environment overrides.

◆ from_json()

static Config orbit::config::Config::from_json ( nlohmann::json  document,
int  argc = 0,
char *  argv[] = nullptr,
ServerConfig::ParseMode  mode = ServerConfig::ParseMode::Lenient 
)
static

As load(), from a document already in memory (tests, generated config).

◆ load()

static Config orbit::config::Config::load ( const std::string &  path,
int  argc = 0,
char *  argv[] = nullptr,
ServerConfig::ParseMode  mode = ServerConfig::ParseMode::Lenient 
)
static

Reads path, applies the environment, then the command line (as ServerConfig::parse(); pass argc = 0 for none).

Exceptions
ConfigErrorif the file cannot be read or parsed, or is invalid.

◆ section()

const nlohmann::json & orbit::config::Config::section ( const std::string &  name) const

An application section, with environment overrides applied; an empty object if the document has none.

◆ server_from_json()

static ServerConfig orbit::config::Config::server_from_json ( const nlohmann::json &  server_section)
static

The server section as a ServerConfig (strictly validated).

Member Data Documentation

◆ document_

nlohmann::json orbit::config::Config::document_ = nlohmann::json::object()
private

◆ server

ServerConfig orbit::config::Config::server

The documentation for this class was generated from the following file: