| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- #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);
- c.httpThreadPoolSize = h.value("thread_pool_size", c.httpThreadPoolSize);
- }
- if (j.contains("database") && j["database"].is_object()) {
- c.dbAddress = j["database"].value("address", c.dbAddress);
- c.dbTimeoutMs = j["database"].value("timeout_ms", c.dbTimeoutMs);
- }
- if (j.contains("functions") && j["functions"].is_object()) {
- const auto& f = j["functions"];
- if (f.contains("runners") && f["runners"].is_array()) {
- // Bind before iterating: a range-for over a temporary subobject of a
- // parsed document dangles.
- const nlohmann::json& runners = f["runners"];
- for (const auto& e : runners)
- if (e.is_string() && !e.get<std::string>().empty())
- c.runnerEndpoints.push_back(e.get<std::string>());
- }
- c.functionRunsKept = f.value("runs_kept", c.functionRunsKept);
- c.runnerToken = f.value("runner_token", c.runnerToken);
- if (f.contains("scheduler") && f["scheduler"].is_object()) {
- const auto& s = f["scheduler"];
- c.schedulerEnabled = s.value("enabled", c.schedulerEnabled);
- c.schedulerTickSec = s.value("tick_interval_sec", c.schedulerTickSec);
- c.reclaimIntervalSec = s.value("reclaim_interval_sec", c.reclaimIntervalSec);
- }
- if (f.contains("jobs") && f["jobs"].is_object()) {
- const auto& j2 = f["jobs"];
- c.claimTimeoutSec = j2.value("claim_timeout_sec", c.claimTimeoutSec);
- c.jobMaxAttempts = j2.value("max_attempts", c.jobMaxAttempts);
- }
- if (f.contains("events") && f["events"].is_object()) {
- const auto& e = f["events"];
- c.eventsEnabled = e.value("enabled", c.eventsEnabled);
- c.eventsMaxDepth = e.value("max_depth", c.eventsMaxDepth);
- c.eventsRefreshSec = e.value("refresh_interval_sec", c.eventsRefreshSec);
- c.eventsMaxPending = e.value("max_pending", c.eventsMaxPending);
- }
- }
- 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
|