| 1234567891011121314151617181920212223242526272829 |
- #include "config.hpp"
- #include <fstream>
- #include <stdexcept>
- namespace svapi {
- Config Config::fromJson(const nlohmann::json& j) {
- Config c;
- c.logLevel = j.value("log_level", c.logLevel);
- if (j.contains("http") && j["http"].is_object()) {
- const auto& h = j["http"];
- c.httpBind = h.value("bind_address", c.httpBind);
- c.httpPort = h.value("port", c.httpPort);
- }
- if (j.contains("database") && j["database"].is_object()) {
- c.dbAddress = j["database"].value("address", c.dbAddress);
- }
- return c;
- }
- Config Config::load(const std::string& path) {
- std::ifstream f(path);
- if (!f) throw std::runtime_error("cannot open config file: " + path);
- nlohmann::json j;
- f >> j;
- return fromJson(j);
- }
- } // namespace svapi
|