From d3a3f304ee30bb83b640e15d033d20f20042ff79 Mon Sep 17 00:00:00 2001 From: "Scott E. Graves" Date: Sat, 22 Feb 2025 04:08:07 -0600 Subject: [PATCH] added test server command --- monitarr/libmonitarr/include/actions.hpp | 2 + monitarr/libmonitarr/src/actions.cpp | 110 ++++++++++++++++++----- monitarr/monitarr/include/test_cmd.hpp | 33 +++++++ monitarr/monitarr/main.cpp | 3 + monitarr/monitarr/src/run_cmd.cpp | 21 +++++ monitarr/monitarr/src/show_cmd.cpp | 21 +++++ monitarr/monitarr/src/test_cmd.cpp | 41 +++++++++ monitarr/monitarr/src/usage_cmd.cpp | 25 +++++- 8 files changed, 231 insertions(+), 25 deletions(-) create mode 100644 monitarr/monitarr/include/test_cmd.hpp create mode 100644 monitarr/monitarr/src/test_cmd.cpp diff --git a/monitarr/libmonitarr/include/actions.hpp b/monitarr/libmonitarr/include/actions.hpp index 2a5b859..6ac1d15 100644 --- a/monitarr/libmonitarr/include/actions.hpp +++ b/monitarr/libmonitarr/include/actions.hpp @@ -45,6 +45,8 @@ class data_db; void remove_stalled(std::string_view download_id, std::string_view title, std::uint64_t episode_id, std::uint64_t movie_id, const server_cfg &server, data_db *state_db = nullptr); + +[[nodiscard]] auto test_server(const server_cfg &server) -> int; } // namespace monitarr #endif // LIBMONITARR_INCLUDE_ACTIONS_HPP_ diff --git a/monitarr/libmonitarr/src/actions.cpp b/monitarr/libmonitarr/src/actions.cpp index 1b001b4..72fd596 100644 --- a/monitarr/libmonitarr/src/actions.cpp +++ b/monitarr/libmonitarr/src/actions.cpp @@ -171,6 +171,48 @@ auto load_config(std::string &cfg_file) -> app_config { return cfg; } + std::for_each( + cfg.server_list.begin(), cfg.server_list.end(), [&](server_cfg &srv) { + utils::string::trim(srv.api_key); + if (srv.api_key.empty()) { + throw utils::error::create_exception( + function_name, { + "server 'api_key' must not be empty", + }); + } + + utils::string::trim(srv.api_version); + if (srv.api_version.empty()) { + throw utils::error::create_exception( + function_name, { + "server 'api_version' must not be empty", + }); + } + + utils::string::trim(srv.id); + if (srv.id.empty()) { + throw utils::error::create_exception( + function_name, { + "server 'id' must not be empty", + }); + } + + if (srv.timeout.count() <= 0) { + throw utils::error::create_exception( + function_name, { + "server 'timeout' must be greater than 0", + }); + } + + utils::string::trim(srv.url); + if (srv.url.empty()) { + throw utils::error::create_exception( + function_name, { + "server 'url' must not be empty", + }); + } + }); + { auto iter = std::ranges::adjacent_find( cfg.server_list, @@ -202,30 +244,27 @@ auto load_config(std::string &cfg_file) -> app_config { }); } - // std::ranges::for_each(id_names, [&](std::string_view name) { - // std::ranges::for_each(cfg.server_list, [&](auto &&srv) { - // auto count = std::ranges::count(srv.id, name); - // }); - // auto count = std::ranges::count( - // std::accumulate(cfg.server_list.begin(), cfg.server_list.end(), - // std::vector(), - // [](auto &&list, auto &&srv) { - // list.push_back(srv.id); - // return list; - // }), - // name); - // if (count <= 1U) { - // return; - // } - // - // throw utils::error::create_exception( - // function_name, { - // "server id", - // name, - // "must contain only one of the following - // values", fmt::format("{}", id_names), - // }); - // }); + std::ranges::for_each(cfg.server_list, [&](const auto &srv) { + std::size_t count{}; + std::ranges::for_each(id_names, [&count, &srv](std::string_view name) { + for (std::size_t pos = 0U; + (pos = srv.id.find(name, pos)) != std::string::npos; ++pos) { + ++count; + } + }); + + if (count == 1U) { + return; + } + + throw utils::error::create_exception( + function_name, { + "server id", + srv.id, + "must contain only one of the following values", + fmt::format("{}", id_names), + }); + }); } return cfg; @@ -329,4 +368,27 @@ void remove_stalled(std::string_view download_id, std::string_view title, server.id, server.url, title, search_id, response->status)); } + +auto test_server(const server_cfg &server) -> int { + MONITARR_USES_FUNCTION_NAME(); + + auto cli = create_client(server); + + auto response = cli.Get("/api"); + if (not response) { + utils::error::handle_error( + function_name, fmt::format("get api request failed|{}|{}|no response", + server.id, server.url)); + return 3; + } + + if (response->status != httplib::StatusCode::OK_200) { + utils::error::handle_error( + function_name, fmt::format("get api request failed|{}|{}|{}", server.id, + server.url, response->status)); + return 3; + } + + return 0; +} } // namespace monitarr diff --git a/monitarr/monitarr/include/test_cmd.hpp b/monitarr/monitarr/include/test_cmd.hpp new file mode 100644 index 0000000..ea27987 --- /dev/null +++ b/monitarr/monitarr/include/test_cmd.hpp @@ -0,0 +1,33 @@ +/* + Copyright <2018-2025> + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. +*/ +#ifndef LIBMONITARR_INCLUDE_TEST_CMD_HPP_ +#define LIBMONITARR_INCLUDE_TEST_CMD_HPP_ + +#include "utils/config.hpp" + +namespace monitarr { +struct app_config; + +[[nodiscard]] auto test_cmd(const app_config &cfg) -> int; +} // namespace monitarr + +#endif // LIBMONITARR_INCLUDE_TEST_CMD_HPP_ diff --git a/monitarr/monitarr/main.cpp b/monitarr/monitarr/main.cpp index b80639f..5b690b6 100644 --- a/monitarr/monitarr/main.cpp +++ b/monitarr/monitarr/main.cpp @@ -33,6 +33,7 @@ #include "run_cmd.hpp" #include "settings.hpp" #include "show_cmd.hpp" +#include "test_cmd.hpp" #include "usage_cmd.hpp" #include "utils/common.hpp" #include "utils/file.hpp" @@ -101,6 +102,8 @@ auto main(int argc, char **argv) -> int { ret = run_cmd(cfg, log_dir); } else if (has_arg("-s", argc, argv)) { ret = show_cmd(argc, argv, cfg); + } else if (has_arg("-t", argc, argv)) { + ret = test_cmd(cfg); } else { ret = usage_cmd(); if (ret == 0) { diff --git a/monitarr/monitarr/src/run_cmd.cpp b/monitarr/monitarr/src/run_cmd.cpp index e299675..0e4b168 100644 --- a/monitarr/monitarr/src/run_cmd.cpp +++ b/monitarr/monitarr/src/run_cmd.cpp @@ -1,3 +1,24 @@ +/* + Copyright <2018-2025> + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. +*/ #include #include "run_cmd.hpp" diff --git a/monitarr/monitarr/src/show_cmd.cpp b/monitarr/monitarr/src/show_cmd.cpp index 6c0c5ef..267720d 100644 --- a/monitarr/monitarr/src/show_cmd.cpp +++ b/monitarr/monitarr/src/show_cmd.cpp @@ -1,3 +1,24 @@ +/* + Copyright <2018-2025> + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. +*/ #include "show_cmd.hpp" #include "actions.hpp" diff --git a/monitarr/monitarr/src/test_cmd.cpp b/monitarr/monitarr/src/test_cmd.cpp new file mode 100644 index 0000000..1ebfc8b --- /dev/null +++ b/monitarr/monitarr/src/test_cmd.cpp @@ -0,0 +1,41 @@ +/* + Copyright <2018-2025> + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. +*/ +#include "test_cmd.hpp" + +#include "actions.hpp" +#include "settings.hpp" + +namespace monitarr { +auto test_cmd(const app_config &cfg) -> int { + MONITARR_USES_FUNCTION_NAME(); + + auto ret{0}; + for (const auto &srv : cfg.server_list) { + auto test_ret = test_server(srv); + if (ret == 0 && test_ret != 0) { + ret = test_ret; + } + } + + return ret; +} +} // namespace monitarr diff --git a/monitarr/monitarr/src/usage_cmd.cpp b/monitarr/monitarr/src/usage_cmd.cpp index c314b64..c941d49 100644 --- a/monitarr/monitarr/src/usage_cmd.cpp +++ b/monitarr/monitarr/src/usage_cmd.cpp @@ -1,3 +1,24 @@ +/* + Copyright <2018-2025> + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. +*/ #include "usage_cmd.hpp" #include "utils/error.hpp" @@ -18,7 +39,9 @@ auto usage_cmd() -> int { " monitarr -r\n" " run monitarr server\n" " monitarr -s -i -id \n" - " show record id details at configuration index\n"); + " show record id details at configuration index\n" + " monitarr -t\n" + " test all server connectivity\n"); return 0; }