generated from Sithas/conan_template
Compare commits
5 Commits
ffad41e92b
..
master
| Author | SHA1 | Date | |
|---|---|---|---|
| e48d3f30cb | |||
| 139cf01557 | |||
| 89d7f62f28 | |||
| cfbe8b4a9f | |||
| 9e792a250e |
+16
-2
@@ -13,7 +13,7 @@ set(Boost_USE_MULTITHREADED ON)
|
||||
set(Boost_INCLUDE_DIR ${BOOST_ROOT})
|
||||
set(Boost_LIBRARY_DIR "${BOOST_ROOT}/stage/lib")
|
||||
|
||||
find_package(Boost 1.88.0 REQUIRED COMPONENTS filesystem json log)
|
||||
find_package(Boost 1.88.0 REQUIRED COMPONENTS filesystem json log system filesystem)
|
||||
if (Boost_FOUND)
|
||||
include_directories(${Boost_INCLUDE_DIR})
|
||||
endif ()
|
||||
@@ -50,9 +50,17 @@ add_executable(App ./src/main.cpp
|
||||
./src/endpoints_handlers/AuthLogoutExecutor.h
|
||||
./src/exceptions/session_exception.cpp
|
||||
./src/exceptions/session_exception.h
|
||||
src/log/Log.h
|
||||
src/log/Log.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(App PRIVATE Boost::boost Boost::json Threads::Threads mysql::concpp)
|
||||
target_link_libraries(App PRIVATE Boost::boost
|
||||
Boost::json
|
||||
Boost::log
|
||||
Boost::system
|
||||
Boost::filesystem
|
||||
Threads::Threads
|
||||
mysql::concpp)
|
||||
|
||||
if (MSVC)
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /bigobj")
|
||||
@@ -70,8 +78,14 @@ add_executable(ControllerTests ./tests/endpoint_handlers/Controller_TEST.cpp
|
||||
target_link_libraries(ControllerTests PRIVATE Boost::boost)
|
||||
add_test(ControllerTests ControllerTests)
|
||||
|
||||
add_executable(AuthRegistrationExecutorTests ./tests/endpoint_handlers/AuthRegistrationExecutor_TEST.cpp
|
||||
./src/endpoints_handlers/AuthRegistrationExecutor.h)
|
||||
target_link_libraries(AuthRegistrationExecutorTests PRIVATE Boost::boost Boost::json mysql::concpp)
|
||||
add_test(AuthRegistrationExecutorTests AuthRegistrationExecutorTests)
|
||||
|
||||
if (WIN32)
|
||||
target_compile_definitions(App PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX)
|
||||
target_compile_definitions(HelpersTests PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX)
|
||||
target_compile_definitions(ControllerTests PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX)
|
||||
target_compile_definitions(AuthRegistrationExecutorTests PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX)
|
||||
endif()
|
||||
|
||||
@@ -1,114 +0,0 @@
|
||||
#include "BasicRequestHandler.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <string_view>
|
||||
|
||||
#include "../content_type.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace string_view_literals;
|
||||
|
||||
namespace uad
|
||||
{
|
||||
namespace beast = boost::beast;
|
||||
namespace http = beast::http;
|
||||
namespace net = boost::asio;
|
||||
|
||||
http::message_generator HandleRequest(
|
||||
beast::string_view doc_root,
|
||||
http::request<http::string_body>&& req)
|
||||
{
|
||||
auto const bad_request =
|
||||
[&req](beast::string_view why)
|
||||
{
|
||||
http::response<http::string_body> res {http::status::bad_request, req.version()};
|
||||
res.set(http::field::server, BOOST_BEAST_VERSION_STRING);
|
||||
res.set(http::field::content_type, "text/html");
|
||||
res.keep_alive(req.keep_alive());
|
||||
res.body() = std::string(why);
|
||||
res.prepare_payload();
|
||||
return res;
|
||||
};
|
||||
|
||||
auto const not_found =
|
||||
[&req](beast::string_view target)
|
||||
{
|
||||
http::response<http::string_body> res {http::status::not_found, req.version()};
|
||||
res.set(http::field::server, BOOST_BEAST_VERSION_STRING);
|
||||
res.set(http::field::content_type, "text/html");
|
||||
res.keep_alive(req.keep_alive());
|
||||
res.body() = "The resource '" + std::string(target) + "' was not found.";
|
||||
res.prepare_payload();
|
||||
return res;
|
||||
};
|
||||
|
||||
auto const server_error =
|
||||
[&req](beast::string_view what)
|
||||
{
|
||||
http::response<http::string_body> res {http::status::internal_server_error, req.version()};
|
||||
res.set(http::field::server, BOOST_BEAST_VERSION_STRING);
|
||||
res.set(http::field::content_type, "text/html");
|
||||
res.keep_alive(req.keep_alive());
|
||||
res.body() = "An error occurred: '" + std::string(what) + "'";
|
||||
res.prepare_payload();
|
||||
return res;
|
||||
};
|
||||
|
||||
if (req.target() == "/HelloWorld"sv)
|
||||
{
|
||||
http::response<http::string_body> res {};
|
||||
res.body() = "{ \"Hello\": \"World\" }"sv;
|
||||
res.content_length(res.body().size());
|
||||
res.set(http::field::content_type, content_type::k_JSON);
|
||||
res.prepare_payload();
|
||||
res.keep_alive(req.keep_alive());
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
if (req.method() != http::verb::get &&
|
||||
req.method() != http::verb::head)
|
||||
return bad_request("Unknown HTTP-method");
|
||||
|
||||
if (req.target().empty() ||
|
||||
req.target()[0] != '/' ||
|
||||
req.target().find("..") != beast::string_view::npos)
|
||||
return bad_request("Illegal request-target");
|
||||
|
||||
std::string path = PathCat(doc_root, req.target());
|
||||
if (req.target().back() == '/')
|
||||
path.append("index.html");
|
||||
|
||||
beast::error_code ec;
|
||||
http::file_body::value_type body;
|
||||
body.open(path.c_str(), beast::file_mode::scan, ec);
|
||||
|
||||
if (ec == beast::errc::no_such_file_or_directory)
|
||||
return not_found(req.target());
|
||||
|
||||
if (ec)
|
||||
return server_error(ec.message());
|
||||
|
||||
auto const size = body.size();
|
||||
|
||||
if (req.method() == http::verb::head)
|
||||
{
|
||||
http::response<http::empty_body> res {http::status::ok, req.version()};
|
||||
res.set(http::field::server, BOOST_BEAST_VERSION_STRING);
|
||||
res.set(http::field::content_type, MimeType(path));
|
||||
res.content_length(size);
|
||||
res.keep_alive(req.keep_alive());
|
||||
return res;
|
||||
}
|
||||
|
||||
http::response<http::file_body> res {
|
||||
std::piecewise_construct,
|
||||
std::make_tuple(std::move(body)),
|
||||
std::make_tuple(http::status::ok, req.version())};
|
||||
res.set(http::field::server, BOOST_BEAST_VERSION_STRING);
|
||||
res.set(http::field::content_type, MimeType(path));
|
||||
res.content_length(size);
|
||||
res.keep_alive(req.keep_alive());
|
||||
return res;
|
||||
}
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <boost/beast/core.hpp>
|
||||
#include <boost/beast/http.hpp>
|
||||
#include <boost/beast/version.hpp>
|
||||
#include <boost/asio/dispatch.hpp>
|
||||
#include <boost/asio/strand.hpp>
|
||||
#include <boost/config.hpp>
|
||||
#include <algorithm>
|
||||
#include <cstdlib>
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
#include <type_traits>
|
||||
|
||||
#include "./../helpers.h"
|
||||
|
||||
namespace uad
|
||||
{
|
||||
namespace beast = boost::beast;
|
||||
namespace http = beast::http;
|
||||
namespace net = boost::asio;
|
||||
|
||||
http::message_generator HandleRequest(
|
||||
beast::string_view doc_root,
|
||||
http::request<http::string_body>&& req);
|
||||
}
|
||||
@@ -5,6 +5,8 @@
|
||||
#include <mysqlx/xdevapi.h>
|
||||
#include <mysqlx/common/api.h>
|
||||
#include <boost/uuid.hpp>
|
||||
#include <boost/log/trivial.hpp>
|
||||
#include <boost/log/utility/setup/common_attributes.hpp>
|
||||
|
||||
#include "IExecutor.h"
|
||||
#include "../DAO/IUserDAO.h"
|
||||
@@ -38,6 +40,8 @@ public:
|
||||
using namespace boost::beast;
|
||||
using namespace std::string_literals;
|
||||
|
||||
// BOOST_LOG_TRIVIAL(info) << "Auth/Login - Request";
|
||||
|
||||
const auto body = req.body();
|
||||
value req_json;
|
||||
|
||||
@@ -47,6 +51,7 @@ public:
|
||||
}
|
||||
catch (const system::system_error& err)
|
||||
{
|
||||
// BOOST_LOG_TRIVIAL(error) << "Auth/Login - Error 400";
|
||||
throw session_exception(http::status::bad_request, "cannot deserialize json");
|
||||
}
|
||||
|
||||
@@ -56,6 +61,7 @@ public:
|
||||
|
||||
if (login.empty() || password.empty())
|
||||
{
|
||||
// BOOST_LOG_TRIVIAL(error) << "Auth/Login - Error 422";
|
||||
throw session_exception(http::status::unprocessable_entity, "Login or password are empty"s);
|
||||
}
|
||||
|
||||
@@ -63,6 +69,7 @@ public:
|
||||
|
||||
if (!maybe_user.has_value() && maybe_user.value().hashed_password != HashPassword(password))
|
||||
{
|
||||
// BOOST_LOG_TRIVIAL(error) << "Auth/Login - Error 403";
|
||||
throw session_exception(http::status::forbidden,"Incorrect login or password");
|
||||
}
|
||||
const std::string token = GenerateUUID();
|
||||
@@ -78,6 +85,8 @@ public:
|
||||
res.set(http::field::content_type, "application/json");
|
||||
res.content_length(res.body().size());
|
||||
|
||||
// BOOST_LOG_TRIVIAL(info) << "Auth/Login - Success - 200";
|
||||
|
||||
return res;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -5,6 +5,8 @@
|
||||
#include <mysqlx/xdevapi.h>
|
||||
#include <mysqlx/common/api.h>
|
||||
#include <boost/uuid.hpp>
|
||||
#include <boost/log/trivial.hpp>
|
||||
#include <boost/log/utility/setup/common_attributes.hpp>
|
||||
|
||||
#include "IExecutor.h"
|
||||
#include "../DAO/IUserDAO.h"
|
||||
@@ -35,6 +37,8 @@ public:
|
||||
using namespace boost::beast;
|
||||
using namespace std::string_literals;
|
||||
|
||||
// BOOST_LOG_TRIVIAL(info) << "Auth/Logout - Request";
|
||||
|
||||
const auto body = req.body();
|
||||
value req_json;
|
||||
|
||||
@@ -44,6 +48,7 @@ public:
|
||||
}
|
||||
catch (const system::system_error& err)
|
||||
{
|
||||
// BOOST_LOG_TRIVIAL(error) << "Auth/Login - Error 500";
|
||||
throw session_exception(http::status::internal_server_error, "cannot deserialize json"s);
|
||||
}
|
||||
|
||||
@@ -51,6 +56,7 @@ public:
|
||||
|
||||
if (!auth_dao_->Logout(token))
|
||||
{
|
||||
// BOOST_LOG_TRIVIAL(error) << "Auth/Login - Error 400";
|
||||
throw session_exception(http::status::bad_request, "token is not authorized"s);
|
||||
}
|
||||
|
||||
@@ -60,6 +66,8 @@ public:
|
||||
res.set(http::field::content_type, "application/json");
|
||||
res.content_length(res.body().size());
|
||||
|
||||
// BOOST_LOG_TRIVIAL(info) << "Auth/Login - Success 200";
|
||||
|
||||
return res;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include <boost/json.hpp>
|
||||
#include <mysqlx/xdevapi.h>
|
||||
#include <boost/log/trivial.hpp>
|
||||
#include <boost/log/utility/setup/common_attributes.hpp>
|
||||
|
||||
#include "IExecutor.h"
|
||||
#include "../DAO/IUserDAO.h"
|
||||
@@ -33,6 +34,8 @@ public:
|
||||
using namespace boost::beast;
|
||||
using namespace std::string_literals;
|
||||
|
||||
// BOOST_LOG_TRIVIAL(info) << "Auth/Registration - Request";
|
||||
|
||||
const auto& body = req.body();
|
||||
value req_json;
|
||||
|
||||
@@ -42,6 +45,7 @@ public:
|
||||
}
|
||||
catch (const system::system_error& err)
|
||||
{
|
||||
// BOOST_LOG_TRIVIAL(error) << "Auth/Logout - Error 400";
|
||||
throw session_exception(http::status::bad_request, "cannot deserialize json");
|
||||
}
|
||||
|
||||
@@ -50,6 +54,7 @@ public:
|
||||
|
||||
if (!ValidateLogin(login) || !ValidatePassword(password))
|
||||
{
|
||||
// BOOST_LOG_TRIVIAL(error) << "Auth/Logout - Error 422";
|
||||
throw session_exception(
|
||||
http::status::unprocessable_entity,
|
||||
"Validations failed. Login should have length from 3 to 50. Password from 5 characters length."s
|
||||
@@ -58,6 +63,7 @@ public:
|
||||
|
||||
if (user_dao_->GetByLogin(login).has_value())
|
||||
{
|
||||
// BOOST_LOG_TRIVIAL(error) << "Auth/Logout - Error 409";
|
||||
throw session_exception(http::status::conflict, "user with login "s + login + " exists"s);
|
||||
}
|
||||
|
||||
@@ -88,6 +94,8 @@ public:
|
||||
res.set(http::field::content_type, "application/json");
|
||||
res.content_length(res.body().size());
|
||||
|
||||
// BOOST_LOG_TRIVIAL(info) << "Auth/Logout - Created 201";
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
|
||||
#include <boost/log/trivial.hpp>
|
||||
#include <boost/log/utility/setup/file.hpp>
|
||||
#include <boost/log/utility/setup/common_attributes.hpp>
|
||||
|
||||
namespace logging = boost::log;
|
||||
|
||||
namespace uad
|
||||
{
|
||||
void InitLogs()
|
||||
{
|
||||
logging::add_file_log(
|
||||
logging::keywords::file_name = "app_%Y-%m-%d_%H-%M-%S.log", // Имя файла с timestamp
|
||||
logging::keywords::rotation_size = 10 * 1024 * 1024, // Ротация при достижении 10 МБ
|
||||
logging::keywords::time_based_rotation =
|
||||
logging::sinks::file::rotation_at_time_point(0, 0, 0), // Ротация каждый день в полночь
|
||||
logging::keywords::format = "[%TimeStamp%] [%Severity%]: %Message%" // Формат записи
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
namespace uad
|
||||
{
|
||||
void InitLogs();
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
#ifdef WIN32
|
||||
#define _WIN32_WINNT 0x0602
|
||||
#include <sdkddkver.h>
|
||||
#endif
|
||||
|
||||
@@ -14,11 +15,15 @@
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
#include <boost/log/trivial.hpp>
|
||||
#include <boost/log/utility/setup/file.hpp>
|
||||
#include <boost/log/utility/setup/common_attributes.hpp>
|
||||
|
||||
#include "./session/WebsocketSession.h"
|
||||
#include "./listener/Listener.h"
|
||||
#include "./db/mysql_connector.h"
|
||||
#include "entities/user.h"
|
||||
#include "log/Log.h"
|
||||
|
||||
namespace beast = boost::beast;
|
||||
namespace http = beast::http;
|
||||
@@ -28,6 +33,7 @@ using tcp = boost::asio::ip::tcp;
|
||||
using namespace uad;
|
||||
using namespace std;
|
||||
using namespace std::string_literals;
|
||||
namespace logging = boost::log;
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
@@ -44,6 +50,11 @@ int main(int argc, char* argv[])
|
||||
auto const threads = std::max<int>(1, std::atoi(argv[4]));
|
||||
string mysql_credentials = argv[5];
|
||||
|
||||
InitLogs();
|
||||
|
||||
// Добавление общих атрибутов (включая время)
|
||||
logging::add_common_attributes();
|
||||
|
||||
uad::SetMySqlSession(new mysqlx::Session(mysql_credentials));
|
||||
|
||||
net::io_context ioc{threads};
|
||||
@@ -53,6 +64,8 @@ int main(int argc, char* argv[])
|
||||
net::signal_set signals(ioc, SIGINT, SIGTERM);
|
||||
signals.async_wait([&](beast::error_code const&, int) { ioc.stop(); });
|
||||
|
||||
BOOST_LOG_TRIVIAL(info) << "Приложение запущено";
|
||||
|
||||
std::vector<std::thread> v;
|
||||
v.reserve(threads - 1);
|
||||
for (auto i = threads - 1; i > 0; --i)
|
||||
|
||||
@@ -3,6 +3,9 @@
|
||||
#include <memory>
|
||||
#include <boost/beast.hpp>
|
||||
#include <boost/asio.hpp>
|
||||
#include <boost/log/trivial.hpp>
|
||||
#include <boost/log/utility/setup/file.hpp>
|
||||
#include <boost/log/utility/setup/common_attributes.hpp>
|
||||
|
||||
namespace uad
|
||||
{
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
#ifdef WIN32
|
||||
#include <sdkddkver.h>
|
||||
#endif
|
||||
|
||||
#define BOOST_TEST_MODULE AuthRegistrationExecutors
|
||||
|
||||
#include <boost/test/included/unit_test.hpp>
|
||||
|
||||
#include "./../../src/endpoints_handlers/AuthRegistrationExecutor.h"
|
||||
#include "./../../src/DAO/MySQLUserDAO.h"
|
||||
#include "./../../src/db/mysql_connector.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace uad;
|
||||
using namespace boost;
|
||||
|
||||
using RouteAuthRegistrationExecutor = AuthRegistrationExecutor<beast::http::string_body,
|
||||
std::allocator<char>,
|
||||
beast::http::string_body>;
|
||||
|
||||
BOOST_AUTO_TEST_CASE(AuthRegistrationExecutor_Succesful_Login)
|
||||
{
|
||||
auto user_dao = make_shared<MySQLUserDAO>(GetMySqlSession());
|
||||
auto executor = make_shared<RouteAuthRegistrationExecutor>(GetMySqlSession(), user_dao);
|
||||
|
||||
BOOST_CHECK(true == true);
|
||||
}
|
||||
Reference in New Issue
Block a user