summaryrefslogtreecommitdiffstats
path: root/server/src/main.cpp
diff options
context:
space:
mode:
authorRutger Broekhoff2026-11-30 11:47:36 +0100
committerRutger Broekhoff2026-11-30 11:47:36 +0100
commit02475e05ccd1aa15c6a6163834f28bfebbef796f (patch)
tree6bc6f453f07dc76f763310e1e5eade9a3e1115b7 /server/src/main.cpp
parent5732f6c14618f50ffaf315b93795340219a67c66 (diff)
downloadroutemon-02475e05ccd1aa15c6a6163834f28bfebbef796f.tar.gz
routemon-02475e05ccd1aa15c6a6163834f28bfebbef796f.zip
RWGPS webhook and such
Diffstat (limited to 'server/src/main.cpp')
-rw-r--r--server/src/main.cpp219
1 files changed, 194 insertions, 25 deletions
diff --git a/server/src/main.cpp b/server/src/main.cpp
index 3ef45c6..df750d9 100644
--- a/server/src/main.cpp
+++ b/server/src/main.cpp
@@ -1,5 +1,6 @@
1#include <boost/asio.hpp> 1#include <boost/asio.hpp>
2#include <malloc.h> // for malloc_trim(3) 2#include <malloc.h> // for malloc_trim(3)
3#include <openssl/crypto.h>
3 4
4import std; 5import std;
5import routemon; 6import routemon;
@@ -7,27 +8,79 @@ import routemon;
7namespace chrono = std::chrono; 8namespace chrono = std::chrono;
8namespace net = boost::asio; 9namespace net = boost::asio;
9 10
11using namespace std::literals::string_view_literals;
12
10enum class exit_status 13enum class exit_status
11{ 14{
15 success,
12 failure, 16 failure,
13 bad_usage, 17 bad_usage,
14}; 18};
15 19
16auto real_main(std::span<char const*> args) -> exit_status 20struct secure_buf
17{ 21{
18 auto sink = routemon::log::make_sink(routemon::log::level::info); 22 void* p;
19 auto l = routemon::log::logger{sink}; 23 std::size_t size = 0;
20 24
21 if (args.size() != 2) 25 secure_buf(secure_buf const&) = delete;
26 secure_buf(secure_buf&&) = delete;
27
28 auto operator=(secure_buf const&) -> secure_buf& = delete;
29 auto operator=(secure_buf&&) -> secure_buf& = delete;
30
31 secure_buf(std::size_t size) : p{OPENSSL_malloc(size)}
22 { 32 {
23 l.error( 33 if (!this->p)
24 "Fatal: expected exactly one argument (the configuration file " 34 throw std::bad_alloc{};
25 "location), got {}",
26 args.size() - 1);
27 return exit_status::bad_usage;
28 } 35 }
29 auto const* config_filename = args[1];
30 36
37 ~secure_buf() { OPENSSL_clear_free(this->p, this->size); }
38};
39
40auto ltrim_ws(std::string_view s) -> std::string_view
41{
42 if (auto i = s.find_first_not_of(" \t\r\n"); i != std::string_view::npos)
43 s.remove_prefix(i);
44 return s;
45}
46auto rtrim_ws(std::string_view s) -> std::string_view
47{
48 if (auto i = s.find_last_not_of(" \t\r\n"); i != std::string_view::npos)
49 return s.substr(0, i + 1);
50 return s;
51}
52auto trim_ws(std::string_view s) -> std::string_view
53{
54 return rtrim_ws(ltrim_ws(s));
55}
56
57// TODO: make own secure string type using the OpenSSL primitives
58template <std::size_t max_size>
59auto read_secret_to_string(std::string const& filename) -> std::string
60{
61 static_assert(max_size <= 65536); // sounds reasonable enough?
62
63 auto f = std::ifstream{filename, std::ios::binary};
64 if (!f)
65 throw std::system_error{
66 errno, std::generic_category(),
67 std::format("opening secret located at {}", filename)
68 };
69 auto buf_cap = max_size;
70 auto buf = secure_buf{max_size};
71 f.read(static_cast<char*>(buf.p), static_cast<std::streamsize>(buf_cap));
72 buf.size = static_cast<std::size_t>(f.gcount());
73 f.seekg(0, std::ios::end);
74 if (f.peek() != std::ifstream::traits_type::eof())
75 throw std::runtime_error{"secret is bigger than allowed"};
76
77 return std::string{trim_ws(
78 std::string_view{static_cast<char const*>(buf.p), buf.size})};
79}
80
81auto server_main(routemon::log::logger& l, routemon::config::app config)
82 -> exit_status
83{
31 auto lgen = routemon::locale::make_generator(); 84 auto lgen = routemon::locale::make_generator();
32 auto default_locale = lgen->generate("en_US.UTF-8"); 85 auto default_locale = lgen->generate("en_US.UTF-8");
33 auto locales = { 86 auto locales = {
@@ -39,28 +92,32 @@ auto real_main(std::span<char const*> args) -> exit_status
39 92
40 auto ioc = net::io_context{1 /* concurrency hint */}; 93 auto ioc = net::io_context{1 /* concurrency hint */};
41 94
42 auto config = routemon::config::app{}; 95 auto dbc = std::shared_ptr<routemon::database::connection>{};
43 try 96 try
44 { 97 {
45 config = routemon::config::load_file(config_filename); 98 dbc = routemon::database::open(config.database.sqlite3_filename);
46 } 99 }
47 catch (std::exception const& e) 100 catch (std::exception const& e)
48 { 101 {
49 l.with("filename", std::string_view{config_filename}) 102 l.with("filename", config.database.sqlite3_filename)
50 .error("Failed to load configuration: {}", e.what()); 103 .error("Failed to open database: {}", e.what());
51 return exit_status::failure; 104 return exit_status::failure;
52 } 105 }
53 sink->set_level(config.logger.level);
54 106
55 auto dbc = std::shared_ptr<routemon::database::connection>{}; 107 auto rwgps_creds = std::shared_ptr<routemon::rwgps::credentials>{};
56 try 108 try
57 { 109 {
58 dbc = routemon::database::open(config.database.sqlite3_filename); 110 rwgps_creds.reset(new routemon::rwgps::credentials{
111 // TODO: bad secrets handling. Too little information for proper error
112 // reporting.
113 read_secret_to_string<4096>(config.rwgps.api_key_filename),
114 read_secret_to_string<4096>(config.rwgps.api_secret_filename),
115 read_secret_to_string<4096>(config.rwgps.auth_token_filename),
116 });
59 } 117 }
60 catch (std::exception const& e) 118 catch (std::exception const& e)
61 { 119 {
62 l.with("filename", config.database.sqlite3_filename) 120 l.error("Failed to read RWGPS credentials: {}", e.what());
63 .error("Failed to open database: {}", e.what());
64 return exit_status::failure; 121 return exit_status::failure;
65 } 122 }
66 123
@@ -94,7 +151,7 @@ auto real_main(std::span<char const*> args) -> exit_status
94 } 151 }
95 } 152 }
96 // Processing the feed is by far the most memory-intensive operation 153 // Processing the feed is by far the most memory-intensive operation
97 // during the run time of this application (at the moment), the 154 // during the run time of this application (at the moment); the
98 // resident set will likely never be this big again. So we ask libc 155 // resident set will likely never be this big again. So we ask libc
99 // to return as much memory as possible to the OS. 156 // to return as much memory as possible to the OS.
100 malloc_trim(0); 157 malloc_trim(0);
@@ -103,9 +160,9 @@ auto real_main(std::span<char const*> args) -> exit_status
103 chrono::duration_cast<chrono::milliseconds>(after_load - before_load); 160 chrono::duration_cast<chrono::milliseconds>(after_load - before_load);
104 l.info("Loading situations finished in {}", dur_load); 161 l.info("Loading situations finished in {}", dur_load);
105 162
106 auto handler = routemon::api::handler{l, std::move(pub)}; 163 auto handler = routemon::api::handler{l, dbc, std::move(pub)};
107 auto http_server = routemon::srv::server{ 164 auto http_server = routemon::srv::server{
108 l, config.http_server, std::move(lsel), std::move(handler) 165 l, config.http_server, rwgps_creds, std::move(lsel), std::move(handler),
109 }; 166 };
110 http_server.spawn(ioc); 167 http_server.spawn(ioc);
111 ioc.run(); 168 ioc.run();
@@ -114,19 +171,131 @@ auto real_main(std::span<char const*> args) -> exit_status
114 return exit_status::failure; 171 return exit_status::failure;
115} 172}
116 173
174auto migrate_main(
175 routemon::log::logger& l, routemon::config::app config,
176 std::int64_t target_db_version) -> exit_status
177{
178 auto m = std::unique_ptr<routemon::migrate::migrator>{};
179 try
180 {
181 m.reset(
182 new routemon::migrate::migrator{l, config.database.sqlite3_filename});
183 }
184 catch (std::exception const& e)
185 {
186 l.with("filename", config.database.sqlite3_filename)
187 .error("Failed to open database: {}", e.what());
188 return exit_status::failure;
189 }
190
191 try
192 {
193 m->migrate_to(target_db_version);
194 }
195 catch (std::exception const& e)
196 {
197 l.error("Failed to migrate database: {}", e.what());
198 return exit_status::failure;
199 }
200
201 return exit_status::success;
202}
203
204// Arguments not including own name (so argv[1..]).
205auto real_main(std::span<char const*> args) -> exit_status
206{
207 auto const sink = routemon::log::make_sink(routemon::log::level::info);
208 auto l = routemon::log::logger{sink};
209
210 auto load_config = [&](char const* config_filename)
211 -> std::expected<routemon::config::app, exit_status>
212 {
213 try
214 {
215 auto const config = routemon::config::load_file(config_filename);
216 sink->set_level(config.logger.level);
217 return config;
218 }
219 catch (std::exception const& e)
220 {
221 l.with("filename", std::string_view{config_filename})
222 .error("Failed to load configuration: {}", e.what());
223 return std::unexpected{exit_status::failure};
224 }
225 };
226
227 if (args.size() <= 0)
228 {
229 l.error(
230 "Fatal: expected at least a subcommand argument (∈ {{migrate, "
231 "server}}), got {} arguments",
232 args.size());
233 return exit_status::bad_usage;
234 }
235
236 if (args[0] == "server"sv)
237 {
238 args = args.subspan<1>();
239 if (args.size() != 1)
240 {
241 l.error(
242 "Fatal: expected exactly one argument (the configuration file "
243 "location), got {}",
244 args.size());
245 return exit_status::bad_usage;
246 }
247 auto mconfig = load_config(args[0]);
248 if (!mconfig)
249 return mconfig.error();
250 return server_main(l, std::move(*mconfig));
251 }
252 else if (args[0] == "migrate"sv)
253 {
254 args = args.subspan<1>();
255 if (args.size() != 2)
256 {
257 l.error(
258 "Fatal: expected exactly two arguments (the configuration file "
259 "location and database version to migrate to), got {}",
260 args.size());
261 return exit_status::bad_usage;
262 }
263 auto mconfig = load_config(args[0]);
264 if (!mconfig)
265 return mconfig.error();
266 auto mtarget_db_version = routemon::util::parse_int<std::int64_t>(args[1]);
267 if (!mtarget_db_version || *mtarget_db_version < 0)
268 {
269 l.error(
270 "Fatal: could not parse target database version ({}) as a "
271 "non-negative integer",
272 args[1]);
273 return exit_status::bad_usage;
274 }
275 return migrate_main(l, std::move(*mconfig), *mtarget_db_version);
276 }
277 else
278 {
279 l.error("Fatal: unrecognized subcommand {}", args[0]);
280 return exit_status::bad_usage;
281 }
282}
283
117auto main(int argc, char* argv[]) -> int 284auto main(int argc, char* argv[]) -> int
118{ 285{
119 if (argc < 0) 286 if (argc < 1)
120 { 287 {
121 std::cout << "Fatal: argument count below zero" << std::endl; 288 std::cout << "Fatal: argument count below one" << std::endl;
122 return EXIT_FAILURE; 289 return EXIT_FAILURE;
123 } 290 }
124 auto res = real_main( 291 auto res = real_main(
125 std::span{ 292 std::span{
126 const_cast<char const**>(argv), static_cast<std::size_t>(argc) 293 const_cast<char const**>(argv + 1), static_cast<std::size_t>(argc - 1)
127 }); 294 });
128 switch (res) 295 switch (res)
129 { 296 {
297 case exit_status::success:
298 return EXIT_SUCCESS;
130 case exit_status::failure: 299 case exit_status::failure:
131 return EXIT_FAILURE; 300 return EXIT_FAILURE;
132 case exit_status::bad_usage: 301 case exit_status::bad_usage: