diff options
| author | Rutger Broekhoff | 2026-08-28 21:12:55 +0200 |
|---|---|---|
| committer | Rutger Broekhoff | 2026-08-28 21:12:55 +0200 |
| commit | a5d95afb96eb9a3b65d82f5f84bf8e4a4fb4c8ac (patch) | |
| tree | b4e0ec57c3799e9f649c6bfe27cb6c3313b97364 /server/src/main.cpp | |
| parent | 973aec43ea54bbf95b64fbcb636403401d1ca60e (diff) | |
| download | routemon-a5d95afb96eb9a3b65d82f5f84bf8e4a4fb4c8ac.tar.gz routemon-a5d95afb96eb9a3b65d82f5f84bf8e4a4fb4c8ac.zip | |
clang-format C++ sources
Diffstat (limited to 'server/src/main.cpp')
| -rw-r--r-- | server/src/main.cpp | 101 |
1 files changed, 67 insertions, 34 deletions
diff --git a/server/src/main.cpp b/server/src/main.cpp index a40b0b0..0a7a784 100644 --- a/server/src/main.cpp +++ b/server/src/main.cpp | |||
| @@ -1,5 +1,5 @@ | |||
| 1 | #include <malloc.h> // for malloc_trim(3) | ||
| 2 | #include <boost/asio.hpp> | 1 | #include <boost/asio.hpp> |
| 2 | #include <malloc.h> // for malloc_trim(3) | ||
| 3 | 3 | ||
| 4 | import std; | 4 | import std; |
| 5 | import routemon; | 5 | import routemon; |
| @@ -7,17 +7,23 @@ import routemon; | |||
| 7 | namespace chrono = std::chrono; | 7 | namespace chrono = std::chrono; |
| 8 | namespace net = boost::asio; | 8 | namespace net = boost::asio; |
| 9 | 9 | ||
| 10 | enum class exit_status { | 10 | enum class exit_status |
| 11 | { | ||
| 11 | failure, | 12 | failure, |
| 12 | bad_usage, | 13 | bad_usage, |
| 13 | }; | 14 | }; |
| 14 | 15 | ||
| 15 | auto real_main(std::span<char const*> args) -> exit_status { | 16 | auto real_main(std::span<char const*> args) -> exit_status |
| 17 | { | ||
| 16 | auto sink = routemon::log::make_sink(routemon::log::level::info); | 18 | auto sink = routemon::log::make_sink(routemon::log::level::info); |
| 17 | auto l = routemon::log::logger{sink}; | 19 | auto l = routemon::log::logger{sink}; |
| 18 | 20 | ||
| 19 | if (args.size() != 2) { | 21 | if (args.size() != 2) |
| 20 | l.error("Fatal: expected exactly one argument (the configuration file location), got {}", args.size() - 1); | 22 | { |
| 23 | l.error( | ||
| 24 | "Fatal: expected exactly one argument (the configuration file " | ||
| 25 | "location), got {}", | ||
| 26 | args.size() - 1); | ||
| 21 | return exit_status::bad_usage; | 27 | return exit_status::bad_usage; |
| 22 | } | 28 | } |
| 23 | auto const* config_filename = args[1]; | 29 | auto const* config_filename = args[1]; |
| @@ -25,54 +31,73 @@ auto real_main(std::span<char const*> args) -> exit_status { | |||
| 25 | auto lgen = routemon::locale::make_generator(); | 31 | auto lgen = routemon::locale::make_generator(); |
| 26 | auto default_locale = lgen->generate("en_US.UTF-8"); | 32 | auto default_locale = lgen->generate("en_US.UTF-8"); |
| 27 | auto locales = { | 33 | auto locales = { |
| 28 | default_locale, | 34 | default_locale, |
| 29 | lgen->generate("nl_NL.UTF-8"), | 35 | lgen->generate("nl_NL.UTF-8"), |
| 30 | lgen->generate("de_DE.UTF-8"), | 36 | lgen->generate("de_DE.UTF-8"), |
| 31 | lgen->generate("en_GB.UTF-8"), | 37 | lgen->generate("en_GB.UTF-8"), |
| 32 | }; | 38 | }; |
| 33 | auto lsel = routemon::locale::selector{locales, default_locale, lgen}; | 39 | auto lsel = routemon::locale::selector{locales, default_locale, lgen}; |
| 34 | 40 | ||
| 35 | auto ioc = net::io_context{1 /* concurrency hint */}; | 41 | auto ioc = net::io_context{1 /* concurrency hint */}; |
| 36 | 42 | ||
| 37 | auto config = routemon::config::app{}; | 43 | auto config = routemon::config::app{}; |
| 38 | try { | 44 | try |
| 45 | { | ||
| 39 | config = routemon::config::load_file(config_filename); | 46 | config = routemon::config::load_file(config_filename); |
| 40 | } catch (std::exception const& e) { | 47 | } |
| 41 | l.with("filename", std::string_view{config_filename}). | 48 | catch (std::exception const& e) |
| 42 | error("Failed to load configuration: {}", e.what()); | 49 | { |
| 50 | l.with("filename", std::string_view{config_filename}) | ||
| 51 | .error("Failed to load configuration: {}", e.what()); | ||
| 43 | return exit_status::failure; | 52 | return exit_status::failure; |
| 44 | } | 53 | } |
| 45 | sink->set_level(config.logger.level); | 54 | sink->set_level(config.logger.level); |
| 46 | // auto rwgps_client = routemon::rwgps::client{ioc, l, config.rwgps.api_key, config.rwgps.auth_token}; | 55 | // auto rwgps_client = routemon::rwgps::client{ioc, l, config.rwgps.api_key, |
| 47 | // for (auto route : rwgps_client.get_all_routes()) { | 56 | // config.rwgps.auth_token}; for (auto route : |
| 48 | // l.info("Route {} (user {}): {} @ {}", route.id, route.user_id, route.name, route.url); | 57 | // rwgps_client.get_all_routes()) |
| 58 | // { | ||
| 59 | // l.info("Route {} (user {}): {} @ {}", route.id, route.user_id, | ||
| 60 | // route.name, route.url); | ||
| 49 | // } | 61 | // } |
| 50 | 62 | ||
| 51 | auto dbc = std::shared_ptr<routemon::database::connection>{}; | 63 | auto dbc = std::shared_ptr<routemon::database::connection>{}; |
| 52 | try { | 64 | try |
| 65 | { | ||
| 53 | dbc = routemon::database::open(config.database.sqlite3_filename); | 66 | dbc = routemon::database::open(config.database.sqlite3_filename); |
| 54 | } catch (std::exception const& e) { | 67 | } |
| 55 | l.with("filename", config.database.sqlite3_filename). | 68 | catch (std::exception const& e) |
| 56 | error("Failed to open database: {}", e.what()); | 69 | { |
| 70 | l.with("filename", config.database.sqlite3_filename) | ||
| 71 | .error("Failed to open database: {}", e.what()); | ||
| 57 | return exit_status::failure; | 72 | return exit_status::failure; |
| 58 | } | 73 | } |
| 59 | 74 | ||
| 60 | l.with("filename", config.situations.datex2_filename). | 75 | l.with("filename", config.situations.datex2_filename) |
| 61 | info("Loading situations"); | 76 | .info("Loading situations"); |
| 62 | auto const before_load = chrono::steady_clock::now(); | 77 | auto const before_load = chrono::steady_clock::now(); |
| 63 | auto d2loader = routemon::datex2::loader{}; | 78 | auto d2loader = routemon::datex2::loader{}; |
| 64 | auto pub = routemon::datex2::situation_publication{}; | 79 | auto pub = routemon::datex2::situation_publication{}; |
| 65 | try { | 80 | try |
| 66 | pub = d2loader.load_situation_publication(config.situations.datex2_filename); | 81 | { |
| 67 | } catch (std::exception const& e) { | 82 | pub = |
| 83 | d2loader.load_situation_publication(config.situations.datex2_filename); | ||
| 84 | } | ||
| 85 | catch (std::exception const& e) | ||
| 86 | { | ||
| 68 | l.error("Failed to load DATEX II situations publication: {}", e.what()); | 87 | l.error("Failed to load DATEX II situations publication: {}", e.what()); |
| 69 | return exit_status::failure; | 88 | return exit_status::failure; |
| 70 | } | 89 | } |
| 71 | if (!d2loader.warnings().empty()) { | 90 | if (!d2loader.warnings().empty()) |
| 91 | { | ||
| 72 | auto const& warns = d2loader.warnings(); | 92 | auto const& warns = d2loader.warnings(); |
| 73 | l.warn("Encountered {} unique warnings while loading DATEX II situations publication", warns.size()); | 93 | l.warn( |
| 94 | "Encountered {} unique warnings while loading DATEX II situations " | ||
| 95 | "publication", | ||
| 96 | warns.size()); | ||
| 74 | auto i = 0uz; | 97 | auto i = 0uz; |
| 75 | for (auto it = warns.begin(); it != warns.end(); it = warns.upper_bound(*it)) { | 98 | for (auto it = warns.begin(); it != warns.end(); |
| 99 | it = warns.upper_bound(*it)) | ||
| 100 | { | ||
| 76 | l.warn("Warning {} (appeared {}×): {}", ++i, warns.count(*it), *it); | 101 | l.warn("Warning {} (appeared {}×): {}", ++i, warns.count(*it), *it); |
| 77 | } | 102 | } |
| 78 | } | 103 | } |
| @@ -82,11 +107,13 @@ auto real_main(std::span<char const*> args) -> exit_status { | |||
| 82 | // to return as much memory as possible to the OS. | 107 | // to return as much memory as possible to the OS. |
| 83 | malloc_trim(0); | 108 | malloc_trim(0); |
| 84 | auto const after_load = chrono::steady_clock::now(); | 109 | auto const after_load = chrono::steady_clock::now(); |
| 85 | auto const dur_load = chrono::duration_cast<chrono::milliseconds>(after_load - before_load); | 110 | auto const dur_load = |
| 111 | chrono::duration_cast<chrono::milliseconds>(after_load - before_load); | ||
| 86 | l.info("Loading situations finished in {}", dur_load); | 112 | l.info("Loading situations finished in {}", dur_load); |
| 87 | 113 | ||
| 88 | auto handler = routemon::api::handler{l, std::move(pub)}; | 114 | auto handler = routemon::api::handler{l, std::move(pub)}; |
| 89 | auto http_server = routemon::srv::server{l, std::move(lsel), std::move(handler)}; | 115 | auto http_server = |
| 116 | routemon::srv::server{l, std::move(lsel), std::move(handler)}; | ||
| 90 | http_server.spawn(ioc); | 117 | http_server.spawn(ioc); |
| 91 | ioc.run(); | 118 | ioc.run(); |
| 92 | 119 | ||
| @@ -94,13 +121,19 @@ auto real_main(std::span<char const*> args) -> exit_status { | |||
| 94 | return exit_status::failure; | 121 | return exit_status::failure; |
| 95 | } | 122 | } |
| 96 | 123 | ||
| 97 | auto main(int argc, char* argv[]) -> int { | 124 | auto main(int argc, char* argv[]) -> int |
| 98 | if (argc < 0) { | 125 | { |
| 126 | if (argc < 0) | ||
| 127 | { | ||
| 99 | std::cout << "Fatal: argument count below zero" << std::endl; | 128 | std::cout << "Fatal: argument count below zero" << std::endl; |
| 100 | return EXIT_FAILURE; | 129 | return EXIT_FAILURE; |
| 101 | } | 130 | } |
| 102 | auto res = real_main(std::span{const_cast<char const**>(argv), static_cast<std::size_t>(argc)}); | 131 | auto res = real_main( |
| 103 | switch (res) { | 132 | std::span{ |
| 133 | const_cast<char const**>(argv), static_cast<std::size_t>(argc) | ||
| 134 | }); | ||
| 135 | switch (res) | ||
| 136 | { | ||
| 104 | case exit_status::failure: | 137 | case exit_status::failure: |
| 105 | return EXIT_FAILURE; | 138 | return EXIT_FAILURE; |
| 106 | case exit_status::bad_usage: | 139 | case exit_status::bad_usage: |