move handler to special function
This commit is contained in:
parent
d647b2af5d
commit
61edce0ecb
|
@ -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)
|
||||
|
|
59
main.cpp
59
main.cpp
|
@ -10,7 +10,9 @@
|
|||
#include <fstream>
|
||||
|
||||
#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<http::string_body>;
|
|||
|
||||
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
|
||||
{
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
#pragma once
|
||||
|
||||
#include <string_view>
|
||||
|
||||
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;
|
||||
};
|
||||
}
|
||||
|
|
|
@ -0,0 +1,56 @@
|
|||
#include <fstream>
|
||||
|
||||
#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;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
#pragma once
|
||||
|
||||
#include <boost/asio/ip/tcp.hpp>
|
||||
#include <boost/asio/strand.hpp>
|
||||
#include <boost/beast/core.hpp>
|
||||
#include <boost/beast/http.hpp>
|
||||
#include <boost/json.hpp>
|
||||
|
||||
#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<http::string_body>;
|
||||
using StringResponse = http::response<http::string_body>;
|
||||
|
||||
StringResponse GetMapByIdHandler(const StringRequest&);
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
#pragma once
|
||||
|
||||
#include <string_view>
|
||||
|
||||
namespace http_server
|
||||
{
|
||||
using namespace std;
|
||||
|
||||
constexpr static string_view k_MapsRoute = "/api/v1/maps"sv;
|
||||
constexpr static string_view k_StaticAssetsPRoute = "/public"sv;
|
||||
|
||||
}
|
Loading…
Reference in New Issue