172 lines
5.1 KiB
C++
172 lines
5.1 KiB
C++
#pragma once
|
|
|
|
#include <string>
|
|
#include <format>
|
|
#include <curl/curl.h>
|
|
|
|
#include "httplib.h"
|
|
#include "cpr/cpr.h"
|
|
#include "hack/logger/logger.hpp"
|
|
#include "hack/exception/exception.hpp"
|
|
#include "pgxx/pgxx.hpp"
|
|
|
|
#include "trs/utils/define.hpp"
|
|
#include "trs/utils/helpers.hpp"
|
|
|
|
#include "trs/utils/var.hpp"
|
|
#include "trs/libs/transaction.hpp"
|
|
#include "trs/libs/function_manager.hpp"
|
|
#include "trs/libs/inspector.hpp"
|
|
#include "trs/helpers/verification.hpp"
|
|
#include "trs/helpers/execute.hpp"
|
|
|
|
namespace trs
|
|
{
|
|
class server : public httplib::Server
|
|
{
|
|
public:
|
|
server() = default;
|
|
~server() = default;
|
|
|
|
public:
|
|
void init(std::string service_name)
|
|
{
|
|
if (!PGXX().ready())
|
|
{
|
|
hack::error()("data base manager connection not initialazed");
|
|
hack::warn()("please add conection to PGXX().init method");
|
|
return;
|
|
}
|
|
|
|
set_read_timeout(5, 0);
|
|
set_write_timeout(5, 0);
|
|
set_idle_interval(0, 1'000'000);
|
|
set_payload_max_length(1024 * 1024 * 512); // 512MB
|
|
set_CORS();
|
|
set_post();
|
|
m_service_name = service_name;
|
|
}
|
|
|
|
void log_to_console(bool v) { m_log_to_console = v; }
|
|
|
|
template<typename Function>
|
|
void registration(std::string func_name, Function insp, Function func)
|
|
{
|
|
m_inspector.registration(func_name, insp);
|
|
m_function_manager.registration(func_name, func);
|
|
}
|
|
|
|
void run()
|
|
{
|
|
try
|
|
{
|
|
hack::log(" ")(std::format("[{}]", m_service_name), "listen:", var::HOST, var::PORT);
|
|
listen(var::HOST, var::PORT);
|
|
}
|
|
catch(std::exception& e)
|
|
{
|
|
hack::error(" ")("error server run:", e.what());
|
|
}
|
|
catch(...)
|
|
{
|
|
hack::error()("SUPPER ERROR!!! GOOD LUCK MY FRIEND!!! :)");
|
|
}
|
|
}
|
|
|
|
private:
|
|
std::string m_service_name;
|
|
bool m_log_to_console { true };
|
|
function_manager<transaction> m_function_manager;
|
|
inspector<transaction> m_inspector;
|
|
|
|
private:
|
|
void set_CORS()
|
|
{
|
|
Options(R"(\*)", [](const auto& req, auto& res) { res.set_header("Allow", "POST, HEAD, OPTIONS"); });
|
|
Options(var::API_URL, [](const auto& req, auto& res)
|
|
{
|
|
res.set_header("Access-Control-Allow-Origin", "*");
|
|
res.set_header("Allow", "POST, HEAD, OPTIONS");
|
|
res.set_header( "Access-Control-Allow-Headers", std::format("X-Requested-With, Content-Type, Accept, Origin, Authorization, {}, {}", var::HEADER_TOKEN, var::HEADER_FUNCTION).c_str());
|
|
res.set_header("Access-Control-Allow-Methods", "OPTIONS, HEAD, POST");
|
|
});
|
|
}
|
|
|
|
void set_post()
|
|
{
|
|
Post(var::API_URL, [&](const httplib::Request& req, httplib::Response& res) {
|
|
res.set_header("Access-Control-Allow-Origin", "*");
|
|
res.set_header("Access-Control-Allow-Headers", "*");
|
|
res.set_header("Access-Control-Allow-Methods", "POST");
|
|
res.set_header("Access-Control-Allow-Credentials", "false");
|
|
|
|
transaction tr;
|
|
|
|
try
|
|
{
|
|
tr.set_data(req);
|
|
verification(m_inspector, m_function_manager, tr);
|
|
execute(m_function_manager, tr);
|
|
}
|
|
catch(hack::exception& ex)
|
|
{
|
|
ex.transaction(tr);
|
|
ex.service(m_service_name);
|
|
if (m_log_to_console)
|
|
ex.log();
|
|
auto r = PGXX().execute(var::LOGGER_DB, var::FUNC_LOGGER, ex.convert_to_json());
|
|
if (m_log_to_console)
|
|
hack::error()(r, tr.m_data.m_result);
|
|
}
|
|
|
|
res.set_content(nlohmann::to_string(tr.m_data.m_result), var::HEADER_FLAG_JSON);
|
|
});
|
|
}
|
|
};
|
|
|
|
class client
|
|
{
|
|
public:
|
|
client() = default;
|
|
client(std::string url) : m_url { url } {}
|
|
client(client&& c) : m_url { std::move(c.m_url) }, m_token { std::move(c.m_token) } {}
|
|
// client(std::string url) : m_url { url }, m_cli { url } {}
|
|
// client(client&& c) : m_cli{ std::move(c.m_cli) }, m_token { std::move(c.m_token) } {}
|
|
~client() = default;
|
|
|
|
public:
|
|
void set_token(std::string t) { m_token = t; }
|
|
// auto post(std::string func_name, JSON&& data)
|
|
// {
|
|
// httplib::Headers headers = {
|
|
// { var::HEADER_TOKEN, m_token },
|
|
// { var::HEADER_FUNCTION, func_name },
|
|
// { "Content-Type", var::HEADER_FLAG_JSON },
|
|
// { "Cookie", "unione_lang=ru" }
|
|
// };
|
|
//
|
|
// return m_cli.Post("/", headers, data.dump(), var::HEADER_FLAG_JSON );
|
|
// }
|
|
|
|
template<typename J>
|
|
auto post(std::string func_name, J&& data)
|
|
{
|
|
auto r = cpr::Post(cpr::Url { m_url },
|
|
cpr::Body { data.dump() },
|
|
cpr::Header {
|
|
{ "Content-Type", var::HEADER_FLAG_JSON },
|
|
{ var::HEADER_TOKEN, m_token },
|
|
{ var::HEADER_FUNCTION, func_name }
|
|
});
|
|
return r;
|
|
}
|
|
|
|
private:
|
|
std::string m_url;
|
|
// httplib::Client m_cli;
|
|
std::string m_token;
|
|
};
|
|
}
|
|
|
|
|