initial commit

This commit is contained in:
chatlanin 2024-07-09 11:07:45 +03:00
commit 88cb77935c
22 changed files with 819 additions and 0 deletions

7
.gitignore vendored Executable file
View File

@ -0,0 +1,7 @@
build
.cache
subprojects/*
!subprojects/hack.wrap
!subprojects/httplib.wrap
!subprojects/jwt-cpp.wrap
!subprojects/nlohmann_json.wrap

4
README.md Executable file
View File

@ -0,0 +1,4 @@
# Базовая реализация типового сервера для разнообразных проектов
run

53
meson.build Executable file
View File

@ -0,0 +1,53 @@
project(
meson.current_source_dir().split('/').get(-1),
'cpp',
version : '1.0.0',
default_options : [
'warning_level=1',
'optimization=3',
'cpp_std=c++20',
])
add_project_arguments (
'-Wpedantic',
'-Wno-shadow',
'-Wno-unused-but-set-variable',
'-Wno-comment',
'-Wno-unused-parameter',
'-Wno-unused-value',
'-Wno-unused-header',
'-Wno-missing-field-initializers',
'-Wno-narrowing',
'-Wno-deprecated-enum-enum-conversion',
'-Wno-volatile',
'-Wno-format-security',
'-Wno-switch',
'-Wno-ignored-attributes',
'-Wno-unused-variable',
'-Wno-deprecated-enum-enum-conversion',
language: 'cpp'
)
#############################################################
#args = ['-lglfw', '-ldl', '-lGL', '-lpthread', '-lX11', '-lXxf86vm', '-lXrandr', '-lXi']
args = []
deps = []
inc = []
deps = [
dependency('uuid'),
dependency('threads'),
dependency('libpqxx'),
subproject('hack').get_variable('hack_dep'),
subproject('nlohmann_json').get_variable('nlohmann_json_dep'),
subproject('jwt-cpp').get_variable('jwt_dep'),
subproject('httplib').get_variable('cpp_httplib_dep'),
]
subdir('src')
subdir('tests')
message('==================================================================================================================')
message('=== т.к. мы не включаем зависимости в проект, котрый пушм на гит, то их нужно все подключить в deps (см. выше) ===')
message('==================================================================================================================')

22
run.sh Executable file
View File

@ -0,0 +1,22 @@
#!/bin/zsh
PROJECT_NAME=$(basename $PWD)
run() {
command meson compile -C build
if [[ -z "$1" ]]; then
cd build
./tests/$PROJECT_NAME
cd ..
else
meson test $1 -C build
fi
}
if [ -d "build" ]; then
run
else
command meson setup build
run
fi

32
src/meson.build Executable file
View File

@ -0,0 +1,32 @@
inc += include_directories('.')
headers = [
'trs/trs.hpp',
'trs/helpers/execute.hpp',
'trs/helpers/verification.hpp',
'trs/libs/database.hpp',
'trs/libs/function_manager.hpp',
'trs/libs/transaction.hpp',
'trs/libs/inspector.hpp',
'trs/utils/define.hpp',
'trs/utils/using.hpp',
'trs/utils/var.hpp',
]
sources = [ ]
lib = library(
meson.project_name(),
include_directories : inc,
sources: [headers, sources],
dependencies : deps,
cpp_args: args
)
trs_dep = declare_dependency(
include_directories: inc,
link_with: lib,
)
deps += trs_dep

View File

@ -0,0 +1,35 @@
#pragma once
#include "hack/exception/exception.hpp"
#include "trs/utils/var.hpp"
namespace trs
{
template<typename FunctionManager, typename Transaction>
void execute(FunctionManager& fm, Transaction& tr)
{
try
{
fm.execute(tr);
}
catch (hack::exception& ex)
{
throw ex;
}
catch (std::exception& e)
{
hack::exception ex;
ex.description("it can be very dangerous. this error when we execute function");
ex.message(var::EXECUTE_ERROR);
ex.system_error(e);
throw ex;
}
catch (...)
{
hack::exception ex;
ex.description(var::ALIEN_SYSTEM_ERROR);
ex.message(var::EXECUTE_ERROR);
throw ex;
}
}
}

View File

@ -0,0 +1,42 @@
#pragma once
#include "hack/exception/exception.hpp"
#include "trs/utils/var.hpp"
namespace trs
{
template<typename Inspector, typename FunctionManager, typename Transaction>
void verification(Inspector& ins, FunctionManager& fm, Transaction& tr)
{
try
{
fm.valid(tr.m_passport.m_func_name);
}
catch (hack::exception& ex)
{
throw ex;
}
try
{
ins.valid(tr);
}
catch (hack::exception& ex)
{
throw ex;
}
catch (std::exception& e)
{
hack::exception ex;
ex.description("this payload failed verification");
ex.system_error(e);
throw ex;
}
catch (...)
{
hack::exception ex;
ex.description(var::ALIEN_SYSTEM_ERROR + " : payload failed verification");
throw ex;
}
}
}

154
src/trs/libs/database.hpp Normal file
View File

@ -0,0 +1,154 @@
#pragma once
#include <pqxx/pqxx>
#include "hack/utils/func_query.hpp"
#include "hack/utils/singleton.hpp"
#include "hack/logger/logger.hpp"
#include "hack/exception/exception.hpp"
#include "trs/utils/var.hpp"
#include "trs/utils/using.hpp"
namespace trs
{
class database : public hack::utils::singleton<database>
{
friend hack::utils::singleton<database>;
public:
~database() = default;
private:
database() = default;
std::mutex m_mutex;
std::string DB_APP_CONNECTION;
std::string DB_LOG_CONNECTION;
public:
void set_connection(std::string app, std::string log)
{
DB_APP_CONNECTION = app;
DB_LOG_CONNECTION = log;
}
template<typename... Args>
JSON execute(const std::string func_name, const Args... args)
{
std::string query;
JSON result;
try
{
query = hack::utils::make_query(func_name, args...);
result = execute(func_name, query);
}
catch(const hack::exception& ex)
{
throw ex;
}
catch (const std::exception& e)
{
hack::exception ex;
ex.description("database dont create query from args");
ex.system_error(e);
ex.params("query", query);
ex.variadic_params(args...);
throw ex;
}
return result;
}
private:
JSON execute(const std::string& func_name, const std::string& query)
{
if (DB_APP_CONNECTION.empty() || DB_LOG_CONNECTION.empty())
{
hack::error()("DB_LOG_CONNECTION or DB_LOG_CONNECTION is empty");
hack::log("")("DB_APP_CONNECTION = ", DB_APP_CONNECTION);
hack::log("")("DB_LOG_CONNECTION = ", DB_LOG_CONNECTION);
std::terminate();
}
JSON result;
pqxx::connection* app_con;
pqxx::connection* log_con;
try
{
app_con = new pqxx::connection(DB_APP_CONNECTION);
log_con = new pqxx::connection(DB_LOG_CONNECTION);
}
catch (const std::exception& e)
{
hack::error()("Dont create connection");
hack::error()(e.what());
std::terminate();
}
catch(...)
{
hack::error()("Dont create connection and no get error in description");
hack::error()("Good luck my friend! ...");
std::terminate();
}
pqxx::result r;
pqxx::nontransaction work { func_name == var::FUNC_LOGGER ? *log_con : *app_con };
try
{
std::lock_guard<std::mutex> lock(m_mutex);
r = work.exec(query);
std::string r_str;
for (auto row : r) r_str = row[func_name].c_str();
work.commit();
result = JSON::parse(r_str);
}
catch (const std::exception& e)
{
if (func_name != var::FUNC_LOGGER)
{
hack::exception ex;
ex.description("No exec query to database");
ex.message(var::EXECUTE_ERROR);
ex.system_error(e);
ex.params("query", query);
ex.params("result", result);
throw ex;
}
else
{
hack::error()("WARNING!!! ERROR LOG TO DATABASE");
hack::error()("query", query);
hack::error()(e.what());
std::terminate();
}
}
catch (...)
{
if (func_name != var::FUNC_LOGGER)
{
hack::exception ex;
ex.description(var::ALIEN_SYSTEM_ERROR);
ex.message(var::EXECUTE_ERROR);
ex.params("query", query);
ex.params("result", result);
throw ex;
}
else
{
hack::error()("WARNING!!! ERROR LOG TO DATABASE");
hack::error()("query", query);
std::terminate();
}
}
delete app_con;
delete log_con;
return result;
}
};
}

View File

@ -0,0 +1,44 @@
#pragma once
#include <map>
#include <functional>
#include "hack/exception/exception.hpp"
namespace trs
{
template<typename Transaction>
class function_manager
{
using functions = std::map<const std::string, std::function<void(Transaction&)>>;
public:
function_manager() = default;
~function_manager() = default;
private:
functions m_functions;
public:
void valid(std::string func_name) const
{
if (m_functions.count(func_name) == 0)
{
hack::exception ex;
ex.description("this function is not register in function manager");
throw ex;
}
}
template<typename Key, typename Function>
void register_function(const Key k, const Function f) noexcept
{
m_functions[k] = f;
}
void execute(Transaction& tr)
{
m_functions[tr.m_passport.m_func_name](tr);
}
};
}

View File

@ -0,0 +1,48 @@
#pragma once
#include <map>
#include <string>
#include <functional>
#include "hack/exception/exception.hpp"
namespace trs
{
template<typename Transaction>
class inspector
{
using functions = std::map<const std::string, std::function<void(Transaction&)>>;
public:
inspector() = default;
~inspector() = default;
public:
template<typename Key, typename Function>
void register_function(const Key k, const Function f) noexcept
{
m_functions[k] = f;
}
void valid(Transaction& tr)
{
if (!valid(tr.m_passport.m_func_name))
{
hack::exception ex;
ex.description("Function is not register in inspector. Please do it");
throw ex;
}
m_functions[tr.m_passport.m_func_name](tr);
}
private:
bool valid(std::string func_name) const noexcept
{
return m_functions.count(func_name) > 0;
}
private:
functions m_functions;
};
}

View File

@ -0,0 +1,122 @@
#pragma once
#include "hack/security/uuid.hpp"
#include "hack/exception/exception.hpp"
#include "hack/utils/json_converter.hpp"
#include "trs/utils/using.hpp"
#include "trs/utils/var.hpp"
namespace trs
{
class transaction : public hack::utils::json_converter<JSON>
{
public:
transaction() : m_transaction_id { hack::security::generate_uuid() } { };
~transaction() = default;
public:
std::string m_transaction_id;
struct passport
{
int m_id = -1000; // Эта сущность, которая зашифрована в токене. Например id организации
std::string m_func_name;
std::string m_token = "no token";
} m_passport;
struct data
{
JSON m_payload;
JSON m_result;
} m_data;
public:
template<typename Request>
void set_data(const Request& req)
{
set_function(req);
set_token(req);
set_payload(req);
}
JSON convert_to_json() override
{
JSON j;
j["transaction_id"] = m_transaction_id;
j["func_name"] = m_passport.m_func_name;
j["token"] = m_passport.m_token;
j["payload"] = m_data.m_payload;
j["result"] = m_data.m_result;
j["id"] = m_passport.m_id;
return j;
}
private:
template<typename Request>
void set_function(const Request& req)
{
try
{
m_passport.m_func_name = req.get_header_value("x-server-function");
if (m_passport.m_func_name.empty()) throw std::invalid_argument{ var::NO_VALID_DATA };
}
catch(std::exception& e)
{
}
catch(...)
{
hack::exception ex;
ex.description(var::ALIEN_SYSTEM_ERROR);
throw ex;
}
}
template<typename Request>
void set_token(const Request &req)
{
try
{
m_passport.m_token = req.get_header_value("x-server-token");
if (m_passport.m_token.empty()) throw std::invalid_argument{ var::NO_VALID_DATA };
}
catch(std::exception& e)
{
hack::exception ex;
ex.description("dont get token from headers, because it system error, or maybe is field be empty");
ex.system_error(e);
throw ex;
}
catch(...)
{
hack::exception ex;
ex.description(var::ALIEN_SYSTEM_ERROR);
throw ex;
}
}
template<typename Request>
void set_payload(const Request &req)
{
if (req.body.empty()) return;
try
{
m_data.m_payload = JSON::parse(req.body);
}
catch(const std::exception& e)
{
hack::exception ex;
ex.description("Dont parser body from string");
ex.system_error(e);
throw ex;
}
catch(...)
{
hack::exception ex;
ex.description(var::ALIEN_SYSTEM_ERROR);
throw ex;
}
}
};
}

108
src/trs/trs.hpp Normal file
View File

@ -0,0 +1,108 @@
#pragma once
#include <string>
#include "httplib.h"
#include "hack/logger/logger.hpp"
#include "trs/utils/var.hpp"
#include "trs/libs/transaction.hpp"
#include "trs/libs/function_manager.hpp"
#include "trs/libs/inspector.hpp"
#include "trs/libs/database.hpp"
#include "trs/helpers/verification.hpp"
#include "trs/helpers/execute.hpp"
namespace trs
{
class http : public httplib::Server
{
public:
http() = default;
~http() = default;
public:
void init(std::string app_connection, std::string log_connection)
{
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();
database::instance().set_connection(app_connection, log_connection);
}
template<typename Key, typename Function>
void register_function(Key func_name, Function func) { m_function_manager.register_function(func_name, func); }
template<typename Key, typename Function>
void register_inspector(Key func_name, Function func) { m_inspector.register_function(func_name, func); }
void run()
{
// HERE
// может в init перенести ???
set_post();
try
{
hack::log(" ")("server 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:
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", "X-Requested-With, Content-Type, Accept, Origin, Authorization, X-server-function, X-token-auth");
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.commit<database>();
ex.commit();
}
res.set_content(nlohmann::to_string(tr.m_data.m_result), var::HEADER_FLAG_JSON);
});
}
};
}

22
src/trs/utils/define.hpp Normal file
View File

@ -0,0 +1,22 @@
#pragma once
// // ERRORS DEFINED
// #define ALIEN_SYSTEM_ERROR() \
// try_exception try_ex; \
// try_ex.set_description(var::ALIEN_SYSTEM_ERROR); \
// try_ex.set_msg_to_front(var::status::ERROR); \
// throw try_ex
//
// #define STL_ERROR(msg) \
// try_exception try_ex; \
// try_ex.set_description(msg); \
// try_ex.set_system_error(e); \
// try_ex.set_msg_to_front(var::status::ERROR); \
// throw try_ex
// HERE
// для малоли что. можно использовать в сию-минутном логировании без боязни забыть
// можно перенести это как дополнение в логер в hack
#define TRS_LOG(...) if (trs::var::status::DEBUG) hack::log(": ")(__VA_ARGS__)
#define TRS_ERROR(...) if (trs::var::status::DEBUG) hack::error(": ")(__VA_ARGS__)

8
src/trs/utils/using.hpp Normal file
View File

@ -0,0 +1,8 @@
#pragma once
#include "nlohmann/json.hpp"
namespace trs
{
using JSON = nlohmann::json;
}

17
src/trs/utils/var.hpp Normal file
View File

@ -0,0 +1,17 @@
#pragma once
#include <string>
namespace trs::var
{
inline const int PORT = 5000;
inline const std::string HOST = "0.0.0.0";
inline const std::string API_URL = "/";
inline const std::string ALIEN_SYSTEM_ERROR = "is alien system error it can be very dangerous!!! good luck my friend!";
inline const std::string NO_VALID_DATA = "no valid data";
inline const std::string EXECUTE_ERROR = "execute error";
inline const std::string HEADER_FLAG_JSON = "application/json";
inline const std::string FUNC_LOGGER = "logger";
}

6
subprojects/hack.wrap Executable file
View File

@ -0,0 +1,6 @@
[wrap-git]
url = https://gitcast.ru/chatlanin/hack.git
revision = master
[provide]
hack = hack_dep

4
subprojects/httplib.wrap Normal file
View File

@ -0,0 +1,4 @@
[wrap-git]
url = https://github.com/yhirose/cpp-httplib.git
revision = master

4
subprojects/jwt-cpp.wrap Normal file
View File

@ -0,0 +1,4 @@
[wrap-git]
url = https://gitcast.ru/chatlanin/jwt-cpp.git
revision = master

View File

@ -0,0 +1,10 @@
[wrap-file]
directory = nlohmann_json-3.11.2
lead_directory_missing = true
source_url = https://github.com/nlohmann/json/releases/download/v3.11.2/include.zip
source_filename = nlohmann_json-3.11.2.zip
source_hash = e5c7a9f49a16814be27e4ed0ee900ecd0092bfb7dbfca65b5a421b774dccaaed
wrapdb_version = 3.11.2-1
[provide]
nlohmann_json = nlohmann_json_dep

36
tests/main.cpp Normal file
View File

@ -0,0 +1,36 @@
#include "hack/exception/exception.hpp"
#include "trs/trs.hpp"
#include "trs/utils/var.hpp"
namespace worckspaces
{
inline void provider(trs::transaction& tr)
{
hack::log()("provider", tr.m_data.m_payload);
tr.m_data.m_result["status"] = "ok";
tr.m_data.m_result["result"] = "super ok";
}
inline void inspector(trs::transaction& tr)
{
if (tr.m_data.m_payload.size() != 0)
{
hack::exception ex;
ex.description("transaction payload size > 0 but payload must be empty");
ex.message(trs::var::NO_VALID_DATA);
ex.commit();
throw ex;
}
}
}
auto main(int argc, char* args[]) -> int
{
trs::http app;
app.register_function("healthcheck", worckspaces::provider);
app.register_inspector("healthcheck", worckspaces::inspector);
app.init("app_connection", "log_connection");
app.run();
}

8
tests/meson.build Normal file
View File

@ -0,0 +1,8 @@
executable(
meson.project_name(),
'main.cpp',
dependencies : deps,
cpp_args: args
)

33
tmp_/meson.build Executable file
View File

@ -0,0 +1,33 @@
inc += include_directories('.')
headers = [
'trs.hpp',
'helpers/execute.hpp',
'helpers/verification.hpp',
'libs/database.hpp',
'libs/function_manager.hpp',
'libs/transaction.hpp',
'libs/inspector.hpp',
'utils/define.hpp',
'utils/using.hpp',
'utils/var.hpp',
]
sources = [ ]
lib = library(
meson.project_name(),
include_directories : inc,
sources: [headers, sources],
dependencies : deps,
cpp_args: args
)
trs_dep = declare_dependency(
include_directories: inc,
link_with: lib,
)
deps += trs_dep