generated from Sithas/conan_template
Базовая компиляция контроллеров и executor'ов
This commit is contained in:
@@ -42,6 +42,8 @@ add_executable(App ./src/main.cpp
|
|||||||
./src/entities/User.h
|
./src/entities/User.h
|
||||||
./src/DAO/MySQLUserDAO.cpp
|
./src/DAO/MySQLUserDAO.cpp
|
||||||
./src/DAO/MySQLUserDAO.h
|
./src/DAO/MySQLUserDAO.h
|
||||||
|
src/endpoints_handlers/IExecutor.h
|
||||||
|
src/endpoints_handlers/AuthRegistrationExecutor.h
|
||||||
)
|
)
|
||||||
|
|
||||||
target_link_libraries(App PRIVATE Boost::boost Threads::Threads mysql::concpp)
|
target_link_libraries(App PRIVATE Boost::boost Threads::Threads mysql::concpp)
|
||||||
|
|||||||
@@ -0,0 +1,26 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "IExecutor.h"
|
||||||
|
|
||||||
|
namespace uad
|
||||||
|
{
|
||||||
|
template <class Body, class Allocator, class ResponseType>
|
||||||
|
class AuthRegistrationExecutor : public IExecutor<Body, Allocator, ResponseType>
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
boost::beast::http::response<ResponseType> operator ()(
|
||||||
|
boost::beast::http::request<Body, boost::beast::http::basic_fields<Allocator>>&& req
|
||||||
|
) override
|
||||||
|
{
|
||||||
|
boost::beast::http::response<ResponseType> res{
|
||||||
|
boost::beast::http::status::ok, req.version()
|
||||||
|
};
|
||||||
|
|
||||||
|
res.body() = "{ \"detail\": \"ok\"}";
|
||||||
|
res.set(boost::beast::http::field::content_type, "application/json");
|
||||||
|
res.content_length(res.body().size());
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -7,7 +7,7 @@
|
|||||||
namespace uad
|
namespace uad
|
||||||
{
|
{
|
||||||
template <class Body, class Allocator, class ResponseType>
|
template <class Body, class Allocator, class ResponseType>
|
||||||
class Controller : IController<Body, Allocator, ResponseType>
|
class Controller : public IController<Body, Allocator, ResponseType>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
using HTTPMethodsToExecutors = std::unordered_map<boost::beast::http::verb, std::shared_ptr<IExecutor<Body, Allocator, ResponseType>>>;
|
using HTTPMethodsToExecutors = std::unordered_map<boost::beast::http::verb, std::shared_ptr<IExecutor<Body, Allocator, ResponseType>>>;
|
||||||
|
|||||||
@@ -1,7 +1,11 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
#include <boost/beast.hpp>
|
#include <boost/beast.hpp>
|
||||||
|
|
||||||
|
#include "Controller.h"
|
||||||
|
#include "AuthRegistrationExecutor.h"
|
||||||
#include "./../helpers/helpers.h"
|
#include "./../helpers/helpers.h"
|
||||||
|
|
||||||
namespace uad
|
namespace uad
|
||||||
@@ -11,10 +15,18 @@ void HandleRequest(
|
|||||||
boost::beast::string_view doc_root,
|
boost::beast::string_view doc_root,
|
||||||
boost::beast::http::request<Body, boost::beast::http::basic_fields<Allocator>>&& req, Send&& send)
|
boost::beast::http::request<Body, boost::beast::http::basic_fields<Allocator>>&& req, Send&& send)
|
||||||
{
|
{
|
||||||
|
using IRouteExecutor = IExecutor<Body, Allocator, boost::beast::http::string_body>;
|
||||||
|
using RouteAuthRegistrationExecutor = AuthRegistrationExecutor<
|
||||||
|
Body, Allocator, boost::beast::http::string_body>;
|
||||||
|
using IRouteController = IController<Body, Allocator, boost::beast::http::string_body>;
|
||||||
|
using RouteController = Controller<Body, Allocator, boost::beast::http::string_body>;
|
||||||
|
using RoutesPathes = std::unordered_map<std::string, std::unique_ptr<IRouteController>>;
|
||||||
|
|
||||||
auto const bad_request = [&req](boost::beast::string_view why)
|
auto const bad_request = [&req](boost::beast::string_view why)
|
||||||
{
|
{
|
||||||
boost::beast::http::response<boost::beast::http::string_body> res{
|
boost::beast::http::response<boost::beast::http::string_body> res{
|
||||||
boost::beast::http::status::bad_request, req.version()};
|
boost::beast::http::status::bad_request, req.version()
|
||||||
|
};
|
||||||
res.set(boost::beast::http::field::server, BOOST_BEAST_VERSION_STRING);
|
res.set(boost::beast::http::field::server, BOOST_BEAST_VERSION_STRING);
|
||||||
res.set(boost::beast::http::field::content_type, "text/html");
|
res.set(boost::beast::http::field::content_type, "text/html");
|
||||||
res.keep_alive(req.keep_alive());
|
res.keep_alive(req.keep_alive());
|
||||||
@@ -26,7 +38,8 @@ void HandleRequest(
|
|||||||
auto const not_found = [&req](boost::beast::string_view target)
|
auto const not_found = [&req](boost::beast::string_view target)
|
||||||
{
|
{
|
||||||
boost::beast::http::response<boost::beast::http::string_body> res{
|
boost::beast::http::response<boost::beast::http::string_body> res{
|
||||||
boost::beast::http::status::not_found, req.version()};
|
boost::beast::http::status::not_found, req.version()
|
||||||
|
};
|
||||||
res.set(boost::beast::http::field::server, BOOST_BEAST_VERSION_STRING);
|
res.set(boost::beast::http::field::server, BOOST_BEAST_VERSION_STRING);
|
||||||
res.set(boost::beast::http::field::content_type, "text/html");
|
res.set(boost::beast::http::field::content_type, "text/html");
|
||||||
res.keep_alive(req.keep_alive());
|
res.keep_alive(req.keep_alive());
|
||||||
@@ -38,7 +51,8 @@ void HandleRequest(
|
|||||||
auto const server_error = [&req](boost::beast::string_view what)
|
auto const server_error = [&req](boost::beast::string_view what)
|
||||||
{
|
{
|
||||||
boost::beast::http::response<boost::beast::http::string_body> res{
|
boost::beast::http::response<boost::beast::http::string_body> res{
|
||||||
boost::beast::http::status::internal_server_error, req.version()};
|
boost::beast::http::status::internal_server_error, req.version()
|
||||||
|
};
|
||||||
res.set(boost::beast::http::field::server, BOOST_BEAST_VERSION_STRING);
|
res.set(boost::beast::http::field::server, BOOST_BEAST_VERSION_STRING);
|
||||||
res.set(boost::beast::http::field::content_type, "text/html");
|
res.set(boost::beast::http::field::content_type, "text/html");
|
||||||
res.keep_alive(req.keep_alive());
|
res.keep_alive(req.keep_alive());
|
||||||
@@ -47,14 +61,24 @@ void HandleRequest(
|
|||||||
return res;
|
return res;
|
||||||
};
|
};
|
||||||
|
|
||||||
// /v1/api/MyOwnUrl?query=123 // посмотреть парсер url'ов
|
static RoutesPathes routes_pathes;
|
||||||
|
|
||||||
|
typename RouteController::HTTPMethodsToExecutors auth_registration_executors;
|
||||||
|
|
||||||
|
auth_registration_executors[boost::beast::http::verb::post] = std::make_shared<
|
||||||
|
RouteAuthRegistrationExecutor>(RouteAuthRegistrationExecutor());
|
||||||
|
|
||||||
|
std::unique_ptr<IRouteController> auth_registration_controller =
|
||||||
|
std::make_unique<RouteController>(std::move(auth_registration_executors));
|
||||||
|
|
||||||
|
routes_pathes["/api/v1/Auth/Register"] = std::move(auth_registration_controller);
|
||||||
|
|
||||||
if (req.method() != boost::beast::http::verb::get &&
|
if (req.method() != boost::beast::http::verb::get &&
|
||||||
req.method() != boost::beast::http::verb::head)
|
req.method() != boost::beast::http::verb::head)
|
||||||
return send(bad_request("Unknown boost::beast::HTTP-method"));
|
return send(bad_request("Unknown boost::beast::HTTP-method"));
|
||||||
|
|
||||||
if (req.target().empty() || req.target()[0] != '/' ||
|
if (req.target().empty() || req.target()[0] != '/' ||
|
||||||
req.target().find("..") != boost::beast::string_view::npos)
|
req.target().find("..") != boost::beast::string_view::npos)
|
||||||
return send(bad_request("Illegal request-target"));
|
return send(bad_request("Illegal request-target"));
|
||||||
|
|
||||||
std::string path = PathCat(doc_root, req.target());
|
std::string path = PathCat(doc_root, req.target());
|
||||||
@@ -75,8 +99,10 @@ void HandleRequest(
|
|||||||
|
|
||||||
if (req.method() == boost::beast::http::verb::head)
|
if (req.method() == boost::beast::http::verb::head)
|
||||||
{
|
{
|
||||||
boost::beast::http::response<boost::beast::http::empty_body> res{boost::beast::http::status::ok,
|
boost::beast::http::response<boost::beast::http::empty_body> res{
|
||||||
req.version()};
|
boost::beast::http::status::ok,
|
||||||
|
req.version()
|
||||||
|
};
|
||||||
res.set(boost::beast::http::field::server, BOOST_BEAST_VERSION_STRING);
|
res.set(boost::beast::http::field::server, BOOST_BEAST_VERSION_STRING);
|
||||||
res.set(boost::beast::http::field::content_type, MimeType(path));
|
res.set(boost::beast::http::field::content_type, MimeType(path));
|
||||||
res.content_length(size);
|
res.content_length(size);
|
||||||
@@ -85,12 +111,13 @@ void HandleRequest(
|
|||||||
}
|
}
|
||||||
|
|
||||||
boost::beast::http::response<boost::beast::http::file_body> res{
|
boost::beast::http::response<boost::beast::http::file_body> res{
|
||||||
std::piecewise_construct, std::make_tuple(std::move(body)),
|
std::piecewise_construct, std::make_tuple(std::move(body)),
|
||||||
std::make_tuple(boost::beast::http::status::ok, req.version())};
|
std::make_tuple(boost::beast::http::status::ok, req.version())
|
||||||
|
};
|
||||||
res.set(boost::beast::http::field::server, BOOST_BEAST_VERSION_STRING);
|
res.set(boost::beast::http::field::server, BOOST_BEAST_VERSION_STRING);
|
||||||
res.set(boost::beast::http::field::content_type, MimeType(path));
|
res.set(boost::beast::http::field::content_type, MimeType(path));
|
||||||
res.content_length(size);
|
res.content_length(size);
|
||||||
res.keep_alive(req.keep_alive());
|
res.keep_alive(req.keep_alive());
|
||||||
return send(std::move(res));
|
return send(std::move(res));
|
||||||
}
|
}
|
||||||
} // namespace uad
|
}
|
||||||
|
|||||||
@@ -2,18 +2,10 @@
|
|||||||
|
|
||||||
#include <boost/beast.hpp>
|
#include <boost/beast.hpp>
|
||||||
|
|
||||||
|
#include "IExecutor.h"
|
||||||
|
|
||||||
namespace uad
|
namespace uad
|
||||||
{
|
{
|
||||||
template <class Body, class Allocator, class ResponseType>
|
|
||||||
class IExecutor
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
virtual boost::beast::http::response<ResponseType> operator ()(
|
|
||||||
boost::beast::http::request<Body, boost::beast::http::basic_fields<Allocator>>&& req
|
|
||||||
) = 0;
|
|
||||||
virtual ~IExecutor() = default;
|
|
||||||
};
|
|
||||||
|
|
||||||
template <class Body, class Allocator, class ResponseType>
|
template <class Body, class Allocator, class ResponseType>
|
||||||
class IController
|
class IController
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -0,0 +1,16 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <boost/beast.hpp>
|
||||||
|
|
||||||
|
namespace uad
|
||||||
|
{
|
||||||
|
template <class Body, class Allocator, class ResponseType>
|
||||||
|
class IExecutor
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
virtual boost::beast::http::response<ResponseType> operator ()(
|
||||||
|
boost::beast::http::request<Body, boost::beast::http::basic_fields<Allocator>>&& req
|
||||||
|
) = 0;
|
||||||
|
virtual ~IExecutor() = default;
|
||||||
|
};
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user