forked from lthn/blockchain
adds blockcontroller / chain data
This commit is contained in:
parent
518e002cb3
commit
668a747325
5 changed files with 41 additions and 14 deletions
|
|
@ -1,4 +1,5 @@
|
|||
#include "ApiServer.hpp"
|
||||
#include "controller/ApiCoreInfoComponent.hpp"
|
||||
#include "controller/InfoController.hpp"
|
||||
#include "controller/BlockController.hpp"
|
||||
|
||||
|
|
@ -23,7 +24,8 @@ void ApiServer::init_options(boost::program_options::options_description& desc)
|
|||
command_line::add_arg(desc, arg_api_bind_host);
|
||||
}
|
||||
|
||||
ApiServer::ApiServer(const boost::program_options::variables_map& vm) : m_vm(vm) {
|
||||
ApiServer::ApiServer(const boost::program_options::variables_map& vm, currency::core* ccore, p2psrv_t* p2p, currency::core_rpc_server* rpc_server)
|
||||
: m_vm(vm), m_ccore(ccore), m_p2p(p2p), m_rpc_server(rpc_server) {
|
||||
if (vm.count(arg_api_bind_port.name)) {
|
||||
m_port = vm[arg_api_bind_port.name].as<uint16_t>();
|
||||
}
|
||||
|
|
@ -37,6 +39,11 @@ void ApiServer::run() {
|
|||
/* Register Components in scope of run() method */
|
||||
Components components;
|
||||
|
||||
OATPP_CREATE_COMPONENT(std::shared_ptr<ApiCoreInfoComponent>, coreInfoComponent)
|
||||
([this] {
|
||||
return std::make_shared<ApiCoreInfoComponent>(*m_ccore, *m_p2p, *m_rpc_server);
|
||||
}());
|
||||
|
||||
/* Get router component */
|
||||
OATPP_COMPONENT(std::shared_ptr<oatpp::web::server::HttpRouter>, router);
|
||||
|
||||
|
|
@ -45,11 +52,11 @@ void ApiServer::run() {
|
|||
auto infoController = std::make_shared<InfoController>();
|
||||
docEndpoints->append(infoController->getEndpoints());
|
||||
|
||||
// auto blockController = std::make_shared<BlockController>();
|
||||
// docEndpoints->append(blockController->getEndpoints());
|
||||
auto blockController = std::make_shared<BlockController>();
|
||||
docEndpoints->append(blockController->getEndpoints());
|
||||
|
||||
router->addController(infoController);
|
||||
// router->addController(blockController);
|
||||
router->addController(blockController);
|
||||
|
||||
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::swagger::DocumentInfo>, swaggerDocumentInfo)
|
||||
([]
|
||||
|
|
|
|||
|
|
@ -1,8 +1,11 @@
|
|||
#ifndef ApiServer_hpp
|
||||
#define ApiServer_hpp
|
||||
|
||||
#include "currency_core/blockchain_storage.h"
|
||||
#include "currency_core/currency_core.h"
|
||||
#include "p2p/net_node.h"
|
||||
#include "currency_protocol/currency_protocol_handler.h"
|
||||
#include "rpc/core_rpc_server.h"
|
||||
|
||||
#include "oatpp/web/server/HttpConnectionHandler.hpp"
|
||||
#include "oatpp/network/tcp/server/ConnectionProvider.hpp"
|
||||
#include "oatpp/parser/json/mapping/ObjectMapper.hpp"
|
||||
|
|
@ -14,12 +17,19 @@
|
|||
|
||||
namespace po = boost::program_options;
|
||||
|
||||
// Define the p2p server type to avoid verbose template syntax
|
||||
typedef nodetool::node_server<currency::t_currency_protocol_handler<currency::core>> p2psrv_t;
|
||||
|
||||
class ApiServer {
|
||||
private:
|
||||
std::thread m_server_thread;
|
||||
std::shared_ptr<oatpp::network::Server> m_server;
|
||||
boost::program_options::variables_map m_vm;
|
||||
|
||||
currency::core* m_ccore;
|
||||
p2psrv_t* m_p2p;
|
||||
currency::core_rpc_server* m_rpc_server;
|
||||
|
||||
void run();
|
||||
|
||||
public:
|
||||
|
|
@ -28,7 +38,7 @@ public:
|
|||
|
||||
static void init_options(po::options_description& desc);
|
||||
|
||||
ApiServer(const boost::program_options::variables_map& vm);
|
||||
ApiServer(const boost::program_options::variables_map& vm, currency::core* ccore, p2psrv_t* p2p, currency::core_rpc_server* rpc_server);
|
||||
|
||||
class Components {
|
||||
public:
|
||||
|
|
|
|||
|
|
@ -35,10 +35,14 @@ target_link_libraries(lthn_api PUBLIC
|
|||
# This assumes the executable is in 'bin' and resources are in 'share/lthn_api'.
|
||||
target_compile_definitions(lthn_api PUBLIC -DOATPP_SWAGGER_RES_PATH="../share/lthn_api/res")
|
||||
|
||||
# Install swagger resources to a conventional location.
|
||||
# Copy resources to build directory for development builds (e.g. running in CLion)
|
||||
file(COPY ${oatpp-swagger_INCLUDE_DIRS}/../bin/oatpp-swagger/res DESTINATION ${CMAKE_BINARY_DIR}/share/lthn_api)
|
||||
file(COPY ${CMAKE_SOURCE_DIR}/utils/sdk/spec/oas-3.0.0.json DESTINATION ${CMAKE_BINARY_DIR}/share/lthn_api)
|
||||
|
||||
# Install swagger resources to a conventional location for packaging.
|
||||
install(DIRECTORY ${oatpp-swagger_INCLUDE_DIRS}/../bin/oatpp-swagger/res DESTINATION share/lthn_api)
|
||||
|
||||
# Install OpenAPI spec for SDK generation
|
||||
# Install OpenAPI spec for SDK generation for packaging.
|
||||
install(FILES ${CMAKE_SOURCE_DIR}/utils/sdk/spec/oas-3.0.0.json DESTINATION share/lthn_api)
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -2,22 +2,28 @@
|
|||
#define ApiCoreInfoComponent_hpp
|
||||
|
||||
#include "currency_core/currency_core.h"
|
||||
#include "currency_protocol/currency_protocol_handler.h" // Full definition needed for template instantiation
|
||||
#include "p2p/net_node.h"
|
||||
#include "currency_protocol/currency_protocol_handler.h"
|
||||
#include "rpc/core_rpc_server.h"
|
||||
|
||||
// Define the p2p server type to avoid verbose template syntax
|
||||
typedef nodetool::node_server<currency::t_currency_protocol_handler<currency::core>> p2psrv_t;
|
||||
|
||||
// A simple holder for core blockchain components that can be injected into controllers.
|
||||
class ApiCoreInfoComponent {
|
||||
private:
|
||||
currency::core& m_core;
|
||||
nodetool::node_server<currency::t_currency_protocol_handler<currency::core>>& m_p2p;
|
||||
p2psrv_t& m_p2p;
|
||||
currency::core_rpc_server& m_rpc_server;
|
||||
|
||||
public:
|
||||
ApiCoreInfoComponent(currency::core& core, nodetool::node_server<currency::t_currency_protocol_handler<currency::core>>& p2p)
|
||||
: m_core(core), m_p2p(p2p)
|
||||
ApiCoreInfoComponent(currency::core& core, p2psrv_t& p2p, currency::core_rpc_server& rpc_server)
|
||||
: m_core(core), m_p2p(p2p), m_rpc_server(rpc_server)
|
||||
{}
|
||||
|
||||
currency::core& getCore() { return m_core; }
|
||||
nodetool::node_server<currency::t_currency_protocol_handler<currency::core>>& getP2p() { return m_p2p; }
|
||||
p2psrv_t& getP2p() { return m_p2p; }
|
||||
currency::core_rpc_server& getRpcServer() { return m_rpc_server; }
|
||||
};
|
||||
|
||||
#endif /* ApiCoreInfoComponent_hpp */
|
||||
|
|
|
|||
|
|
@ -470,7 +470,7 @@ int main(int argc, char* argv[])
|
|||
|
||||
// Initialize API server
|
||||
oatpp::base::Environment::init();
|
||||
ApiServer api_server(vm);
|
||||
ApiServer api_server(vm, &ccore, &p2psrv, &rpc_server);
|
||||
api_server.start();
|
||||
|
||||
// Setup signal handler to gracefully stop the main p2p loop
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue