2025-10-16 16:08:28 +01:00
|
|
|
|
// Copyright (c) 2017-2025 Lethean (https://lt.hn)
|
|
|
|
|
|
//
|
|
|
|
|
|
// Licensed under the European Union Public Licence (EUPL) version 1.2.
|
|
|
|
|
|
// You may obtain a copy of the licence at:
|
|
|
|
|
|
//
|
|
|
|
|
|
// https://joinup.ec.europa.eu/software/page/eupl/licence-eupl
|
|
|
|
|
|
//
|
|
|
|
|
|
// The EUPL is a copyleft licence that is compatible with the MIT/X11
|
|
|
|
|
|
// licence used by the original projects; but maintains OSS status,
|
|
|
|
|
|
// where regional copyright law requires ownership to dictate licence terms.
|
|
|
|
|
|
//
|
|
|
|
|
|
// SPDX‑License‑Identifier: EUPL-1.2
|
|
|
|
|
|
//
|
|
|
|
|
|
|
2025-10-09 13:44:34 +01:00
|
|
|
|
#ifndef ApiServer_hpp
|
|
|
|
|
|
#define ApiServer_hpp
|
|
|
|
|
|
|
2025-10-10 23:18:02 +01:00
|
|
|
|
#include "currency_core/currency_core.h"
|
2025-10-16 14:10:04 +01:00
|
|
|
|
#include "p2p/net_node.h"
|
|
|
|
|
|
#include "currency_protocol/currency_protocol_handler.h"
|
|
|
|
|
|
#include "rpc/core_rpc_server.h"
|
|
|
|
|
|
|
2025-10-09 13:44:34 +01:00
|
|
|
|
#include "oatpp/web/server/HttpConnectionHandler.hpp"
|
|
|
|
|
|
#include "oatpp/network/tcp/server/ConnectionProvider.hpp"
|
2025-10-10 23:18:02 +01:00
|
|
|
|
#include "oatpp/parser/json/mapping/ObjectMapper.hpp"
|
2025-10-09 13:44:34 +01:00
|
|
|
|
#include "oatpp/core/macro/component.hpp"
|
|
|
|
|
|
#include "oatpp-swagger/Resources.hpp"
|
2025-10-15 20:58:57 +01:00
|
|
|
|
#include "oatpp/network/Server.hpp"
|
|
|
|
|
|
#include <thread>
|
2025-10-15 22:28:49 +01:00
|
|
|
|
#include <boost/program_options.hpp>
|
|
|
|
|
|
|
|
|
|
|
|
namespace po = boost::program_options;
|
2025-10-15 20:58:57 +01:00
|
|
|
|
|
2025-10-16 14:10:04 +01:00
|
|
|
|
// Define the p2p server type to avoid verbose template syntax
|
|
|
|
|
|
typedef nodetool::node_server<currency::t_currency_protocol_handler<currency::core>> p2psrv_t;
|
|
|
|
|
|
|
2025-10-09 13:44:34 +01:00
|
|
|
|
class ApiServer {
|
2025-10-15 20:58:57 +01:00
|
|
|
|
private:
|
|
|
|
|
|
std::thread m_server_thread;
|
|
|
|
|
|
std::shared_ptr<oatpp::network::Server> m_server;
|
2025-10-15 22:28:49 +01:00
|
|
|
|
boost::program_options::variables_map m_vm;
|
2025-10-15 20:58:57 +01:00
|
|
|
|
|
2025-10-16 14:10:04 +01:00
|
|
|
|
currency::core* m_ccore;
|
|
|
|
|
|
p2psrv_t* m_p2p;
|
|
|
|
|
|
currency::core_rpc_server* m_rpc_server;
|
|
|
|
|
|
|
2025-10-15 20:58:57 +01:00
|
|
|
|
void run();
|
2025-10-09 13:44:34 +01:00
|
|
|
|
|
2025-10-10 23:18:02 +01:00
|
|
|
|
public:
|
2025-10-15 22:28:49 +01:00
|
|
|
|
static uint16_t m_port;
|
|
|
|
|
|
static std::string m_host;
|
|
|
|
|
|
|
|
|
|
|
|
static void init_options(po::options_description& desc);
|
2025-10-09 13:44:34 +01:00
|
|
|
|
|
2025-10-16 14:10:04 +01:00
|
|
|
|
ApiServer(const boost::program_options::variables_map& vm, currency::core* ccore, p2psrv_t* p2p, currency::core_rpc_server* rpc_server);
|
2025-10-10 23:18:02 +01:00
|
|
|
|
|
|
|
|
|
|
class Components {
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
|
|
|
|
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::network::ServerConnectionProvider>, serverConnectionProvider)([] {
|
2025-10-15 22:28:49 +01:00
|
|
|
|
return oatpp::network::tcp::server::ConnectionProvider::createShared({ApiServer::m_host, ApiServer::m_port, oatpp::network::Address::IP_4});
|
2025-10-10 23:18:02 +01:00
|
|
|
|
}());
|
|
|
|
|
|
|
|
|
|
|
|
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::web::server::HttpRouter>, httpRouter)([] {
|
|
|
|
|
|
return oatpp::web::server::HttpRouter::createShared();
|
|
|
|
|
|
}());
|
|
|
|
|
|
|
|
|
|
|
|
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::network::ConnectionHandler>, serverConnectionHandler)([] {
|
|
|
|
|
|
OATPP_COMPONENT(std::shared_ptr<oatpp::web::server::HttpRouter>, router);
|
|
|
|
|
|
return oatpp::web::server::HttpConnectionHandler::createShared(router);
|
|
|
|
|
|
}());
|
|
|
|
|
|
|
|
|
|
|
|
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::data::mapping::ObjectMapper>, apiObjectMapper)([] {
|
|
|
|
|
|
auto jsonMapper = oatpp::parser::json::mapping::ObjectMapper::createShared();
|
|
|
|
|
|
jsonMapper->getSerializer()->getConfig()->useBeautifier = true;
|
|
|
|
|
|
return jsonMapper;
|
|
|
|
|
|
}());
|
|
|
|
|
|
|
|
|
|
|
|
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::swagger::Resources>, swaggerResources)([] {
|
|
|
|
|
|
return oatpp::swagger::Resources::loadResources(OATPP_SWAGGER_RES_PATH);
|
|
|
|
|
|
}());
|
|
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2025-10-15 20:58:57 +01:00
|
|
|
|
void start();
|
|
|
|
|
|
void stop();
|
|
|
|
|
|
void wait();
|
2025-10-09 13:44:34 +01:00
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#endif /* ApiServer_hpp */
|