summaryrefslogtreecommitdiffstats
path: root/server/src/config.cpp
diff options
context:
space:
mode:
authorRutger Broekhoff2026-08-28 23:29:00 +0200
committerRutger Broekhoff2026-08-28 23:29:00 +0200
commit35878b76049b9c3e752480e9f298b7e2da6fdf1f (patch)
tree034bd4bcbbd0445e8f3a13dba0b7d517807bbaaa /server/src/config.cpp
parentb0d7600970d2fdb1eb6fff813c7336ea34d4e228 (diff)
downloadroutemon-35878b76049b9c3e752480e9f298b7e2da6fdf1f.tar.gz
routemon-35878b76049b9c3e752480e9f298b7e2da6fdf1f.zip
Make RWGPS config optional
Diffstat (limited to 'server/src/config.cpp')
-rw-r--r--server/src/config.cpp40
1 files changed, 29 insertions, 11 deletions
diff --git a/server/src/config.cpp b/server/src/config.cpp
index eab97b5..5c1a161 100644
--- a/server/src/config.cpp
+++ b/server/src/config.cpp
@@ -23,9 +23,9 @@ class location
23 { 23 {
24 if (next_) 24 if (next_)
25 { 25 {
26 next_->second->append_to(s);
26 s += next_->first; 27 s += next_->first;
27 s += "."; 28 s += ".";
28 next_->second->append_to(s);
29 } 29 }
30 } 30 }
31 31
@@ -41,8 +41,9 @@ public:
41 { 41 {
42 if (!next_) 42 if (!next_)
43 return ""; 43 return "";
44 auto s = std::string{next_->first}; 44 auto s = std::string{};
45 next_->second->append_to(s); 45 next_->second->append_to(s);
46 s += next_->first;
46 return s; 47 return s;
47 } 48 }
48 49
@@ -80,11 +81,11 @@ public:
80 } 81 }
81 82
82 template <class T> 83 template <class T>
83 auto expect_at(std::string_view key) -> T 84 auto allow_at(std::string_view key) -> std::optional<T>
84 { 85 {
85 visited_.emplace(key); 86 visited_.emplace(key);
86 auto const loc = loc_.sub(key); 87 auto const loc = loc_.sub(key);
87 if (auto const jv = obj_.try_at(key)) 88 if (auto const jv = obj_.try_at(key); jv && !jv->is_null())
88 { 89 {
89 try 90 try
90 { 91 {
@@ -98,8 +99,21 @@ public:
98 } 99 }
99 else 100 else
100 { 101 {
102 return std::nullopt;
103 }
104 }
105
106 template <class T>
107 auto expect_at(std::string_view key) -> T
108 {
109 if (auto&& mv = allow_at<T>(key); mv)
110 {
111 return std::forward<T>(*mv);
112 }
113 else
114 {
101 throw std::runtime_error{std::format( 115 throw std::runtime_error{std::format(
102 "did not find expected key {}", loc.to_string())}; 116 "did not find expected key {}", loc_.sub(key).to_string())};
103 } 117 }
104 } 118 }
105}; 119};
@@ -124,8 +138,9 @@ auto tag_invoke(
124 [](object_reader& r) -> rwgps 138 [](object_reader& r) -> rwgps
125 { 139 {
126 return { 140 return {
127 .api_key = r.expect_at<std::string>("api_key"), 141 .api_key_filename = r.expect_at<std::string>("api_key_filename"),
128 .auth_token = r.expect_at<std::string>("auth_token"), 142 .auth_token_filename =
143 r.expect_at<std::string>("auth_token_filename"),
129 }; 144 };
130 }); 145 });
131} 146}
@@ -206,7 +221,7 @@ auto json_value_to_app(json::value const& jv) -> app
206 [](object_reader& r) -> app 221 [](object_reader& r) -> app
207 { 222 {
208 return { 223 return {
209 .rwgps = r.expect_at<rwgps>("rwgps"), 224 .rwgps = r.allow_at<rwgps>("rwgps"),
210 .situations = r.expect_at<situations>("situations"), 225 .situations = r.expect_at<situations>("situations"),
211 .database = r.expect_at<database>("database"), 226 .database = r.expect_at<database>("database"),
212 .http_server = r.expect_at<http_server>("http_server"), 227 .http_server = r.expect_at<http_server>("http_server"),
@@ -217,9 +232,12 @@ auto json_value_to_app(json::value const& jv) -> app
217 232
218auto load_file(std::string const& filename) -> app 233auto load_file(std::string const& filename) -> app
219{ 234{
220 auto f = std::ifstream{ 235 // TODO: migrate away from json::value_to w/ tag_invoke.
221 filename 236 // Boost.JSON does not properly propagate custom exceptions accross
222 }; // TODO: ensure that we are opening in binary mode? 237 // json::value_to calls when certain containers are involved
238 // (std::{optional,variant,vector}).
239
240 auto f = std::ifstream{filename, std::ios::binary};
223 if (!f.is_open()) 241 if (!f.is_open())
224 throw std::runtime_error{std::format("failed to open {}", filename)}; 242 throw std::runtime_error{std::format("failed to open {}", filename)};
225 auto jv = json::value{}; 243 auto jv = json::value{};