diff --git a/CMakeLists.txt b/CMakeLists.txt index 9e4f022..fdcf98f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -32,6 +32,9 @@ add_executable(hello_async src/sdk.h src/handlers/GetMapsHandler.cpp src/handlers/GetMapsHandler.h - src/content_type.h) + src/content_type.h + src/handlers/GetMapByIdHandler.cpp + src/handlers/GetMapByIdHandler.h + src/routes.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 7fd4adf..297fac8 100644 --- a/main.cpp +++ b/main.cpp @@ -10,7 +10,9 @@ #include #include "src/http_server.h" +#include "src/routes.h" #include "src/handlers/GetMapsHandler.h" +#include "src/handlers/GetMapByIdHandler.h" namespace { @@ -30,66 +32,21 @@ using StringResponse = http::response; StringResponse HandleRequest(StringRequest&& req) { - constexpr static string_view k_MapsPattern = "/api/v1/maps"sv; - constexpr static string_view k_StaticAssetsPattern = "/public"sv; const auto route = req.target(); - if (equal(k_MapsPattern.begin(), - k_MapsPattern.end(), + if (equal(k_MapsRoute.begin(), + k_MapsRoute.end(), route.begin(), route.end())) { return GetMapsHandler(req); } - else if (equal(k_MapsPattern.begin(), - k_MapsPattern.end(), + else if (equal(k_MapsRoute.begin(), + k_MapsRoute.end(), route.begin(), - route.begin() + k_MapsPattern.size())) + route.begin() + k_MapsRoute.size())) { - 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); - string_view map_id {route.begin() + k_MapsPattern.size() + 1, route.end()}; - StringResponse res(http::status::ok, 1); - res.set(http::field::content_type, content_type::k_JSON); - - const auto& maps = parsed_config.as_object().at("maps"sv).as_array(); - - const auto finded = find_if(maps.begin(), maps.end(), [&map_id](auto& elem) -> bool - { - const auto& value = elem.as_object().at("id"sv).as_string(); - - return value == map_id; - }); - - if (finded == maps.end()) - { - StringResponse res(http::status::bad_request, 1); - res.set(http::field::content_type, content_type::k_JSON); - string body = "{\n" - " \"code\": \"mapNotFound\",\n" - " \"message\": \"Map not found\"\n" - "}"s; - res.body() = body; - res.content_length(body.size()); - res.keep_alive(true); - - return res; - } - - res.body() = json::serialize(*finded); - res.content_length(res.body().size()); - res.keep_alive(true); - - return res; + return GetMapByIdHandler(req); } else { diff --git a/src/content_type.h b/src/content_type.h index 468948b..8b341c6 100644 --- a/src/content_type.h +++ b/src/content_type.h @@ -1,3 +1,5 @@ +#pragma once + #include namespace http_server @@ -7,7 +9,7 @@ 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; + constexpr inline static std::string_view k_TextHTML = "text/html"sv; + constexpr inline static std::string_view k_JSON = "application/json"sv; }; } diff --git a/src/handlers/GetMapByIdHandler.cpp b/src/handlers/GetMapByIdHandler.cpp new file mode 100644 index 0000000..10cba79 --- /dev/null +++ b/src/handlers/GetMapByIdHandler.cpp @@ -0,0 +1,56 @@ +#include + +#include "../routes.h" +#include "GetMapByIdHandler.h" + +namespace http_server +{ +StringResponse GetMapByIdHandler(const StringRequest& req) +{ + const auto route = req.target(); + 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); + string_view map_id {route.begin() + k_MapsRoute.size() + 1, route.end()}; + StringResponse res(http::status::ok, 1); + res.set(http::field::content_type, content_type::k_JSON); + + const auto& maps = parsed_config.as_object().at("maps"sv).as_array(); + + const auto finded = find_if(maps.begin(), maps.end(), [&map_id](auto& elem) -> bool + { + const auto& value = elem.as_object().at("id"sv).as_string(); + + return value == map_id; + }); + + if (finded == maps.end()) + { + StringResponse res(http::status::bad_request, 1); + res.set(http::field::content_type, content_type::k_JSON); + string body = "{\n" + " \"code\": \"mapNotFound\",\n" + " \"message\": \"Map not found\"\n" + "}"s; + res.body() = body; + res.content_length(body.size()); + res.keep_alive(true); + + return res; + } + + res.body() = json::serialize(*finded); + res.content_length(res.body().size()); + res.keep_alive(true); + + return res; +} +} diff --git a/src/handlers/GetMapByIdHandler.h b/src/handlers/GetMapByIdHandler.h new file mode 100644 index 0000000..bff194e --- /dev/null +++ b/src/handlers/GetMapByIdHandler.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 GetMapByIdHandler(const StringRequest&); +} diff --git a/src/routes.h b/src/routes.h new file mode 100644 index 0000000..0a05eaa --- /dev/null +++ b/src/routes.h @@ -0,0 +1,12 @@ +#pragma once + +#include + +namespace http_server +{ +using namespace std; + +constexpr static string_view k_MapsRoute = "/api/v1/maps"sv; +constexpr static string_view k_StaticAssetsPRoute = "/public"sv; + +}