diff --git a/CMakeLists.txt b/CMakeLists.txt index 2525e9b..9e4f022 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -29,6 +29,9 @@ add_executable(hello_async src/session_base.cpp src/listener.h src/listener.cpp - src/sdk.h) + src/sdk.h + src/handlers/GetMapsHandler.cpp + src/handlers/GetMapsHandler.h + src/content_type.h) target_include_directories(hello_async PUBLIC ${Boost_INCLUDE_DIR}) target_link_libraries(hello_async PRIVATE Threads::Threads Boost::filesystem Boost::json) diff --git a/main.cpp b/main.cpp index 47f2256..7fd4adf 100644 --- a/main.cpp +++ b/main.cpp @@ -10,6 +10,7 @@ #include #include "src/http_server.h" +#include "src/handlers/GetMapsHandler.h" namespace { @@ -19,6 +20,7 @@ namespace sys = boost::system; namespace json = boost::json; namespace http = boost::beast::http; using namespace std; +using namespace http_server; using net::ip::tcp; // Запрос, тело которого представлено в виде строки @@ -26,14 +28,6 @@ using StringRequest = http::request; // Ответ, тело которого представлено в виде строки using StringResponse = http::response; -struct content_type -{ - content_type() = delete; - constexpr static std::string_view k_TextHTML = "text/html"sv; - constexpr static std::string_view k_JSON = "application/json"sv; - // При необходимости внутрь content_type можно добавить и другие типы контента -}; - StringResponse HandleRequest(StringRequest&& req) { constexpr static string_view k_MapsPattern = "/api/v1/maps"sv; @@ -45,24 +39,7 @@ StringResponse HandleRequest(StringRequest&& req) route.begin(), route.end())) { - ifstream stream("./config.json"s); - string config; - string buf; - - while (std::getline(stream, buf)) - { - config += buf; - } - - string_view config_ref {config.begin(), config.end()}; - auto parsed_config = json::parse(config_ref); - StringResponse res(http::status::ok, 1); - res.set(http::field::content_type, content_type::k_JSON); - res.body() = json::serialize(parsed_config.as_object().at("maps"sv).as_array()); - res.content_length(res.body().size()); - res.keep_alive(true); - - return res; + return GetMapsHandler(req); } else if (equal(k_MapsPattern.begin(), k_MapsPattern.end(), diff --git a/src/content_type.h b/src/content_type.h new file mode 100644 index 0000000..468948b --- /dev/null +++ b/src/content_type.h @@ -0,0 +1,13 @@ +#include + +namespace http_server +{ +using namespace std; + +struct content_type +{ + content_type() = delete; + constexpr static std::string_view k_TextHTML = "text/html"sv; + constexpr static std::string_view k_JSON = "application/json"sv; +}; +} diff --git a/src/handlers/GetMapsHandler.cpp b/src/handlers/GetMapsHandler.cpp new file mode 100644 index 0000000..545dac1 --- /dev/null +++ b/src/handlers/GetMapsHandler.cpp @@ -0,0 +1,30 @@ +#include "GetMapsHandler.h" + +#include + +namespace http_server +{ +StringResponse GetMapsHandler(const StringRequest& req) +{ + ifstream stream("./config.json"s); + string config; + string buf; + + while (std::getline(stream, buf)) + { + config += buf; + } + + string_view config_ref {config.begin(), config.end()}; + auto parsed_config = json::parse(config_ref); + StringResponse res(http::status::ok, 1); + res.set(http::field::content_type, content_type::k_JSON); + res.body() = json::serialize(parsed_config.as_object().at("maps"sv).as_array()); + res.content_length(res.body().size()); + res.keep_alive(true); + + return res; +} +} + + diff --git a/src/handlers/GetMapsHandler.h b/src/handlers/GetMapsHandler.h new file mode 100644 index 0000000..e2e3c39 --- /dev/null +++ b/src/handlers/GetMapsHandler.h @@ -0,0 +1,25 @@ +#pragma once + +#include +#include +#include +#include +#include + +#include "../content_type.h" + +namespace http_server +{ +namespace net = boost::asio; +using namespace std::literals; +namespace sys = boost::system; +namespace json = boost::json; +namespace http = boost::beast::http; +using namespace std; +using net::ip::tcp; + +using StringRequest = http::request; +using StringResponse = http::response; + +StringResponse GetMapsHandler(const StringRequest&); +}