move handler to special function
This commit is contained in:
parent
0ac648fb42
commit
d647b2af5d
|
@ -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)
|
||||
|
|
29
main.cpp
29
main.cpp
|
@ -10,6 +10,7 @@
|
|||
#include <fstream>
|
||||
|
||||
#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<http::string_body>;
|
|||
// Ответ, тело которого представлено в виде строки
|
||||
using StringResponse = http::response<http::string_body>;
|
||||
|
||||
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(),
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
#include <string_view>
|
||||
|
||||
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;
|
||||
};
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
#include "GetMapsHandler.h"
|
||||
|
||||
#include <fstream>
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -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 GetMapsHandler(const StringRequest&);
|
||||
}
|
Loading…
Reference in New Issue