generated from Sithas/conan_template
Базовая компиляция контроллеров и executor'ов
This commit is contained in:
@@ -1,7 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include <boost/beast.hpp>
|
||||
|
||||
#include "Controller.h"
|
||||
#include "AuthRegistrationExecutor.h"
|
||||
#include "./../helpers/helpers.h"
|
||||
|
||||
namespace uad
|
||||
@@ -11,10 +15,18 @@ void HandleRequest(
|
||||
boost::beast::string_view doc_root,
|
||||
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)
|
||||
{
|
||||
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::content_type, "text/html");
|
||||
res.keep_alive(req.keep_alive());
|
||||
@@ -26,7 +38,8 @@ void HandleRequest(
|
||||
auto const not_found = [&req](boost::beast::string_view target)
|
||||
{
|
||||
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::content_type, "text/html");
|
||||
res.keep_alive(req.keep_alive());
|
||||
@@ -38,7 +51,8 @@ void HandleRequest(
|
||||
auto const server_error = [&req](boost::beast::string_view what)
|
||||
{
|
||||
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::content_type, "text/html");
|
||||
res.keep_alive(req.keep_alive());
|
||||
@@ -47,14 +61,24 @@ void HandleRequest(
|
||||
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 &&
|
||||
req.method() != boost::beast::http::verb::head)
|
||||
req.method() != boost::beast::http::verb::head)
|
||||
return send(bad_request("Unknown boost::beast::HTTP-method"));
|
||||
|
||||
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"));
|
||||
|
||||
std::string path = PathCat(doc_root, req.target());
|
||||
@@ -75,8 +99,10 @@ void HandleRequest(
|
||||
|
||||
if (req.method() == boost::beast::http::verb::head)
|
||||
{
|
||||
boost::beast::http::response<boost::beast::http::empty_body> res{boost::beast::http::status::ok,
|
||||
req.version()};
|
||||
boost::beast::http::response<boost::beast::http::empty_body> res{
|
||||
boost::beast::http::status::ok,
|
||||
req.version()
|
||||
};
|
||||
res.set(boost::beast::http::field::server, BOOST_BEAST_VERSION_STRING);
|
||||
res.set(boost::beast::http::field::content_type, MimeType(path));
|
||||
res.content_length(size);
|
||||
@@ -85,12 +111,13 @@ void HandleRequest(
|
||||
}
|
||||
|
||||
boost::beast::http::response<boost::beast::http::file_body> res{
|
||||
std::piecewise_construct, std::make_tuple(std::move(body)),
|
||||
std::make_tuple(boost::beast::http::status::ok, req.version())};
|
||||
std::piecewise_construct, std::make_tuple(std::move(body)),
|
||||
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::content_type, MimeType(path));
|
||||
res.content_length(size);
|
||||
res.keep_alive(req.keep_alive());
|
||||
return send(std::move(res));
|
||||
}
|
||||
} // namespace uad
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user